如何在CakePHP中使用PasswordableBehavior?

时间:2013-12-19 04:23:18

标签: php cakephp cakephp-2.4

由于vanilla CakePHP在用户编辑视图上没有很好地处理密码字段(在密码字段中回显哈希密码等),我正在尝试使用dereuromark的PasswordableBehavior来处理用户注册和密码更新。

我尝试按照教程(http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/)进行以下更改,但服务器不断抛出错误。这里有什么问题?因为错误在PasswordableBehavior.php中,所以我并不是100%肯定我搞砸了。

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']);
}

register.ctp (可能的安全漏洞警报)

<?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();

最后,服务器错误:

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 个答案:

答案 0 :(得分:1)

1)严格的错误不是什么大问题。 IMO只是关闭严格错误报告。

2)您看到的错误是因为行为中的两种方法(beforeValidate()beforeSave())没有完整选项。

只需确保它们具有如下所示的正确选项,严格的错误就会消失:

public function beforeValidate(Model $model, $options = array()) {
    //...

public function beforeSave(Model $model, $options = array()) {
    //...