【问题标题】:Allow user to change password Cakephp 2允许用户更改密码 Cakephp 2
【发布时间】:2012-11-17 22:46:47
【问题描述】:

我查看了各种方法以及有关此主题的各种问题,但没有运气。在我的例子中,控制器代码“出现”工作,并且消息闪烁“您的更改已保存”,但密码数据库字段未更改。有什么我遗漏的吗?

控制器代码

public function changepass($id = null) {
    $this->layout = 'profile_page';
    //$this->request->data['User']['id'] = $this->Session->read('Auth.User.id');

    $user = $this->User->find('first', array( 
        'conditions' => array('User.id' => $this->Auth->user('id'))
    ));     // 'User.id' => $id
    $this->set('user',$user);


    if ($this->request->is('post') || $this->request->is('put'))
    {   
        $this->User->saveField('password', AuthComponent::password($this->request->data['User']['newpass']));
        // $this->User->saveField('password', $this->data['User']['password']);
        // $this->data['User']['password']= $this->request->data['User']['newpass'];

        if ($this->User->save($this->request->data))
        {   
            $this->Session->setFlash(__('Your password has been changed!'));
            $this->redirect(array('controller'=>'articles','action'=>'index'));
        }
        else
        {
            $this->Session->setFlash(__('Whoops! Something went wrong... try again?'));
            $this->redirect(array('controller'=>'users','action'=>'changepass'));
        }
    }
    $this->request->data = $this->User->read(null, $id);
    unset($this->request->data['User']['password']); // tried commenting out
}

型号

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        if (isset($this->data[$this->alias]['newpass'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['newpass']);
        }
    }
    return true;
}

当然稍后我会进行现有密码检查,并确认新密码检查,但我需要让现有密码更新基本方法正常工作。

非常感谢您在此提供的任何启发,

我想我已经猜到了。首先,我的主要问题是——在我看来,我会把 echo $this->Form->create('User', array('action' => 'edit')); -- 当然将 action 更改为 'changepass'

新控制器代码:

    public function changepass ($id = null) {
    $this->layout = 'profile_page';
    $this->User->id = $id;
    if (!$this->User->exists()) {
        throw new NotFoundException(__('Invalid user'));
    }
            //debug($this->request->data);

    if ($this->request->is('post') || $this->request->is('put')) {
        $this->data['User']['password']= $this->request->data['User']['newpass'];
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('Your password changes have been saved'));
             $this->redirect(array('controller' => 'articles', 'action' => 'index'));
        } else {
            $this->Session->setFlash(__('The profile could not be saved. Please, try again.'));
         }
    } else {
                if ($this->Auth->user('id')!= $id) {
        $this->Session->setFlash('You are not allowed that operation!');
        $this->redirect(array('controller' => 'articles', 'action' => 'index'));
        }

        $this->request->data = $this->User->read(null, $id);  
        debug($this->request->data);
        unset($this->request->data['User']['password']);
    }
}

模型 - 根据 eboletaire 的建议整理

    public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
    }
    if (isset($this->data[$this->alias]['newpass'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['newpass']);
    }

return true;
}  

【问题讨论】:

  • 为什么要保存两次密码???首先用$this->User->saveField 保存它,然后用$this->User->save($this->request->data) 保存它
  • 刚刚尝试了各种不同的替代方案。如果我更改保存字段并改为 $this->data['User']['password']= $this->request->data['User']['newpass']; ,但该字段仍未更新。
  • 你的代码看起来很乱用户表)

标签: passwords cakephp-2.0


【解决方案1】:
if ($this->request->is('post') || $this->request->is('put')) {
    $this->data['User']['password']= $this->request->data['User']['newpass'];
    if ($this->User->save($this->request->data)) {

应该是

if ($this->request->is('post') || $this->request->is('put')) {
    $this->data['User']['id'] = $id;
    $this->data['User']['password']= $this->request->data['User']['newpass'];
    if ($this->User->save($this->data)) {

注意$this->data$this->request->data

【讨论】:

    【解决方案2】:

    首先,您要保存两次密码。删除/评论这一行:

    $this->User->saveField('password', AuthComponent::password($this->request->data['User']['newpass']));
    

    无论如何,我认为问题出在您的模型上。查看您的 beforeSave 方法。为什么要先用字段密码设置密码,然后用字段newpass?

    PD。清理你的代码我还看到第二个 if 应该在第一个之外。

    【讨论】:

    • 如 cmets 中所述,我已经编辑了该行,但没有效果:( beforeSave 方法用于新用户的字段密码..
    • 尽量让你的方法绝对干净(注释代码的其他部分)。评论模型上的 beforeSave 方法和控制器中的 saveField。然后尝试保存。密码存储正确吗? (当然,它应该以普通方式存储)。如果密码没有正确存储,那么在你的 $this->User->save 之前添加一个“debug($this->request->data)”。确保您正确接收到带有字段名的密码。确保字段名与数据库相同。
    • 结合了您的一些提示,并发现了我自己的错误,现在可以正常工作了。因此,我将其作为已接受的答案输入。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2016-04-07
    相关资源
    最近更新 更多