有一种方法(在models / User.php中)用我的自定义表的用户名和密码替换默认的Yii用户(例如admin,demo等)?
private static $users = [
'100' => [
'id' => '100',
'username' => 'admin',
'password' => 'admin',
'authKey' => 'test100key',
'accessToken' => '100-token',
],
'101' => [
'id' => '101',
'username' => 'demo',
'password' => 'demo',
'authKey' => 'test101key',
'accessToken' => '101-token',
],
];
答案 0 :(得分:0)
您应该从\yii\web\IdentityInterface
实施您的用户类并实现这些功能
public static function findIdentity($id) {
$model = self::findOne(['users_id' => $id]);
return ($model ? new static($model) : NULL);
}
public static function findIdentityByAccessToken($token, $type = null) {
return null;
}
public function getId() {
return $this->users_id;
}
public function getAuthKey() {
return FALSE;
}
public function validateAuthKey($authKey) {
}
public static function findByid($id) {
return self::findOne(['users_id' => $id]);
}
public function validatePassword($password) {
return Yii::$app->security->validatePassword($password, $this->uusers_password);
}
public function setPassword($password) {
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
并更改此类登录表单以从数据库中获取用户
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = Users::findByid($this->username);
}
return $this->_user;
}