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

为什么CakePHP身份验证组件没有散列我的密码?

  •  1
  • Eric  · 技术社区  · 17 年前

    我正在使用 CakePHP 1.2具有Auth和ACL组件。

    if ($this->data['User']['password'] !=
        $this->Auth->password($this->data['User']['confirm_password']))
    

    即使我提交了相同的值,这也是真的 password confirm_password . 我知道密码未被更改,因为当我删除对的调用时 Auth->password

    以下是我的看法:

    <?php
        echo $form->create('User', array('action' => 'register'));
    
        echo $form->input('email',
                          array('after' => $form->error(
                            'email_unique', 'This email is already registered.')));
        echo $form->input('password');
        echo $form->input('confirm_password', array('type' => 'password'));
        echo $form->end('Register');
    ?>
    

    function register(){
        if ($this->data) {
            if ($this->data['User']['password'] !=
                $this->Auth->password($this->data['User']['confirm_password'])) {
    
                $this->Session->setFlash(__('Password and Confirm Password must match.', true));
                $this->data['User']['password'] = '';
                $this->data['User']['confirm_password'] = '';
            }
            else{
                $this->User->create();
                if ($this->User->save($this->data)){
                    $this->redirect(array('action' => 'index'), null, true);
                }
                else {
                    $this->data['User']['password'] = '';
                    $this->data['User']['confirm_password'] = '';
                    $this->Session->setFlash(__('Some problem saving your information.', true));
                }
            }
        }
    }
    

    这是我的 appController 其中包括 Auth Acl 模块:

    class AppController extends Controller {
        var $components = array('Acl', 'Auth');
    
        function beforeFilter(){
            if (isset($this->Auth)) {
                $this->Auth->allow('display');
                $this->Auth->fields =
                  array(
                    'username' => 'email',
                    'password' => 'password');
                $this->Auth->authorize = 'actions';
            }
        }
    }
    

    我做错了什么?

    4 回复  |  直到 15 年前
        1
  •  2
  •   Eric    17 年前

    除非用户名包含提交的值,否则CakePHP不会散列密码。我正在用电子邮件替换用户名字段。但是,我通过设置Auth->字段数组。但是,我是在appController中而不是在userController中这样做的。因此,移动这条线:

    $this->Auth->fields = array('username' => 'email', 'password' => 'password');
    

    从appController到userController解决了这个问题。
    现在问题变成了“为什么我不能重置appController中的Auth->字段?”

        2
  •  2
  •   dr Hannibal Lecter    17 年前

    AppController::beforeFilter() 用你的 UsersController::beforeFilter() .

    parent::beforeFilter() 在函数的开头。

        3
  •  1
  •   Jason Plank Maksim Kondratyuk    14 年前

    function beforeSave() {
      if(isset($this->data[$this->alias]['password']))
        $this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
      return true;
    }
    

    beforeFilter() 到您的用户控制器:

    if(in_array($this->action, array('register'))) {
      $this->Auth->fields = array('username' => 'email', 'password' => 'wrongfield');
    }
    

    这意味着在注册过程中密码不会被散列(在注册表表单验证失败的情况下)。

        4
  •  0
  •   WalterJ89    17 年前

    hashPasswords ($data)
    

    看看这些页面。他们应该给你指出正确的方向。您还可以尝试在核心配置文件中更改调试级别。将其从0(生产)更改为3可以查看sql输出。可能会有帮助。

    AuthComponent-Methods

    Cakephp troubleshooting