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 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译