رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
چرا رمز عبور بصورت نادرست تغییر داده میشود؟
#1
سلام استاد
در بخش تغییر رمز، رمز عبور بدون هیچ مشکلی ذخیره میشه اما موقع لاگین خطای اشتباه بودن رمز عبور رو میاره. در واقع رمز بصورت نادرست تغییر میکنه.
کدهایی که نوشتم اینا هستن:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
     * 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 باشه:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?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;
    }
 
}
پاسخ
تشکر شده توسط:




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