Note that there are some explanatory texts on larger screens.

plurals
  1. POAllow user to change password Cakephp 2
    text
    copied!<p>I've looked over various approaches, and various questions published on this subject, but with no luck. In my case, the controller code "appears" to work, and the message flashes up "Your changes have been saved", but the password database field is unchanged. Is there something I am missing?</p> <p>Controller code</p> <pre><code>public function changepass($id = null) { $this-&gt;layout = 'profile_page'; //$this-&gt;request-&gt;data['User']['id'] = $this-&gt;Session-&gt;read('Auth.User.id'); $user = $this-&gt;User-&gt;find('first', array( 'conditions' =&gt; array('User.id' =&gt; $this-&gt;Auth-&gt;user('id')) )); // 'User.id' =&gt; $id $this-&gt;set('user',$user); if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { $this-&gt;User-&gt;saveField('password', AuthComponent::password($this-&gt;request-&gt;data['User']['newpass'])); // $this-&gt;User-&gt;saveField('password', $this-&gt;data['User']['password']); // $this-&gt;data['User']['password']= $this-&gt;request-&gt;data['User']['newpass']; if ($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('Your password has been changed!')); $this-&gt;redirect(array('controller'=&gt;'articles','action'=&gt;'index')); } else { $this-&gt;Session-&gt;setFlash(__('Whoops! Something went wrong... try again?')); $this-&gt;redirect(array('controller'=&gt;'users','action'=&gt;'changepass')); } } $this-&gt;request-&gt;data = $this-&gt;User-&gt;read(null, $id); unset($this-&gt;request-&gt;data['User']['password']); // tried commenting out } </code></pre> <p>Model</p> <pre><code>public function beforeSave($options = array()) { if (isset($this-&gt;data[$this-&gt;alias]['password'])) { $this-&gt;data[$this-&gt;alias]['password'] = AuthComponent::password($this-&gt;data[$this-&gt;alias]['password']); if (isset($this-&gt;data[$this-&gt;alias]['newpass'])) { $this-&gt;data[$this-&gt;alias]['password'] = AuthComponent::password($this-&gt;data[$this-&gt;alias]['newpass']); } } return true; } </code></pre> <p>Of course later I'd put in existing password check, and confirm new password check, but i need to get the existing password update basic approach working. </p> <h2>Many thanks in advance for any light you can shed on this,</h2> <p>I think I've sussed this. First, major bloop on my part - in my view I'd put echo $this->Form->create('User', array('action' => 'edit')); -- of course change action to 'changepass'</p> <p>New Controller code:</p> <pre><code> public function changepass ($id = null) { $this-&gt;layout = 'profile_page'; $this-&gt;User-&gt;id = $id; if (!$this-&gt;User-&gt;exists()) { throw new NotFoundException(__('Invalid user')); } //debug($this-&gt;request-&gt;data); if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { $this-&gt;data['User']['password']= $this-&gt;request-&gt;data['User']['newpass']; if ($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash(__('Your password changes have been saved')); $this-&gt;redirect(array('controller' =&gt; 'articles', 'action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash(__('The profile could not be saved. Please, try again.')); } } else { if ($this-&gt;Auth-&gt;user('id')!= $id) { $this-&gt;Session-&gt;setFlash('You are not allowed that operation!'); $this-&gt;redirect(array('controller' =&gt; 'articles', 'action' =&gt; 'index')); } $this-&gt;request-&gt;data = $this-&gt;User-&gt;read(null, $id); debug($this-&gt;request-&gt;data); unset($this-&gt;request-&gt;data['User']['password']); } } </code></pre> <p>Model - tidied up as per advice from eboletaire</p> <pre><code> public function beforeSave($options = array()) { if (isset($this-&gt;data[$this-&gt;alias]['password'])) { $this-&gt;data[$this-&gt;alias]['password'] = AuthComponent::password($this-&gt;data[$this-&gt;alias]['password']); } if (isset($this-&gt;data[$this-&gt;alias]['newpass'])) { $this-&gt;data[$this-&gt;alias]['password'] = AuthComponent::password($this-&gt;data[$this-&gt;alias]['newpass']); } return true; } </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