[Yii Framework] Update Password With CPasswordHelper

I post this item for answer this question here :https://sabitlabscode.wordpress.com/2013/08/12/yii-framework-use-cpasswordhelper-for-authentication/#comment-2141. So how to update password with CPasswordHelper?

We need to take an action to update password, so create it in controller like this :

 public function actionChangepassword()
    {
        $data=$this->loadModel(Yii::app()->user->id);//get current user that active now

        if(isset($_POST['old'],$_POST['baru1'],$_POST['baru2'])) // if user post to change password
        {
            if($_POST['baru1']!==$_POST['baru2']) // check if it have same password for validation?
            {
                $data->addError('username','Your New Password Not Match'); // if not same, show error
            }
            else // if same, next
            {
                if(CPasswordHelper::verifyPassword($_POST['old'], $data->pass)) // check the old password that user input same with old password?
                {
                    $dua=$_POST['baru1'];
                    $data->pass=CPasswordHelper::hashPassword($_POST['baru1']);// encryp that
                    if($data->save()) // save to tabel
                    {
                        $this->redirect(array('/site'));    
                    }
                }
                else //if password not match with old password, show error
                {
                    $data->addError('username','Wrong Password');
                }
            }
        }

        $this->render('cp',array( //call "cp" view
            'data'=>$data,
        ));
    }

    // function to get a user
    public function loadModel($id)
    {
        $model=User::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

After that, we need to create new file for view with name “cp.php” (we call “cp.php” in render, so the name must be match) and insert this code :

 <?php
/* @var $this UserController */
/* @var $model User */

$this->breadcrumbs=array(
    'Change Password',
);

?>

<h1>Change Password</h1>

<div>
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'user-form',
    'enableAjaxValidation'=>true,
)); ?>

    <?php echo $form->errorSummary($data); ?>
    <table>
        <tr>
            <td><?php echo 'Your Current Password :'; ?></td>
            <td>
                <?php echo CHtml::passwordField('old','',array('size'=>50,'maxlength'=>50)); ?>
            </td>
        </tr>
        <tr>
            <td><?php echo 'New Password :'; ?></td>
            <td>
                <?php echo CHtml::passwordField('baru1','',array('size'=>50,'maxlength'=>50)); ?>
            </td>
        </tr>
        <tr>
            <td><?php echo 'Confirmation Your New Password :'; ?></td>
            <td>
                <?php echo CHtml::passwordField('baru2','',array('size'=>50,'maxlength'=>50)); ?>
            </td>
        </tr>
    </table>

    <div>
        <?php echo CHtml::submitButton('Change Password'); ?>
    </div>

<?php $this->endWidget(); ?>
</div><!-- form -->

Finish… hope it help..
happy coding… 😀

Advertisements

Solve Sql Safe Update Mode in Mysql Workbench

Just for my note when i use mysql workbench for monitor my database. When we want to delete or update file, it will return some error result that say

Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.

To fix this, you must set sql update safe first with this code :

SET SQL_SAFE_UPDATES=0;
delete from `blablabla..`

ok, finish.. hope it help…
happy coding..

%d bloggers like this: