dongyilai4214 2013-05-17 10:51
浏览 33
已采纳

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

add in 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 only makes call to check the username and/or email fields to see if they exist or not (I've left them in the controller because I'd like to fix this first, I know I need to move them to the model).

confirmPass only compares the fields password and confirmPassword from the view, returning true or false.

beforeSave in 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;
}

Yesterday everything was working perfectly. All the values are still being saved to the correct tables, just no hashing on the password is being done (md5+salt). Does anyone have any ideas if this is a known or common problem with AuthComponent?

If I remove the isset I get from my model:

Notice (8): Trying to get property of non-object [APP/Plugin/Account/Model/User.php, line 65]

Notice (8): Trying to get property of non-object [APP/Plugin/Account/Model/User.php, line 65]

Notice (8): Indirect modification of overloaded property User::$request has no effect [APP/Plugin/Account/Model/User.php, line 65]

  • 写回答

1条回答 默认 最新

  • dqprf0976 2013-05-17 10:55
    关注

    Use this instead:

    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;
    }
    

    Reason: In models you just need $this->data, not $this->request->controller->data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?