رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
چرا رمز عبور بصورت نادرست تغییر داده میشود؟
#1
سلام استاد
در بخش تغییر رمز، رمز عبور بدون هیچ مشکلی ذخیره میشه اما موقع لاگین خطای اشتباه بودن رمز عبور رو میاره. در واقع رمز بصورت نادرست تغییر میکنه.
کدهایی که نوشتم اینا هستن:
/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity= new AdminIdentity($this->username,$this->password);
			if(!$this->_identity->authenticate()){
				$this->addError('password','نام کاربری یا رمز عبور اشتباه است.');
			}
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new AdminIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===AdminIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else{
			return false;
		}
	}

          /**
	 *
	 * @param type $password
	 * @return type true when $password==$this->password
	 */
	public function validatePassword($password){
		return CPasswordHelper::verifyPassword($password, $this->password);
	}
	
	public function afterValidate(){
		parent:: afterValidate();
		if($this->password=''){
			$this->password= $this->_oldpass;
		}
		if(!$this->hasErrors() && $this->_oldpass!=$this->password){
			$this->password= CPasswordHelper::hashPassword($this->password);
		}
	}

	public function afterFind() {
		$this->_oldpass= $this->password;
	}

public function actionChangePass(){
		$model = new ChangePassForm;
        if(isset($_POST['ChangePassForm'])) {
            $model->attributes = $_POST['ChangePassForm'];
            $model->validate();
            if(!$model->hasErrors()) {
                $admin = Admins::model()->findByPk(Yii::app()->user->id);
                if(!$admin->validatePassword($model->oldpass)) {
                    $model->addError('oldpass','رمز قبلی صحیح نیست.');
                }
                else {
                    $admin->password = $model->newpass;
                    if($admin->save()) {
                        Yii::app()->user->setFlash('message','رمز کاربر با موفقیت تغییر کرد.');
                        $this->refresh();
                    }
                    else {
                        Yii::app()->user->setFlash('message','خطایی در زمان تغییر رمز رخ داد.');
                    }
                }
            }
        }
        $this->render('changepass',compact('model'));
	}

متشکرم.
پاسخ
تشکر شده توسط:
#2
کد مدل User و کد UserIdentity رو بگذارین.
پاسخ
تشکر شده توسط:
#3
در پروژه گالری که مدل User نداریم. فک کنم منظورتون همین مدل Admins باشه:


<?php

/**
 * This is the model class for table "admin".
 *
 * The followings are the available columns in table 'admin':
 * @property integer $id
 * @property string $name
 * @property string $username
 * @property string $password
 * @property integer $confirm
 */
class Admins extends CActiveRecord
{
 private $_oldpass;
 /**
 * @return string the associated database table name
 */
 public function tableName()
 {
 return 'admin';
 }

 /**
 * @return array validation rules for model attributes.
 */
 public function rules()
 {
 // NOTE: you should only define rules for those attributes that
 // will receive user inputs.
 return array(
 array('name, username, password, confirm', 'required'),
 array('confirm', 'numerical', 'integerOnly'=>true),
 array('name, username, password', 'length', 'max'=>255),
 // The following rule is used by search().
 // @todo Please remove those attributes that should not be searched.
 array('id, name, username, password, confirm', 'safe', 'on'=>'search'),
 );
 }

 /**
 * @return array relational rules.
 */
 public function relations()
 {
 // NOTE: you may need to adjust the relation name and the related
 // class name for the relations automatically generated below.
 return array(
 );
 }

 /**
 * @return array customized attribute labels (name=>label)
 */
 public function attributeLabels()
 {
 return array(
 'id' => 'ردیف',
 'name' => 'نام',
 'username' => 'نام کاربری',
 'password' => 'رمز عبور',
 'confirm' => 'فعال',
 );
 }

 /**
 * Retrieves a list of models based on the current search/filter conditions.
 *
 * Typical usecase:
 * - Initialize the model fields with values from filter form.
 * - Execute this method to get CActiveDataProvider instance which will filter
 * models according to data in model fields.
 * - Pass data provider to CGridView, CListView or any similar widget.
 *
 * @return CActiveDataProvider the data provider that can return the models
 * based on the search/filter conditions.
 */
 public function search()
 {
 // @todo Please modify the following code to remove attributes that should not be searched.

 $criteria=new CDbCriteria;

 $criteria->compare('id',$this->id);
 $criteria->compare('name',$this->name,true);
 $criteria->compare('username',$this->username,true);
 $criteria->compare('password',$this->password,true);
 $criteria->compare('confirm',$this->confirm);

 return new CActiveDataProvider($this, array(
 'criteria'=>$criteria,
 ));
 }

 /**
 * Returns the static model of the specified AR class.
 * Please note that you should have this exact method in all your CActiveRecord descendants!
 * @param string $className active record class name.
 * @return Admins the static model class
 */
 public static function model($className=__CLASS__)
 {
 return parent::model($className);
 }

 /**
 *
 * @param type $password
 * @return type true when $password==$this->password
 */
 public function validatePassword($password){
 return CPasswordHelper::verifyPassword($password, $this->password);
 }
 
 public function afterValidate(){
 parent:: afterValidate();
 if($this->password=''){
 $this->password= $this->_oldpass;
 }
 if(!$this->hasErrors() && $this->_oldpass!=$this->password){
 $this->password= CPasswordHelper::hashPassword($this->password);
 }
 }

 public function afterFind() {
 $this->_oldpass= $this->password;
 }
}

class AdminIdentity extends CUserIdentity
{
	private $_id;
	private $_name;
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		$admin= Admins::model()->find('LOWER(username)=:username', array(':username'=>  strtolower($this->username)));
		if(!$admin){
			$this->errorCode= self::ERROR_USERNAME_INVALID;
		}
		elseif(!$admin->validatePassword($this->password)){
			$this->errorCode= self::ERROR_PASSWORD_INVALID;
		}
		else{
			$this->errorCode= self::ERROR_NONE;
			$this->_id= $admin->id;
			$this->_name= $admin->name;
		}
		return !$this->errorCode;
	}

	public function getId() {
		return $this->_id;
	}

	public function getName() {
		return $this->_name;
	}

}
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان