I have this example proyect, I try to use generatePasswordHash,validatePassword but I have no luck I read Incorrect username or password.. the problem is validatepassword always return false, I test pass input and pass stored are used
<?php
namespace app\models;
use Yii;
use yii\web\IdentityInterface;
/**
-
This is the model class for table “usuarios”.
-
@property int $id
-
@property string $nombre_usuario
-
@property int $rol
-
@property int $estado
-
@property string $pass
-
@property string $auth_key
-
@property string $access_token
/
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
/*- {@inheritdoc}
*/
public static function tableName()
{
return ‘usuarios’;
}
/**
- {@inheritdoc}
*/
public function rules()
{
return [
[[‘nombre_usuario’, ‘rol’, ‘estado’, ‘pass’, ‘auth_key’, ‘access_token’], ‘required’],
[[‘rol’, ‘estado’], ‘integer’],
[[‘nombre_usuario’], ‘string’, ‘max’ => 55],
[[‘pass’, ‘auth_key’, ‘access_token’], ‘string’, ‘max’ => 255],
];
}
/**
- {@inheritdoc}
*/
public function attributeLabels()
{
return [
‘id’ => ‘ID’,
‘nombre_usuario’ => ‘Nombre Usuario’,
‘rol’ => ‘Rol’,
‘estado’ => ‘Estado’,
‘pass’ => ‘Pass’,
‘auth_key’ => ‘Auth Key’,
‘access_token’ => ‘Access Token’,
];
}
/**
- {@inheritdoc}
*/
public static function findIdentity($id)
{
return self::findOne($id);
}
/**
- {@inheritdoc}
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return self::find()->where([“access_token”=>$token])->one();
}
/**
- Finds user by username
- @param string $username
-
@return static|null
*/
public static function findByUsername($username)
{
return self::findOne([“nombre_usuario”=>$username]);
}
/**
- {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
- {@inheritdoc}
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
- {@inheritdoc}
*/
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
/**
- Validates password
- @param string $password password to validate
-
@return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->pass);
}
}
- {@inheritdoc}
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.');
}*/
if (!$user) {
$this->addError($attribute, 'Incorrect username.');
} else {
if (!$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect 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;
}
}
<?php
/* @var $this yii\web\View /
/ @var $form yii\bootstrap\ActiveForm /
/ @var $model app\models\LoginForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->title = ‘Login’;
$this->params[‘breadcrumbs’][] = $this->title;
?>
<?= Html::encode($this->title) ?>
<p>Please fill out the following fields to login:</p>
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'rememberMe')->checkbox([
'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
]) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<div class="col-lg-offset-1" style="color:#999;">
You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>
To modify the username/password, please check out the code <code>app\models\User::$users</code>.
</div>
DB