![](/img/trans.png)
[英]CakePHP: BeforeSave & BeforeValidate with PasswordableBehavior
[英]How to use PasswordableBehavior with CakePHP?
由於香草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)嚴格的錯誤並不重要。 IMO只是關閉了嚴格錯誤報告。
2)您看到的錯誤是因為Behavior中的兩個方法( beforeValidate()
和beforeSave()
)沒有完整的選項。
只需確保它們具有如下正確的選項,然后嚴格的錯誤就會消失:
public function beforeValidate(Model $model, $options = array()) {
//...
public function beforeSave(Model $model, $options = array()) {
//...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.