2026-05-29 19:54:56 +08:00

155 lines
4.3 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/6/12
* Time: 10:36
*/
namespace app\models;
use common\utils\EnumUtil;
use common\utils\RedisKeyUtil;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/**
* 管理
* @property integer $id
* @property string $username
* @property string $password_hash
* @property string $password_reset_token
* @property string $auth_key
* @property string $role
* @property integer $del
* @property integer $created_at
* @property integer $updated_at
*/
class Admin extends ActiveRecord implements IdentityInterface
{
public $token = '';
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'trim'],
['password_hash', 'string', 'min' => 6],
['password_reset_token', 'default', 'value' => ''],
['auth_key', 'default', 'value' => ''],
['del', 'default', 'value' => EnumUtil::DELETE_NOT],
['del', 'in', 'range' => EnumUtil::DELETE_LIST],
];
}
/**
* Finds an identity by the given ID.
* @param string|int $id the ID to be looked for
* @return IdentityInterface the identity object that matches the given ID.
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentity($id)
{
return self::findOne(['id' => $id, 'del' => EnumUtil::DELETE_NOT]);
}
/**
* Finds an identity by the given token.
* @param mixed $token the token to be looked for
* @param mixed $type the type of the token. The value of this parameter depends on the implementation.
* For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
* @return IdentityInterface the identity object that matches the given token.
* Null should be returned if such an identity cannot be found
* or the identity is not in an active state (disabled, deleted, etc.)
*/
public static function findIdentityByAccessToken($token, $type = null)
{
}
public static function findByUsername($username)
{
return self::findOne(['username' => $username, 'del' => EnumUtil::DELETE_NOT]);
}
/**
* Returns an ID that can uniquely identify a user identity.
* @return string|int an ID that uniquely identifies a user identity.
*/
public function getId()
{
return $this->id;
}
/**
* Returns a key that can be used to check the validity of a given identity ID.
*
* The key should be unique for each individual user, and should be persistent
* so that it can be used to check the validity of the user identity.
*
* The space of such keys should be big enough to defeat potential identity attacks.
*
* This is required if [[User::enableAutoLogin]] is enabled.
* @return string a key that is used to check the validity of a given identity ID.
* @see validateAuthKey()
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* Validates the given auth key.
*
* This is required if [[User::enableAutoLogin]] is enabled.
* @param string $authKey the given auth key
* @return bool whether the given auth key is valid.
* @see getAuthKey()
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() == $authKey;
}
public function validatePassword($password)
{
return Yii::$app->getSecurity()->validatePassword($password, $this->password_hash);
}
public function getRole()
{
return (object)['name' => $this->role];
}
public static function loadByToken($token)
{
return Yii::$app->redis->get(RedisKeyUtil::backAdminToken($token));
}
public function saveToken()
{
Yii::$app->redis->set(RedisKeyUtil::backAdminToken($this->token), $this->id);
}
public static function loginOut($token)
{
Yii::$app->redis->del(RedisKeyUtil::backAdminToken($token));
Yii::$app->user->logout();
}
}