Can';t存储用户标识Yii2
当我的用户登录并顺利通过验证时,我在Can';t存储用户标识Yii2,yii2,identity,Yii2,Identity,当我的用户登录并顺利通过验证时,我在LoginForm.php中有一个login函数,该函数必须在会话中保存用户。我的函数如下所示: public function login() { if ($this->validate()) { $email = $this->email; $user = $this::find()->where(['email' => $email])->asArray
LoginForm.php
中有一个login
函数,该函数必须在会话中保存用户。我的函数如下所示:
public function login()
{
if ($this->validate()) {
$email = $this->email;
$user = $this::find()->where(['email' => $email])->asArray()->one();
$user['password'] = '_';
Yii::$app->getUser()->login($user);
}
return false;
}
在SiteController
的内部,我有以下功能:
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->login()) {
$this->redirect(['users/profile']);
}
return $this->render('login', [
'model' => $model
]);
}
问题出在我的login
函数中,因为当我在if语句中注释&&&$model->login()
部分时,错误消失了
下面是我得到的错误:
Argument 1 passed to yii\web\User::login() must be an instance of yii\web\IdentityInterface, array given, called in C:\xampp\htdocs\yii_grig\models\LoginForm.php on line 72
错误是我的login
function-Yii::$app->getUser()->login($user)中的这一行代码>
这就是我的models/User.php
文件的外观:
<?
namespace app\models;
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
}
应该是这样的:
use common\models\User;
...
/**
* Logs in a user using the provided username and password.
*
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
$user = $this->getUser();
if($user){
if($user->status === User::STATUS_ACTIVE){
return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0); //set session
}
//any other logic
}
return false;
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByEmail($this->username);
}
return $this->_user;
}
希望能有所帮助。这就是我们应该做的:
use common\models\User;
...
/**
* Logs in a user using the provided username and password.
*
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
$user = $this->getUser();
if($user){
if($user->status === User::STATUS_ACTIVE){
return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0); //set session
}
//any other logic
}
return false;
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByEmail($this->username);
}
return $this->_user;
}
希望能有所帮助。尝试将此代码用于您的模型LoginForm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
尝试将此代码用于您的模型LoginForm.php
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
您只需删除登录方法中的->asArray()
调用即可。同时删除这一行$user['password']='
在删除->asArray()
并注释$user['password']='代码>足够公平。。你能发布LoginForm::find()
方法吗?我在LoginForm
中没有find
方法,你正在调用$this::find()
这里:$user=$this::find()->where(['email'=>$email])->asArray()->one()
这可能需要是user::find()->where(['email'=>$email])->one()
您只需删除登录方法中的->asArray()
调用即可。同时删除这一行$user['password']='
在删除->asArray()
并注释$user['password']='代码>足够公平。。你能发布LoginForm::find()
方法吗?我在LoginForm
中没有find
方法,你正在调用$this::find()
这里:$user=$this::find()->where(['email'=>$email])->asArray()->one()
这可能需要是user::find()->where(['email'=>$email])->one()