使用或不使用密码更新用户 - CakePHP
Updating user with or without password - CakePHP
我试图在 cakePHP v.2.7 中找到一种处理管理面板的好方法"edit user"。
明确一点:我希望能够在覆盖或不覆盖密码的情况下编辑我的用户,但是 cakePHP 验证器工具不允许我做我想做的事...
我已经看过 CakePHP: Edit Users without changing password and Updating user email and password with CakePHP 但它看起来真的很脏 :
- 第一个不应用更新规则
- 第二次显示哈希(只是没有...u_u")
没有其他办法吗? (尽可能少的行)
长话短说:
查看
// add in your view `app/View/Users/edit.ctp`
// a 'fake' field you'll only use on the controller
echo $this->Form->input('new_password');
控制器
// add in your controller `app/Model/User.php@edit()`
// if we have a new password, create key `password` in data
if(!empty($new_password = $this->request->data['User']['new_password']))
$this->request->data['User']['password'] = $new_password;
else // else, we remove the rules on password
$this->User->validator()->remove('password');
好的,我终于得到了我想要的,这是我的代码:
在您的 app/View/Users/edit.ctp
上,您向表单添加了一个字段(自定义字段,不要将其添加到您的数据库)
<?php
// app/View/Users/edit.ctp
echo $this->Form->create('User');
// your other fields
// a 'fake' field you'll only use on the controller
echo $this->Form->input('new_password');
不要改变你的 app/Model/User.php
;这是我的 :
<?php
// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $validate = array(
// [...] other rules
'password' => array(
'passwordLength'=>array(
'rule' => array('minLength', 8),
'message' => 'Too short...',
),
'passwordNotBlank'=>array(
'rule' => 'notBlank',
'required' => true,
'allowEmpty' => false,
'message' => 'A password is required',
),
),
);
public function beforeSave($options = array()) {
if (!empty($pwd = $this->data[$this->alias]['password']))
$this->data[$this->alias]['password'] = AuthComponent::password($pwd);
return true;
}
}
然后在你的 app/Controller/UsersController.php
上使用这个:
<?php
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists())
throw new NotFoundException(__('Invalid user'));
if ($this->request->is('post') || $this->request->is('put')) {
// IMPORTANT >>>>>>>>>>>
// if we have a new password, create key `password` in data
if(!empty($new_password = $this->request->data['User']['new_password']))
$this->request->data['User']['password'] = $new_password;
else // else, we remove the rules on password
$this->User->validator()->remove('password');
// <<<<<<<<<<<<<<<<<<<<<
// then we try to save
if ($this->User->save($this->request->data)) {
$this->Flash->success(__('The user has been updated'));
$this->redirect(array('action' => 'index'));
}
else
$this->Flash->warning(__('The user could not be updated.'));
}
else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
有了这 4 行重要内容,您现在可以根据需要设置新密码或禁用密码验证。
我用这个作为参考
http://book.cakephp.org/2.0/en/models/data-validation.html#removing-rules-from-the-set
我试图在 cakePHP v.2.7 中找到一种处理管理面板的好方法"edit user"。
明确一点:我希望能够在覆盖或不覆盖密码的情况下编辑我的用户,但是 cakePHP 验证器工具不允许我做我想做的事...
我已经看过 CakePHP: Edit Users without changing password and Updating user email and password with CakePHP 但它看起来真的很脏 :
- 第一个不应用更新规则
- 第二次显示哈希(只是没有...u_u")
没有其他办法吗? (尽可能少的行)
长话短说:
查看
// add in your view `app/View/Users/edit.ctp`
// a 'fake' field you'll only use on the controller
echo $this->Form->input('new_password');
控制器
// add in your controller `app/Model/User.php@edit()`
// if we have a new password, create key `password` in data
if(!empty($new_password = $this->request->data['User']['new_password']))
$this->request->data['User']['password'] = $new_password;
else // else, we remove the rules on password
$this->User->validator()->remove('password');
好的,我终于得到了我想要的,这是我的代码:
在您的 app/View/Users/edit.ctp
上,您向表单添加了一个字段(自定义字段,不要将其添加到您的数据库)
<?php
// app/View/Users/edit.ctp
echo $this->Form->create('User');
// your other fields
// a 'fake' field you'll only use on the controller
echo $this->Form->input('new_password');
不要改变你的 app/Model/User.php
;这是我的 :
<?php
// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
public $validate = array(
// [...] other rules
'password' => array(
'passwordLength'=>array(
'rule' => array('minLength', 8),
'message' => 'Too short...',
),
'passwordNotBlank'=>array(
'rule' => 'notBlank',
'required' => true,
'allowEmpty' => false,
'message' => 'A password is required',
),
),
);
public function beforeSave($options = array()) {
if (!empty($pwd = $this->data[$this->alias]['password']))
$this->data[$this->alias]['password'] = AuthComponent::password($pwd);
return true;
}
}
然后在你的 app/Controller/UsersController.php
上使用这个:
<?php
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists())
throw new NotFoundException(__('Invalid user'));
if ($this->request->is('post') || $this->request->is('put')) {
// IMPORTANT >>>>>>>>>>>
// if we have a new password, create key `password` in data
if(!empty($new_password = $this->request->data['User']['new_password']))
$this->request->data['User']['password'] = $new_password;
else // else, we remove the rules on password
$this->User->validator()->remove('password');
// <<<<<<<<<<<<<<<<<<<<<
// then we try to save
if ($this->User->save($this->request->data)) {
$this->Flash->success(__('The user has been updated'));
$this->redirect(array('action' => 'index'));
}
else
$this->Flash->warning(__('The user could not be updated.'));
}
else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
有了这 4 行重要内容,您现在可以根据需要设置新密码或禁用密码验证。
我用这个作为参考 http://book.cakephp.org/2.0/en/models/data-validation.html#removing-rules-from-the-set