問題描述
我嘗試在 cakePHP v.2.7 中找到一種良好而干凈的方法來處理管理面板編輯用戶".
I try to find a good and clean way to deal with an admin panel "edit user" in cakePHP v.2.7.
需要明確的是:我希望能夠在覆蓋或不覆蓋用戶密碼的情況下編輯我的用戶,但是 cakePHP 驗證器工具不允許我做我想做的事...
To be clear : I want to be able to edit my user with or without overwriting their password, but the cakePHP validator tool don't let me do what I want...
我已經看過CakePHP:在不更改密碼的情況下編輯用戶 和 使用 CakePHP 更新用戶電子郵件和密碼 但似乎真的臟:
I've already take a look at CakePHP: Edit Users without changing password and Updating user email and password with CakePHP but it seem really dirty :
- 第一個不應用更新規則
- 第二個顯示哈希值(只是沒有... u_u")
沒有其他方法可以做到嗎?(盡可能少的行)
There is no other way to do it ? (with as few line as possible)
推薦答案
TL;DR :
查看
// 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');
<小時>
好的,我終于得到了我想要的,這是我的代碼:
Ok, I finally get what I want, here is my code :
在您的 app/View/Users/edit.ctp
上,您將一個字段添加到表單中(自定義字段,不要將其添加到您的數據庫中)
On your app/View/Users/edit.ctp
you add a field to your form (a custom one, don't add it to your DB)
<?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
;這是我的:
Don't change your app/Model/User.php
; here is mine :
<?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
上你使用這個:
And on your app/Controller/UsersController.php
you use this :
<?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 行重要代碼,您現在可以根據需要設置新密碼或禁用密碼驗證.
With the 4 important lines, you are now able to set a new password if needed or disable the validation on the password.
我用這個作為參考http://book.cakephp.org/2.0/en/models/data-validation.html#removing-rules-from-the-set
這篇關于使用或不使用密碼更新用戶 - CakePHP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!