Yii Framework Forum: Updating Password - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Updating Password Rate Topic: -----

#1 User is offline   Sler 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 17-July 12

Posted 14 October 2012 - 09:57 PM

Hello: I have this code for the form of updating my user:

<div class="form">

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

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'Old password'); ?>
			<?php echo $form->passwordField($model,'password',array('maxlength'=>100, 'value'=>"")); ?>
		<?php echo $form->error($model,'password'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'New password'); ?>	
		<?php echo $form->passwordField($model,'new_password',array('maxlength'=>100)); ?>
		<?php echo $form->error($model,'new_password'); ?>
	</div>
	<div>
		<?php echo $form->labelEx($model,'Confirm password'); ?>	
		<?php echo $form->passwordField($model,'repeat_password',array('maxlength'=>100)); ?>
		<?php echo $form->error($model,'repeat_password'); ?>
	</div>
		
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->


Which will display a old password, new password and confirm pass.
I would like to make a function which will hash the passwords like my beforeSave(), then compare the old password to my old one (which is hashed). If they matched. it will update to the new password. How can I do this?
0

#2 User is offline   Hieu Vo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 10-December 11

Posted 14 October 2012 - 11:34 PM

To make it simple you should use an ActiveRecord class, ex: User, after validate matching between old pass and new pass (manually or create new model class, ex: UpdatePassForm), set the new password and save it

$old_pass = $_POST['password'];
$new_pass = $_POST['new_password'];

$user = User::model()->findByPk($user_id);

if (md5($old_pass) === $user->password) {

   $user->password = md5($new_password);

   if ($user->save()) {
       // do something
   }
}
else {
   // do something
}

1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users