【问题标题】:How to Edit User CakePHP 3如何编辑用户 CakePHP 3
【发布时间】:2015-06-18 22:28:46
【问题描述】:

所以我一直在尝试让编辑用户功能在我的应用程序中运行,但我对如何使用 CakePHP 3 执行此操作有点困惑。这就是我的编辑操作在我的 UsersController.php 中:

public function edit() {
    $this->layout = 'dashboard';

    $user = $this->Users->get($this->Auth->user('id'));
    if ($this->request->is(['post', 'put'])) {
      $this->Users->patchEntity($user, $this->request->data);
      if ($this->Users->save($user)) {
        $this->Flash->success(__('Your account has been edited'));
        return $this->redirect(['controller' => 'Users', 'action' => 'edit']);
      }
      $this->Flash->error(__('Your account could not be edited. Please fix errors below.'));
    }
    $this->set(compact('user'));
}

在我的 edit.ctp 文件中:

<?php
    $this->Form->templates($form_templates['defaultBootstrap']);
    echo $this->Form->create($user);
?>
<fieldset>
    <legend><?php echo __('Edit Profile'); ?></legend>
    <?php
      echo $this->Form->input('email', [
        'label' => __('Email'),
            'placeholder' => __('Email'),
            'autofocus'
        ]);
        echo $this->Form->input('currency', [
          'label' => __('Default Currency'),
          'options' => [
             'CAD' => 'CAD',
             'USD' => 'USD'
           ]
        ]);
        echo $this->Form->input('password', array(
          'label' => __('Password'),
          'placeholder' => __('Password'),
          'value' => ''
        ));
        echo $this->Form->input('confirm_password', array(
          'label' => __('Confirm Password'),
          'placeholder' => __('Confirm Password'),
          'type' => 'password'
        ));
    ?>
</fieldset>
<?php
    echo $this->Form->submit(__('Edit'));
    echo $this->Form->end();
?>

这个问题是附加到表单的密码是散列的,所以当我使用patchEntity时,它再次被散列,因为在实体User.php中:

protected function _setPassword($password) {
  return (new DefaultPasswordHasher)->hash($password);
}

当我在控制器中设置 $user 时,我也尝试过不获取密码。但是当我使用 patchEntity 时,它只是对空白值进行哈希处理。

也许我的做法完全错误,我只是在寻找有关如何解决这个问题的方向,如果有人可以帮忙的话。

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    如果您需要能够更改 edit 表单中的密码,那么您必须确保在编组之前将其删除,以防没有提供数据。

    这可以使用Users 表类中的Model.beforeMarshal 事件来实现。

    http://book.cakephp.org/3.0/en/orm/saving-data.html#before-marshal

    public function beforeMarshal(Event $event, \ArrayObject $data, \ArrayObject $options)
    {
        if(isset($data['password']) && empty($data['password'])) {
            unset($data['password']);
        }
    }
    

    这是一个非常基本的示例,您可能想要添加一些更严格的检查,也许在测试值是否为空之前删除空格等

    您还可以将编辑配置文件数据和编辑凭据分成不同的操作/视图/表单,然后使用fieldList 选项来限制可以编组的字段。

    http://book.cakephp.org/3.0/en/orm/saving-data.html#avoiding-property-mass-assignment-attacks

    编辑个人资料:

    $this->Users->patchEntity($user, $this->request->data, [
        'fieldList' => ['currency']
    ]);
    

    编辑凭据:

    $this->Users->patchEntity($user, $this->request->data, [
        'fieldList' => ['email', 'password']
    ]);
    

    【讨论】:

    • 你说“如果你需要有能力的话”,好像允许用户更改密码是一个奇怪的请求?我应该以不同的方式解决这个问题吗?
    • @ArakTai'Roth 让用户更改密码并不奇怪,只是编辑凭据和个人资料数据经常被分成应用程序的不同形式/部分,例如“更改您的个人资料”和“更改您的登录信息”。
    • 有什么理由认为这是一个更好的主意,还是有关系?
    • @ArakTai'Roth 这只是另一种避免您所面临的问题的方法,除了这可能主要是个人喜好,除非应用对此有任何特定要求。
    • 我在我的问题中添加了一点关于拆分配置文件数据和凭据的第二个选项。如果你知道的话,也许你可以补充你的答案?
    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多