dongqia0240 2013-12-19 04:23
浏览 35
已采纳

如何在CakePHP中使用PasswordableBehavior?

Since vanilla CakePHP doesn't handle password fields very well on user edit views (echoing the hashed password into the password field, etc), I'm trying to use dereuromark's PasswordableBehavior to handle user registration and password updates.

I tried following the tutorial (http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/) making the following changes, but the server keeps throwing an error. What is the problem here? Because the error is in PasswordableBehavior.php, I'm not 100% certain I'm screwing up.

UsersController.php:

public function register() {
if ($this->request->is('post') || $this->request->is('put')) {
    $this->User->Behaviors->attach('Tools.Passwordable');
    if ($this->User->save($this->request->data, true, array('username', 'name', 'email', 'pwd', 'pwd_repeat', 'group_id'))) {
    $this->Session->setFlash(__('The user has been saved'), 'flash/success');
            $this->redirect(array('action' => 'index'));
} else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'flash/error');
        }
    unset($this->request->data['User']['pwd']);
    unset($this->request->data['User']['pwd_repeat']);
}

and register.ctp (possible security hole alert)

<?php 
echo $this->Form->create('User', array('role' => 'form'));
echo $this->Form->input('username', array('class' => 'form-control'));
echo $this->Form->input('name', array('class' => 'form-control'));
echo $this->Form->input('email', array('class' => 'form-control'));
echo $this->Form->input('password', array('class' => 'form-control'));
echo $this->Form->hidden('group_id', array('value'=>3));
echo $this->Form->submit('Submit', array('class' => 'btn btn-large btn-primary'));
echo $this->Form->end();

Finally, the server error:

Strict (2048): Declaration of PasswordableBehavior::beforeValidate() should be compatible with ModelBehavior::beforeValidate(Model $model, $options = Array) [APP/Plugin/Tools/Model/Behavior/PasswordableBehavior.php, line 338]
Strict (2048): Declaration of PasswordableBehavior::beforeSave() should be compatible with ModelBehavior::beforeSave(Model $model, $options = Array) [APP/Plugin/Tools/Model/Behavior/PasswordableBehavior.php, line 338]
  • 写回答

1条回答 默认 最新

  • dongxia9519 2013-12-19 05:30
    关注

    1) Strict errors are not a big deal. IMO just turn off Strict Error Reporting.

    2) The errors you're seeing are because the two methods in the Behavior (beforeValidate() and beforeSave()) don't have the full options.

    Just make sure they have the correct options like below, and the strict errors will go away:

    public function beforeValidate(Model $model, $options = array()) {
        //...
    
    public function beforeSave(Model $model, $options = array()) {
        //...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵
  • ¥15 cfx离心泵非稳态计算