久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用或不使用密碼更新用戶 - CakePHP

Updating user with or without password - CakePHP(使用或不使用密碼更新用戶 - CakePHP)
本文介紹了使用或不使用密碼更新用戶 - CakePHP的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試在 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 亚洲精品v | 一区福利视频 | 国内精品一区二区 | 免费看a| 一区二区三区在线 | 一本一道久久a久久精品蜜桃 | 午夜手机在线视频 | 亚洲狠狠 | 国产精品99久久久久久宅男 | 亚洲综合色 | 欧美激情久久久久久 | 日日操操 | 在线观看日韩av | 日韩久久久久久 | 久久精品视频91 | 久久大全 | 玖玖玖在线 | 免费久久久 | 日本a∨精品中文字幕在线 亚洲91视频 | 欧美三级成人理伦 | 欧美一区二区三区免费电影 | 91秦先生艺校小琴 | av大片 | 天天拍夜夜爽 | 精品一区二区久久久久久久网站 | www.亚洲| 日韩av一区二区在线观看 | 久久草视频 | 日韩欧美中文字幕在线观看 | 激情欧美日韩一区二区 | 日韩字幕一区 | 天天操天天干天天曰 | 国产精品不卡 | 免费视频中文字幕 | 男女黄网站 | 欧美 日韩 国产 成人 在线 | 欧美视频在线播放 | 国产欧美日韩综合精品一 | 久久精品中文字幕 | 久久综合爱 | 精品一区精品二区 |