2012-07-24 131 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

相關問題