رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
خطا در رمز عبور
#1
سلام استاد.
وقتی هش 123 یا هر عدد دیگه ای رو بصورت دستی در دیتابیس ذخیره میکنم موقع لاگین وارد میشم و خطایی پیش نمیاد.
وقتی در قسمت تغییر رمز عبور، رمز جدیدی وارد میکنم رمز رو بدون خطا ذخیره میکنه.
وقتی دوباره همان رمز رو وارد میکنم که رمز دیگه ای بدم، اینبار خطا میده که رمز قبلی صحیح نیست. موقع ورود هم این خطا رو میده.
کد تییر رمز اینه:
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
کد مدلتون رو بگذارین. احتمالاً رمز شما داره دوبار هش میشه.
پاسخ
تشکر شده توسط:
#3
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time',
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			if(!$this->_identity->authenticate())
				$this->addError('password','Incorrect username or 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 UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
}

هش پسورد کجا و توی کدوم متد صورت میگیره؟
کدها رو که بررسی میکردم به نظرم اومد هیچ جایی پسورد هش نمیشه.
پاسخ
تشکر شده توسط:
#4
نه این رو نمیگم. مدل Users منظورمه. همچنین کد اکشن تغییر رمز رو هم بگذارین. دقت کنید که اگه از هش خودکار که آموزش دادم استفاده میکنید، نباید موقع قراردادن پسورد توی مدل Users نسخه هش شده رو بگذارین (چون دوباره هش میشه) و باید همون پسورد خام رو بگذارین تا توسط متد afterValidate یا beforeSave تبدیل به هش بشه.
پاسخ
تشکر شده توسط:
#5
اکشن تغییر رمزو در پست اول گذاشتم.
در موقع وارد کردن پسورد از طریق فرم لاگین، پسورد خام مثل 123456 رو وارد میکنم.
مدل admin:
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;
	}
}
موقع ذخیره پسورد جدید، یا یه چیزی بهش اضافه میکنه و یا چیزی ازش کم میکنه.
یه پسورد هش شده هم ذخیره میکنه اما در فرم لاگین بهش خطا میده.
پاسخ
تشکر شده توسط:
#6
خوب پس مشکل توی کلاس UserIdentity هست که موقع لاگین خطا ایجاد میشه. کد اون رو هم بگذارین.
پاسخ
تشکر شده توسط:
#7
این کدی هست که خود Yii ایجاد کرده:
class UserIdentity extends CUserIdentity
{
	public function authenticate()
	{
		if($this->username==='demo' && $this->password==='demo')
			$this->errorCode=self::ERROR_NONE;
		else
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		return !$this->errorCode;
	}
}
به صورت زیر تغییرش دادم ولی بازم همون خطا رو میده:
class AdminIdentity extends CUserIdentity
{
	public function authenticate($userName, $passWord)
	{
		if($this->username===$userName && $this->password===$passWord)
			$this->errorCode=self::ERROR_NONE;
		else
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		return !$this->errorCode;
	}
}
پاسخ
تشکر شده توسط:
#8
(16-03-1394، 08:58 ق.ظ)ADMIN نوشته: خوب پس مشکل توی کلاس UserIdentity هست که موقع لاگین خطا ایجاد میشه. کد اون رو هم بگذارین.
class AdminIdentity extends CUserIdentity
{
	private $_id;
	private $_name;
	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;
	}

}
پاسخ
تشکر شده توسط:
#9
(16-03-1394، 08:58 ق.ظ)ADMIN نوشته: خوب پس مشکل توی کلاس UserIdentity هست که موقع لاگین خطا ایجاد میشه. کد اون رو هم بگذارین.
class AdminIdentity extends CUserIdentity
{
	private $_id;
	private $_name;
	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;
	}

}
پاسخ
تشکر شده توسط:
#10
خوب الان باید توی متد login از کلاس LoginForm از کلاس AdminIdentity استفاده کنید نه UserIdentity

اگه مشکل همچنان حل نشده بود، توی پیام خصوصی تیم ویور بدین تا ببینم مشکل چیه.
پاسخ
تشکر شده توسط:




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