在添加到MySQL之前,CakePHP没有哈希密码

时间:2013-05-17 10:51:57

标签: php cakephp authentication

添加UsersController.php

public function add() {
    if ($this->request->is('POST')) {
        if ($this->confirmPass()) {
            //Want false
            if (!$this->validateSQL('username', 'add')) {
                $this->Session->write('User.username', strtolower('User.username'));
                //Want false
                if (!$this->validateSQL('email', 'add')) {
                    $this->Session->write('User.email', strtolower('User.email'));
                    $this->User->create();
                    if ($this->User->save($this->request->data)) {
                        $this->Session->setFlash(__('Your account has been created'), 'flash_success', array('class' => 'success'), 'success');
                        $this->redirect(ACCOUNT_LOG);
                    } else {
                        $this->Session->setFlash(__('Your account could not be created'), 'flash_failure', array('class' => 'failure'), 'failure');
                    }
                } else {
                    $this->Session->setFlash(__('E-mail already in use'), 'flash_failure', array('class' => 'failure'), 'failure');
                }
            } else {
                $this->Session->setFlash(__('Username already in use'), 'flash_failure', array('class' => 'failure'), 'failure');
            }
        }
    }
}

validateSQL仅调用检查用户名和/或电子邮件字段以查看它们是否存在(我已将它们留在控制器中,因为我想先修复它,我知道我需要将它们移动到模特)。

confirmPass仅比较视图中的字段password和confirmPassword,返回true或false。

在User.php中保存之前

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

昨天一切都运转良好。所有值仍然保存到正确的表中,只是没有对密码进行散列(md5 + salt)。如果这是AuthComponent的已知或常见问题,有没有人有任何想法?

如果我删除了我从模型中获取的isset:

注意(8):试图获取非对象的属性[APP / Plugin / Account / Model / User.php,第65行]

注意(8):试图获取非对象的属性[APP / Plugin / Account / Model / User.php,第65行]

注意(8):间接修改重载属性User :: $ request无效[APP / Plugin / Account / Model / User.php,第65行]

1 个答案:

答案 0 :(得分:0)

请改用:

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

<强>原因: 在模型中,您只需要$this->data,而不是$this->request->controller->data