Я пытаюсь использовать поведение DerEuromark's Passwordable с моим приложением CakePHP, но у меня проблемы с его работой. Я выполнил инструкции по установке (http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/), изменив свой контроллер и виды, но я все время получаю сообщение об ошибке, заявляя, что мои BeforeValidate
и BeforeSave
несовместимы с поведением - и, конечно, поведение не работает.CakePHP: BeforeSave & BeforeValidate с PasswordableBehavior
Я знаю, что мне нужно, чтобы эти две настройки были правильно настроены в моей модели, но я не знаю, как они должны выглядеть: инструкции не охватывают этот момент.
Что должно быть основной, ваниль BeforeValidate
и BeforeSave
должно быть похоже, что работает с таким образом?
Под моим контроллером Пользователи:
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']);
}
Passwordable поведение с BeforeValidate
и BeforeSave
, что мои user.php
должен быть совместим с: https://github.com/dereuromark/tools/blob/master/Model/Behavior/PasswordableBehavior.php
Погрешность:
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]
Edit: Модель пользователя:
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
App::uses('PasswordableBehavior', 'Tools.Model/Behavior');
/**
* User Model
*
* @property Group $Group
* @property Post $Post
*/
class User extends AppModel {
//simplified per-group only permissions- tell ACL to skip checking user AROs and only check group AROs
public function bindNode($user) {
return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
}
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'username' => array(
'rule' => 'isUnique',
'required' => true,
'allowEmpty' => false,
'on' => 'create',
'last' => false,
'message' => 'That username has already been taken'
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'group_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Group' => array(
'className' => 'Group',
'foreignKey' => 'group_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public function beforeValidate($options = array()) {
}
public function beforeSave($options = array()) {
}
}
Пожалуйста, обновите свой вопрос с кодом вашей модели 'User'. – Jan
Я добавил модель. У меня есть фиктивные функции, настроенные для BeforeValidate и BeforeSave, но они пока ничего не делают. Насколько я понимаю, PasswordableBehavior должен принимать pwd и pwd_repeat, сравнивать их и хешировать, если они совпадают - это правильно? Есть ли что-то еще, что мне нужно для правильной маршрутизации этих переменных? – schnauss