【问题标题】:CakePHP not hashing password before adding to MySQLCakePHP 在添加到 MySQL 之前没有散列密码
【发布时间】:2013-05-17 10:51:56
【问题描述】:

添加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 仅比较视图中的字段密码和确认密码,返回真或假。

在 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, line 65]

【问题讨论】:

  • 你的$this->validateSQL() 部分与所有那些奇怪的会话事情正在发生,看起来不是很cakish - 或干净。这就是模型中的 save() 和验证通常的用途。
  • 我注意到我会将这些迁移到模型中。而且由于我将其用作插件,并且 save() 并没有完全按照我的意愿做,而且我是 Cake 的新手,所以我首先用 PHP 编写了它,然后将迁移到完整的 cake 语法,所有无需修改基础库。
  • 这个 cakephp 是 2.0 还是 3.0?
  • @user1767754 2; 3 直到 2015 年 3 月才向公众开放。这个问题于 2013 年发布。

标签: php cakephp authentication


【解决方案1】:

改用这个:

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

【讨论】:

  • 谢谢。我忽略了那部分,因为我认为我没有改变任何东西。可能发生在我将部分复制粘贴到彼此之上时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 2018-03-26
  • 2013-03-24
  • 1970-01-01
  • 2011-05-03
  • 2021-04-08
  • 1970-01-01
相关资源
最近更新 更多