User Profile Password Change

Hi All,

I am having a user table, when the user login with his id, his profile is diplayed in read only mode (view page is rendered).

I want that some of the fields to be updated by him. or if the user want to change his password then how can he do it.

Thanks

create an action ChangePassword and in the view have a change password field straight forward

EDIT:

have a link on profile page to that actoin

Hi Thanks for your prompt reply…

I have done as per your suggestion but i am getting "Error 400 Your request is invalid".

Find below the steps and code

My model controller




public function actionChangepassword($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Customer']))

		{

			$model->attributes=$_POST['Customer'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('Changepassword',array(

			'model'=>$model,

		));

	}



Allowed the changepassword for authenticated users.




array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update', 'changepassword'),

				'users'=>array('@'),



changepassword.php




$this->breadcrumbs=array(

	'Customer'=>array('index'),

	$model->name=>array('view','id'=>$model->id),

	'Changepassword',

);





<h1>Update Customer <?php echo $model->id; ?></h1>


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>



ends the url (link) to [controller]/changepassword/id/{number} ?

if it is ok then check step by step in which block of code occurs the problem and give more details

Dear Friend

I hope the following is helpful




public function actionChangeProfile($id)

        {

        $model=$this->loadModel($id);

        $oldPassword=$model->password;//Capture the old password.

        $model->password='';//Make this empty.else the hashed password appears as string of dots in form field.

                

                if(isset($_POST['User']))

                {

                        $model->attributes=$_POST['User'];


                        if($model->password=="")

                            $model->password=$oldPassword;

                        else $model->password=md5($model->password);//customize the encrypting logic in your own way.You can put some additional salt.


                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }


                $this->render('update',array(

                        'model'=>$model,

                ));

        }






create a link in your view.




echo chtml::link(CHtml::encode('Change Your Profile'),array('changeProfile','id'=>$model->id));



Make permission in Controller




array('allow', // allow authenticated user to perform 'create' and 'update' actions

                                'actions'=>array('create','update', 'changeProfile'),

                                'users'=>array('@'),



Hi,

Thanks

I am now able to change the password but i am seeing the existing password in hash format, so kindly let me know how to remove the existing hash of the password and keep the field blank.

I am now redirecting the user to the view the profile with the below code from sitecontroller in actionlogin. I want to create a menu call profile and on clicking that link his profile should be shown.




if($model->validate() && $model->login())  

			$this->redirect('/customerwebsite/index.php/customer/view/id/'.Yii::app()->user->Id);



Thanks

Hi seenivasan,

oops!

posted just at the same time.

Thanks for the solution,

I have resolved the problem for dotted password.

awaiting for the solution on below

I am now redirecting the user to the view the profile with the below code from sitecontroller in actionlogin. I want to create a menu call profile and on clicking that link his profile should be shown


if($model->validate() && $model->login())  

			$this->redirect('/customerwebsite/index.php/customer/view/id/'.Yii::app()->user->Id);

What did you mean? If you want to add a menu link on customer/actionview/id you could add it into view file inside of

$this->menu = array(

array(‘label’ => ‘Profile’, ‘url’ => array(‘customer/view’, ‘id’ => $model->id)),

)

just double check if you adding a profile link to your main menu you might not have access to the $model object

so you can something as following




<?php

  ..... // your other links


  array("View Profile", "url"=>array("customer/view", 'id'=>Yii::app()->user->id)),


  .... 

?>

Hi alirz/konapaz,

Thanks both of you. I already used your suggestions for sidebar link.

i think i should have been more clear on my requirement.

on the index page there are four top menu link

Home

About

Contact

Login

I changed the contact to profile with below code in main.php




<div id="mainmenu">

		<?php $this->widget('zii.widgets.CMenu',array(

			'items'=>array(

				array('label'=>'Home', 'url'=>array('/site/index'),'visible'=>!Yii::app()->user->isGuest),

				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),

				array('label'=>'Profile', 'url'=>array('/site/profile'),'visible'=>!Yii::app()->user->isGuest),

				array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),



now i wanted that when a user clicks on profile his profile page should be displayed.

for this i have created a profile page in views/site and added below code in this file.




<?php

$this->redirect('/customerwebsite/index.php/customer/view/id/'.Yii::app()->user->Id);

?>



now if someone clicks on this link his profile is displayed.

I am still learning YII so correct me if there is other method to achive this.

you dont have to do a redirect you can just simply add the link to you customer/view/id action since you profile only will be displayed when user is logged in


 array('label'=>'Profile', 'url'=>array('site/profile'),'visible'=>!Yii::app()->user->isGuest),



to


 array('label'=>'Profile', 'url'=>array('/customerwebsite/index.php/customer/view/id/'.Yii::app()->user->Id),'visible'=>!Yii::app()->user->isGuest),

Hi alirz,

Thanks for the shortcut.

it works fine.

Now i am concern with the url it shows the id of the user at the end “/customerwebsite/index.php/customer/view/id/37” if the user changes id from 37 to 38 he goes in another user’s profile. is there any way to avoid this, also i want to show “/customerwebsite/index.php/profile” in the url.

I tried using url manager but not succeded. find below the code




'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

			'profile'=>array('/customer/view/id/'),



you can use the name of the user to instead of id Or alternatively append something a random number or somthing

regards to your url change the it to




'urlManager'=>array(

                        'urlFormat'=>'path',

                        'rules'=>array(

                        'profile/id'=>'customer/view',