使用或不使用密码更新用户 - CakePHP

2023-01-18php开发问题
4

本文介绍了使用或不使用密码更新用户 - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17