2012-07-24 37 views
0

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

<?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'); 
?> 

我怎么会去检查当前密码是否有效,检查是否新电子邮件和密码传递验证规则,然后从我的控制器中更新我的用户表中的用户记录?

回答

1

添加新用户 - 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:

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'])... 
+0

这是如何检查用户输入的密码与存储在数据库中的密码相同的?从我所看到的情况来看,它假定它是相同的并继续进行下去。 – 2012-07-24 21:34:45

+0

噢...对不起,我会编辑它。 :)给我几分钟。 :) – 2012-07-24 21:36:05

0

答:

请问这个检查,如果用户输入密码进入相同什么存储在数据库中? - James Dawson

您可以在模型中添加下面的函数。

function check_user($check) { 
    if(!empty($check["EMail"]) && !empty($_POST['data']['User']['password'])) 
    { 
     $user = $this->find('first',array('conditions'=>array('User.EMail'=>$check["EMail"],'User.IsVerified'=>1))); 
     if(empty($user)) { 
     return FALSE; 
     } 
     $Encrypted = md5($_POST['data']['User']['password']); 
     if($user['User']['password'] != ($Encrypted)) { 
     return FALSE; 
     } 
    } 
     return TRUE; 
    } 

和验证规则

'EMail' => array(
      'email' => array(
       'rule' => array('email'), 
       'message' => 'Please enter valid email address..!', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       'on' => 'login', // Limit validation to 'create' or 'update' operations 
      ), 

      'check_user'=>array(
       'rule'=>'check_user', 
       'message'=>'Either your Username or Password is invalid', 
       'on' => 'login', // Limit validation to 'create' or 'update' operations 
       'last'=>TRUE, 

      ), 

     ), 
0
protected function _update_password() { 

     $password_error = false; 

     /** 
     * Handle post 
     */ 
     if (($this->request->is('post') || $this->request->is('put')) && isset($this->request->data['User'])) { 

      $old_pass_in_db = $this->User->read('password', $this->Session->read('Auth.User.id')); 

      $old_pass_in_post = $this->Auth->password($this->request->data['User']['old_password']); 

      //assign post data 
      $this->User->set($this->request->data); 


      //validate 
      if (trim($old_pass_in_post) != trim($old_pass_in_db['User']['password'])) { 
       $this->User->validationErrors['old_password'] = __("Old password do not match."); 
      } else { 
       unset($this->User->validationErrors['old_password']); 
      } 

      if ($this->User->validates(array('fieldList' => array('password', 'password_confirm'))) && ($old_pass_in_post == $old_pass_in_db['User']['password'])) { 
       $this->User->id = $this->Session->read('Auth.User.id'); 
       if ($this->User->save(array('password', $this->Auth->password($this->request->data['User']['password'])), false)) { 
        $this->Session->setFlash(__('Password updated successfully.', true), 'default', array('class' => 'alert alert-success')); 
       } //end save 
      } else { 
       $password_error = true; 
      } 
     } 

     $this->set('password_error', $password_error); 
    } 
1

对于那些谁想要的东西不改变模式,而不显示哈希密码:Updating user with or without password - CakePHP

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` 
// 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'); 
+0

尽管此链接可能会回答问题,但最好在此处包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/10169608) – d0nut 2015-11-11 14:44:09

+1

@iismathwizard好的,我添加了一个'TL; DR';感谢您的反馈意见 ;) – Blag 2015-11-11 15:06:48

相关问题