【问题标题】:new password not save in database aftter change password cakephp更改密码后新密码不保存在数据库中 cakephp
【发布时间】:2014-10-08 07:34:09
【问题描述】:

对不起,请问,更改密码忘记密码时,如果我的 beforeSave 函数是这样的,新密码不会存储在数据库中:

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

但是如果 BeforeSave 的功能变成这样

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

新密码的值是成功保存到数据库,但是当用户做编辑功能和密码留空时,数据库中的密码有两次散列 请帮助我,谢谢之前

哦,是的,这是我的 changePassword 功能:

public function account(){
    if(!$this->Session->check('Auth.User')){
        $this->Session->setFlash(__('You must be logged in to view this page.'));
        return $this->redirect(array('action' => 'login'));
    }
    //set user's ID in  model which is needed for validation
    $this->User->id = $this->Auth->user('id');

    //load the user (avoid populating $this->data)
    $current_user = $this->User->findById($this->User->id);
    $this->set('current_user', $current_user);

    $this->User->useValidationRules('ChangePassword');
    $this->User->validate['re_password']['compare']['rule'] = array('equalToField', 'password', false);

    $this->User->set($this->data);
    if(!empty($this->data) && $this->User->validates()){
        $password = $this->data['User']['password'];
        $this->User->saveField('password', $password);

        $this->Session->setFlash('Your password has been updated');
        $this->redirect(array('action' => 'account'));
    }

    $this->layout = 'dashboard_admin';
}

【问题讨论】:

  • 为什么你还要在编辑中保存密码。只需在编辑时取消设置密码即可。
  • 我已经尝试在编辑功能中取消设置密码但仍然不起作用,你能举个例子吗?
  • This article 可能会提供更多关于最佳实践的见解,并且还展示了针对此问题的行为驱动方法。
  • 总是提到 CakePHP 版本。我猜你使用的是 Cakephp 1.3。如果是,那么下面的两个答案都不起作用..
  • @FazalRasel 我正在使用 cakephp 2.5.4

标签: cakephp cakephp-2.0 edit change-password before-save


【解决方案1】:

在编辑表单中添加新表单字段,而不是密码,添加 new_password。仅当用户在其中放一些东西时才会对其进行哈希处理...

public function edit($id = null) {
  $this->User->id = $id;

  if (!$this->User->exists()) {
    throw new NotFoundException(__('Invalid user'));
  }

  if ($this->request->is('post') || $this->request->is('put')) {
    if ($this->User->save($this->request->data)) {
      $this->Session->setFlash(__('Saved.', true));

      $this->redirect(array('action' => 'view', $id));
    } else {
      $this->Session->setFlash(__('Error.', true));
    }
  } else {
    $user = $this->User->read(null, $id);
    $this->request->data = $user;

    unset($this->request->data['User']['password']);
    $this->set('user', $user);
  }


public function beforeSave($options = array()) {
  if (!empty($this->data['User']['new_password'])) {
    $this->data['User']['password'] = AuthComponent::password($this->data['User']['new_password']);

    unset($this->data['User']['new_password']);
  }
}

【讨论】:

  • 嘿兄弟,谢谢帮助,我尝试使用它,但是当编辑用户并且没有更改数据然后提交时,密码已经哈希两次
  • 我已经编辑了我的答案。更改您的编辑视图,使其使用 new_password 而不是密码字段
  • unset($this->request->data['User']['password']);你不应该使用这个,而不是你应该使用 unset($user['User']['password']);在设置 $this->request->data 之前
  • @GrzegorzMotyl oke 编辑用户成功,但更改密码时,密码已保存在数据库中,没有散列,就像我有密码 123456 然后我更改为 test123 ,密码保存到数据库 test123 没有散列,任何其他想法?
  • @Abhishek 是的,我改变了 unset($this->request->data['User']['password']);进入 unset($user['User']['password']);谢谢你的理由
【解决方案2】:

仅在编辑的情况下使用此选项(用户没有更改他的密码),您甚至不应该向您的用户显示散列密码

  if ($this->request->is('post') || $this->request->is('put')) {
    unset($this->request->data['User']['password']);
    if ($this->User->save($this->request->data)) {
      $this->Session->setFlash(__('Saved.', true));

      $this->redirect(array('action' => 'view', $id));
    } else {
      $this->Session->setFlash(__('Error.', true));
    }
  }

【讨论】:

    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 2018-09-20
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2017-01-08
    • 2017-11-12
    相关资源
    最近更新 更多