Hello,
I’ve been having a problem verifying my password for days.
The strange thing that only happens to me when I change a password.
This code, Model User, Controller User, and module autenticator.
class UsersController extends Controller
{
public function actionUpdate($usuario)
{
$model = $this->loadModel($usuario);
if (isset($_POST['users'])) {
$model->attributes = $_POST['users'];
$model->scenario = 'update';
if ($model->validate()) {
if ($model->saveModel($model)) {
$this->redirect(array('view', 'usuario' => $model->Usuario));
}
}
}
$this->render('update', array(
'model' => $model,
));
}
}
class users extends CActiveRecord
{
public function rules()
{
return array(..
array('password, repeat_password', 'required', 'on'=>'insert'),
array('password, repeat_password', 'length', 'min'=>6, 'max'=>60),
array('password', 'compare', 'compareAttribute'=>'repeat_password'),
);
}
public function beforeSave()
{
// in this case, we will use the old hashed password.
if(empty($this->password) && empty($this->repeat_password) && !empty($this->initialPassword))
$this->password=$this->repeat_password=$this->initialPassword;
return parent::beforeSave();
}
public function afterFind()
{
//reset the password to null because we don't want the hash to be shown.
$this->initialPassword = $this->password;
$this->password = null;
parent::afterFind();
}
public function saveModel($data = array())
{
if (!Helpers::isEmptyString($data['password']) && !Helpers::isEmptyString($data['repeat_password'])) {
if ($data['password'] === $data['repeat_password']) {
$data['password'] = CPasswordHelper::hashPassword($data['password']);
$data['repeat_password'] = $data['password'];
}
}
$this->attributes = $data;
if (!$this->save()){
return CHtml::errorSummary($this);
}
return true;
}
}
class UserIdentity extends CUserIdentity
{
public function authenticate()
{
$user = users::model()->findByAttributes(array('Usuario' => $this->username, ));
if ($user === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
// } else if ($user->password !== $this->password) {
}else if (!CPasswordHelper::verifyPassword($this->password,$user->initialPassword)){
$this->errorCode = self::ERROR_PASSWORD_INVALID;
} else if ($user->users_types == null) {
$this->errorCode = self::ERROR_UNKNOWN_IDENTITY;
} else { // Okay!
$this->errorCode = self::ERROR_NONE;
$this->setUser($user);
$this->_id=$user->Usuario;
}
unset($user);
return !$this->errorCode;
}
}