[英]Yii2 framework cannot login using a custom database
新手和学习yii2。 我有一个自定义的用户数据库,希望用作我的登录凭证,幸运的是,我能够查询它,但是在我的公共静态函数validatePassword($ password)上,当不在对象上下文中时,我得到了using $ this。
这是我的代码
public static function findByUsername($username)
{
$user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();
return new static($user);
}
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public static function validatePassword($password)
{
return $this->password === $password;
}
所有脚本都是使用gii模块生成的,所以我不知道为什么会产生此错误,而且在观看教程时,我会复制其代码,但最终无法正常工作。 请帮忙,谢谢。
根据要求,这里是DbUser.php代码
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "{{%RBAC_USERS}}".
*
* @property string $USR_UID
* @property string $USR_USERNAME
* @property string $USR_PASSWORD
* @property string $USR_FIRSTNAME
* @property string $USR_LASTNAME
* @property string $USR_EMAIL
* @property string $USR_DUE_DATE
* @property string $USR_CREATE_DATE
* @property string $USR_UPDATE_DATE
* @property int $USR_STATUS
* @property string $USR_AUTH_TYPE
* @property string $UID_AUTH_SOURCE
* @property string $USR_AUTH_USER_DN
* @property string $USR_AUTH_SUPERVISOR_DN
*/
class DbUsers extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%RBAC_USERS}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['USR_UID', 'USR_DUE_DATE'], 'required'],
[['USR_DUE_DATE', 'USR_CREATE_DATE', 'USR_UPDATE_DATE'], 'safe'],
[['USR_STATUS'], 'integer'],
[['USR_UID', 'USR_AUTH_TYPE', 'UID_AUTH_SOURCE'], 'string', 'max' => 32],
[['USR_USERNAME', 'USR_EMAIL'], 'string', 'max' => 100],
[['USR_PASSWORD'], 'string', 'max' => 128],
[['USR_FIRSTNAME', 'USR_LASTNAME'], 'string', 'max' => 50],
[['USR_AUTH_USER_DN', 'USR_AUTH_SUPERVISOR_DN'], 'string', 'max' => 255],
[['USR_UID'], 'unique'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'USR_UID' => Yii::t('app', 'Uid'),
'USR_USERNAME' => Yii::t('app', 'Username'),
'USR_PASSWORD' => Yii::t('app', 'Password'),
];
}
/**
* {@inheritdoc}
*/
public static function findIdentity($id)
{
throw new NotSupportedException();
}
/**
* {@inheritdoc}
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException();
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public function getAuthKey()
{
throw new NotSupportedException();
}
/**
* {@inheritdoc}
*/
public function validateAuthKey($authKey)
{
throw new NotSupportedException();
}
public static function findByUsername($username, $password)
{
$user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();
return $user;
}
public function getId()
{
return $this->getPrimaryKey();
}
/**
* {@inheritdoc}
*/
public static function validatePassword($password)
{
return $this->password === $password;
}
}
这是我的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 = false;
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 = DbUsers::findByUsername($this->username, $this->password);
}
return $this->_user;
}
}
在静态函数中,不能使用$ this。 使validatePassword()函数为非静态。
您不能将$this
与静态函数一起使用,更改为
public function validatePassword($password)
{
return $this->password === $password;
}
请更新您的问题,添加一个DbUsers模型代码。 在哪里执行方法validatePassword()
? 当然,您不能在静态方法中使用$ this,而使该方法不是静态的
我回答我自己的问题。 我在查找中添加了以下代码
public static function findByUsername($username, $password)
{
$user = self::find()->where(['USR_USERNAME' => $username, 'USR_STATUS' => '1'])->one();
if (hash('sha256', $password) == $user->USR_PASSWORD)
{
return new static($user);
}
}
现在,这将验证是否以sha256加密方式将密码与数据库匹配,然后在我的validatePassword方法上将其修改为simple,始终返回true,因为在LoginForm上,他们总是先搜索用户名,然后检查密码验证,因此如果用户名/密码错误,它将立即验证该用户不存在
public static function validatePassword($password)
{
return true;
}
我发现yii2很难,因为缺少文档非常难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.