Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't think this is the correct way to do the password reset. Since the same $password field is used for "Current Password" and "New Password", when the form is posted you'll only have access to "New Password" as it will override the "Current Password". Hence you'll not be able to verify if the user's current password is valid. So the best way forward is to create a separate model called "ChangePassword.php" in your models folder with the following code,</p> <p>ChangePassword.php</p> <p> <pre><code>/** * Password change class. */ class ChangePassword extends CFormModel { public $password; public $new_password; public $password_repeat; public $username; public function rules(){ return array( array('username, password, new_password, password_repeat', 'required'), // Required fields array('password_repeat','compare','compareAttribute'=&gt;'password', 'message'=&gt; 'Passwords don\'t match!'), // Validator to check if the new password and password repeat match. array('password', 'isValid'), // Custom validator to check if the current password is valid. ); } public function isValid($attribute, $params){ if(!$this-&gt;hasErrors()){ if($user = Users::model()-&gt;findByAttributes(array('username'=&gt;$this-&gt;username))){ // Fetch the user model using username. if($user-&gt;password !== Yii::app()-&gt;utils-&gt;hash($this-&gt;old_password)){ // Check if the current password is valid $this-&gt;addError('password', 'Current Password is invalid!'); } } else $this-&gt;addError('username', 'User does not exist!'); } } public function attributeLabels() { return array( 'username'=&gt;'Username', 'password'=&gt;'Current password', 'new_password' =&gt; 'New password', 'password_repeat' =&gt; 'Confirm password' ); } } </code></pre> <p>Modify your controller code as,</p> <pre><code>public function actionUpdate($id) { $model = new ChangePassword; // set the parameters for the bizRule $params = array('GroupzSupport'=&gt;$model); // now check the bizrule for this user if (!Yii::app()-&gt;user-&gt;checkAccess('updateSelf', $params) &amp;&amp; !Yii::app()-&gt;user-&gt;checkAccess('admin')) { throw new CHttpException(403, 'You are not authorized to perform this action'); } else { if(isset($_POST['ChangePassword'])) { $model-&gt;attributes=$_POST['ChangePassword']; if($model-&gt;validate()){ // If all the information entered were correct $user = Users::model()-&gt;findByAttributes(array('username'=&gt;$model-&gt;username)); $user-&gt;password = hashPasswordFunction($model-&gt;password); // Call the function to hash your password which in most of the cases will be md5($model-&gt;password) $user-&gt;save(); } $this-&gt;render('update',array( 'model'=&gt;$model, )); } } </code></pre> <p>Now your view file will change to,</p> <pre><code>&lt;div class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'username'); echo $form-&gt;textField($model,'username',array('size'=&gt;45,'maxlength'=&gt;150)); echo $form-&gt;error($model,'username'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'Current password'); echo $form-&gt;textField($model,'password',array('size'=&gt;45,'maxlength'=&gt;150)); echo $form-&gt;error($model,'password'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt;&lt;?php echo $form-&gt;labelEx($model,'New password'); echo $form-&gt;passwordField($model,'new_password',array('size'=&gt;45,'maxlength'=&gt;150)); echo $form-&gt;error($model,'new_password'); ?&gt; &lt;/div&gt; &lt;div class="row"&gt; &lt;?php echo $form-&gt;label($model,'password_repeat'); ?&gt; &lt;?php echo $form-&gt;passwordField($model,'password_repeat',array('size'=&gt;45,'maxlength'=&gt;150)); ?&gt; &lt;?php echo $form-&gt;error($model,'password_repeat'); ?&gt; &lt;/div&gt; &lt;div class="row buttons"&gt;&lt;?php echo CHtml::submitButton('Reset Your Password'); ?&gt;&lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload