简体   繁体   English

如何在CakePHP中使用PasswordableBehavior?

[英]How to use PasswordableBehavior with CakePHP?

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. 由于香草CakePHP在用户编辑视图上不能很好地处理密码字段(将散列密码回显到密码字段中,等等),因此,我尝试使用dereuromark的PasswordableBehavior来处理用户注册和密码更新。

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. 我尝试按照本教程( http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/ )进行以下更改,但是服务器不断抛出错误。 What is the problem here? 这里有什么问题? Because the error is in PasswordableBehavior.php, I'm not 100% certain I'm screwing up. 由于该错误位于PasswordableBehavior.php中,因此我不确定100%是否搞砸了。

UsersController.php: 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) 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();

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) Strict errors are not a big deal. 1)严格的错误并不重要。 IMO just turn off Strict Error Reporting. IMO只是关闭了严格错误报告。

2) The errors you're seeing are because the two methods in the Behavior ( beforeValidate() and beforeSave() ) don't have the full options. 2)您看到的错误是因为Behavior中的两个方法( beforeValidate()beforeSave() )没有完整的选项。

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()) {
    //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM