0

我正在为我的站点开发一些管理功能,具有管理员权限的用户能够为给定的用户 ID 打开表单。通过这种形式,他们可以更改用户权限、更改用户密码或其他方式。

但是,当从模型构造表单时,密码字段会从数据库中提取散列密码并填充密码字段。结果,当管理员保存表单时,散列密码被视为明文,因此再次散列,覆盖原始密码。

我需要的是某种方法来允许管理员用户更改表单,但只有在更改时才在数据库上散列和更新密码。

我的想法是将表单设置密码构造为空白:

查看/用户/edit.ctp:

echo $this->Form->input('User.password',array(
        'value' => '',
        'type' => 'password',
        'autocomplete' => 'off'
    )
);

save并对跳过密码进行某种检查;但这就是我卡住的地方。

控制器/用户控制器.php

public function edit($id = null) 
{
    $this->User->id = $id;
    if ($this->request->is('get')) 
    {
        $this->request->data = $this->User->read();
    } else  {
        //Something here????
        if ($this->User->save($this->data))
        {
            $this->Session->setFlash('Your user has been updated.');
            $this->redirect(array('action' => 'index'));
        } else 
        {
            $this->Session->setFlash('Unable to update your user.');
        }
    }

    $this->set('groups',$this->User->Group->find('list'));//, array( 'fields' => array('id', 'name'))));
    $this->set('sites',$this->User->Site->find('list'));//, array( 'fields' => array('id', 'name'))));
}

如果没有更改,如何检查并防止密码更新?

决定的解决方案

根据提供的答案,我在同一页面上使用了第二个表单,该表单重新使用了用户通过的注册验证。更新站点/组权限时,用户通过一种形式发送,而密码通过另一种形式发送。

4

2 回答 2

1

我会构建两种管理表单,一种用于更改权限,另一种仅用于更新密码。当您使用它时,更改密码表单应该有第二个字段用于验证更改。

有一些 CakePHP 插件可以帮助管理用户,特别是密码。

于 2012-11-28T19:21:45.783 回答
1

我总是创建一个专门用于更改密码的新表单。您应该将密码字段替换为更改密码的链接。

或者,您可以禁用输入字段并要求单击按钮并使用 javascript 删除disabled输入元素上的属性

echo $this->Form->input('User.password',array(
        'value' => '',
        'type' => 'password',
        'autocomplete' => 'off',
        'disabled' => true
    )
);

Jquery,因为它很容易

$(function(){
   $('UsersPassword').click(function(){
      $(this).attr('disabled', '0');
   });
});
于 2012-11-28T19:48:00.030 回答