使用CakePHP更新用户电子邮件和密码 [英] Updating user email and password with CakePHP

查看:138
本文介绍了使用CakePHP更新用户电子邮件和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户注册的CakePHP应用程序。在用户页面上,我希望他们能够更新他们的电子邮件和密码。这是我的用户模型:

I have a CakePHP application with user registration. On the users page, I want them to be able to update their email and password. THis is my User model:

<?php

class User extends AppModel {
    public $name = 'User';
    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            ),
            'range' => array(
                'rule' => array('between', 4, 20),
                'message' => 'Between 4 and 20 characters'
            ),
            'characters' => array(
                'rule' => array('alphaNumeric'),
                'message' => 'Alphanumeric characters only'
            ),
            'unique' => array(
                'rule' => array('isUnique'),
                'message' => 'This username is taken'
            )
        ),
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'An email is required'
            ),
            'validEmail' => array(
                'rule' => array('email'),
                'message' => 'Please provide a valid email'
            ),
            'range' => array(
                'rule' => array('between', 5, 64),
                'message' => 'Between 5 and 64 characters'
            ),
            'unique' => array(
                'rule' => array('isUnique'),
                'message' => 'This email has already been used'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            ),
            'range' => array(
                'rule' => array('between', 5, 64),
                'message' => 'Between 5 and 64 characters'
            ),
        )
    );

    public function beforeSave() {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }

}

帮助程序创建表单:

<p>Modify your account settings</p>
<?php echo $this->Session->flash(); ?>
<?php
    echo $this->Form->create('User');
    echo $this->Form->input('currentPassword', array('type' => 'password'));
    echo $this->Form->input('username', array('disabled' => 'disabled', 'value' => $username));
    echo $this->Form->input('email');
    echo $this->Form->input('newPassword', array('type' => 'password'));
    echo $this->Form->end('Update');
?>

我如何检查当前密码是否有效,检查新电子邮件和密码是否通过

How would I go about checking if the current password is valid, checking if the new email and password pass validation rules and then updating the users record in my users table from within my controller?

推荐答案

添加新用户 - UsersController.php :

Adding a new user - UsersController.php:

public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
                    if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Registration complete. :)'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Error... Please try again.'));
            }
        }
    }

编辑用户 - UsersController。 php:

Editing a user - UsersController.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')) {
        if($this->Auth->user('password') == AuthComponent::password($this->request->data['User']['password']){
        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }}else{
         $this->Session->setFlash(__('Incorrect password.'));
        }
         else {
        $this->request->data = $this->User->read(null, $id);
    }
}

只需观察{}。:D

如果它不工作,请尝试

if($this->Auth->user('password') == $this->request->data['User']['password'])...

这篇关于使用CakePHP更新用户电子邮件和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆