66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace admin\models;
|
|||
|
|
|
|||
|
|
use yii\base\Model;
|
|||
|
|
use yii\db\ActiveRecord;
|
|||
|
|
use yii\web\IdentityInterface;
|
|||
|
|
|
|||
|
|
class User extends ActiveRecord implements IdentityInterface
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public static function tableName()
|
|||
|
|
{
|
|||
|
|
return '{{%admin}}';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//通过ID,返回用户实例
|
|||
|
|
public static function findIdentity($id)
|
|||
|
|
{
|
|||
|
|
// TODO: Implement findIdentity() method.
|
|||
|
|
return static::findOne($id);
|
|||
|
|
}
|
|||
|
|
//通过令牌,返回用户实例,一般用于无状态的restful应用
|
|||
|
|
//如果你的应用不需要用到,直接留空就行
|
|||
|
|
public static function findIdentityByAccessToken($token, $type = null)
|
|||
|
|
{
|
|||
|
|
return static::findOne(['access_token'=>$token]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//通过用户名,返回用户实例
|
|||
|
|
public static function findByUsername($name)
|
|||
|
|
{
|
|||
|
|
return static::findOne(['name' => $name]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//获取用户ID
|
|||
|
|
public function getId()
|
|||
|
|
{
|
|||
|
|
return $this->id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//获取用户认证密钥
|
|||
|
|
public function getAuthKey()
|
|||
|
|
{
|
|||
|
|
return $this->auth_key;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//生成cookie中的authkey
|
|||
|
|
public function generateAuthKey()
|
|||
|
|
{
|
|||
|
|
$this->auth_key=\Yii::$app->security->generateRandomString(32);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//验证用户认证密钥
|
|||
|
|
public function validateAuthKey($authKey)
|
|||
|
|
{
|
|||
|
|
// TODO: Implement validateAuthKey() method.
|
|||
|
|
return $this->getAuthKey()===$authKey;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//验证密码是否正确,当然我们也可以自已定义加密解密方式
|
|||
|
|
public function validatePassword($password)
|
|||
|
|
{
|
|||
|
|
return \Yii::$app->security->validatePassword($password,$this->pwd);
|
|||
|
|
}
|
|||
|
|
}
|