Files
xk-api-yii/admin/models/User.php
2024-09-19 11:32:39 +08:00

66 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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);
}
}