代码之家  ›  专栏  ›  技术社区  ›  sipiatti

有没有更好的方法可以使用Auth在cakephp中更改用户密码?

  •  14
  • sipiatti  · 技术社区  · 16 年前

    class UsersController extends AppController {
    
        var $name = 'Users';
    
        function login() {
        }
    
        function logout() {
            $this->redirect($this->Auth->logout());
        }
    
        function changepassword() {
            $session=$this->Session->read();
            $id=$session['Auth']['User']['id'];
            $user=$this->User->find('first',array('conditions' => array('id' => $id)));
            $this->set('user',$user);
            if (!empty($this->data)) {
                if ($this->Auth->password($this->data['User']['password'])==$user['User']['password']) {
                    if ($this->data['User']['passwordn']==$this->data['User']['password2']) {
                    // Passwords match, continue processing
                    $data=$this->data;
                    $this->data=$user;
                    $this->data['User']['password']=$this->Auth->password($data['User']['passwordn']);
                    $this->User->id=$id;
                    $this->User->save($this->data);
                    $this->Session->setFlash('Password changed.');
                    $this->redirect(array('controller'=>'Toners','action' => 'index'));
                    } else {
                        $this->Session->setFlash('New passwords differ.');
                        }
                } else {
                    $this->Session->setFlash('Typed passwords did not match.');
                }
            }
        }
    }
    

    password是旧密码,passwordn是新密码,password2是重新键入的新密码。 有没有其他更酷的方法来做蛋糕?

    4 回复  |  直到 16 年前
        1
  •  30
  •   SnowTiger Mike    11 年前

    我看到您在控制器中验证和操作数据。在模型中这样做通常是更好的做法。几天前我实现了类似的功能。我的 change_password()

    # app/controllers/users_controller.php
    function change_password() {
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash('Password has been changed.');
                // call $this->redirect() here
            } else {
                $this->Session->setFlash('Password could not be changed.');
            }
        } else {
            $this->data = $this->User->findById($this->Auth->user('id'));
        }
    }
    

    下面是该方法使用的视图的精简版本:

    # app/views/users/change_password.ctp
    echo $this->Form->create('User');
    echo $this->Form->input('id');
    echo $this->Form->input('current_password');
    echo $this->Form->input('password1');
    echo $this->Form->input('password2');
    echo $this->Form->end('Submit');
    

    做一些有趣事情的代码在模型中。我将表单中的字段添加到 validate 财产和财产 custom validation methods

    # app/models/user.php
    var $validate = array(
        'current_password' => array(
            'rule' => 'checkCurrentPassword',
            'message' => '...'
        ),
        'password1' => array(
            'rule' => 'checkPasswordStrength',
            'message' => '...',
        ),
        'password2' => array(
            'rule' => 'passwordsMatch',
            'message' => '...',
        )
    );
    

    beforeSave() 我设置的模型的回调 password password1 准备要存储在数据库中的数据。

        2
  •  22
  •   azam    14 年前

    Mike提供的解决方案很好,但是他忽略了“checkCurrentPassword”功能。下面是一个可以放置在模型中的函数示例:

    # app/models/user.php
    public function checkCurrentPassword($data) {
        $this->id = AuthComponent::user('id');
        $password = $this->field('password');
        return(AuthComponent::password($data['current_password']) == $password);
    }
    

    此解决方案从Auth组件获取当前用户ID,并更改模型以指向该特定用户。然后将表单上输入的当前密码的哈希值与为该用户存储的哈希值进行比较。

    此外,您可以使用beforeSave函数对新密码进行哈希运算:

    # app/models/user.php
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password1'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password1']);
        }       
        return true;
    }
    
        3
  •  0
  •   jvperrin    12 年前
        4
  •  0
  •   Dinesh Rawat    12 年前

    步骤1)

    $password = $this->Auth->password($this->data['User']['password']); // It will generate the hashed password using the cakephp's Auth component.
    

    if($this->User->update(array('User.password'=>$password), array('User.id'=>$this->Auth->User('id')))) {
         echo $this->Session->setFlash('Password changed successfully.', 'default',   
         array('class'=>'successMsg'));
    }