2011-06-10 3 views
0

완벽하게 작동하는 것처럼 보였습니다. 사용자가 비밀번호를 변경할지 여부를 결정할 수 있도록 양식에 체크 박스를 추가했습니다. 사용자 추가 작업에서 유효성 검사가 제대로 작동하고 오류가있는 경우 사용자를 저장하지 않지만 사용자 편집 작업을 통해 두 암호 필드가 비어있는 경우에도 유효성 검사를 저장하지만 데이터가 있으면 올바르게 검사합니다 어느 쪽인가의 패스워드 입력. 여기비밀번호 저장시 Auth가 제대로 확인되지 않음

class User extends AppModel { 
var $name = 'User'; 
var $displayField = 'name'; 
var $validate = array(
    'username' => array(
     'notempty' => array(
      'rule' => array('notempty'), 
      'message' => 'User must have a username to login with', 
     ), 
    ), 
    'password' => array(
     'notempty' => array(
      'rule' => array('notempty'), 
      'message' => 'User must have a password', 
     ), 
     'alphanumeric' => array(
      'rule' => array('alphanumeric'), 
      'required' => true, 
      'message' => 'User must have a password' 
     ), 
     'minlength' => array(
      'rule' => array('minlength', 6), 
      'message' => 'Password must be at least 6 characters', 
     ), 
     'confirmPassword' => array(
      'rule' => array('confirmPassword', 'password'), 
      'message' => 'Passwords do not match' 
     ), 
    ), 
    'password_confirm' => array(
     'notempty' => array(
      'rule' => array('notempty'), 
      'message' => 'User must have a password', 
     ), 
     'alphanumeric' => array(
      'rule' => array('alphanumeric'), 
      'required' => true, 
      'message' => 'User must have a password' 
     ), 
     'minlength' => array(
      'rule' => array('minlength', 6), 
      'message' => 'Password must be at least 6 characters', 
     ), 
    ), 
); 

function confirmPassword($data) { 
    return ($data['password'] === Security::hash(Configure::read('Security.salt').$this->data['User']['password_confirm'])); 
} 

그리고 내 사용자 편집 작업은 다음과 같습니다 : 여기 내 모델의이 단지 절반 대답,하지만 당신이 올바른 방향으로 이동 가야로 사전에

function admin_edit($id = null) { 
    $this->set('title_for_layout', 'Users/Editing User'); 

    if (!$id && empty($this->data)) { 
     $this->Session->setFlash(__('Invalid user specified', true), 'flash_error'); 
     $this->redirect(array('action' => 'index')); 
    } 
    if (!empty($this->data)) { 
     $redirect = array('action' => 'index'); 
     if ($this->data['User']['edit_password'] == 1) { 
      $fields = array('username', 'confirm_password', 'password', 'name'); 
      if ($this->data['User']['id'] == $this->Auth->user('id')) { 
       $redirect['action'] = 'logout'; 
      } 
     } else { 
      $fields = array('username', 'name'); 
     } 

     if ($this->User->save($this->data, true, $fields)) { 
      $this->Session->setFlash(__(sprintf('The user <i>%s</i> was saved successfully.', $this->data['User']['username']), true), 'flash_success'); 
      $this->redirect($redirect); 
     } else { 
      $this->Session->setFlash(__('There were errors when trying to save the user', true), 'flash_error'); 
     } 
    } 
    if (empty($this->data)) { 
     $this->data = $this->User->read(null, $id); 
     $this->data['User']['password'] = ''; 
     $this->data['User']['edit_password'] = 0; 
    } 
} 

답변

0

죄송합니다.

정식 구성 요소는 자동으로 사용자 이름 필드는

그래서 당신이 예상 중 하나에 데이터를 설정 한 제출 된 데이터에있는 경우 .. 암호 필드를 해시하거나 방지 할 수 있습니다.

이 방법도 유용 할 것입니다. http://book.cakephp.org/view/1259/hashPasswords

+0

도움 주셔서 감사합니다. 문제를 해결하기 위해 password_confirm 대신 confirm_password를 확인하고 비밀번호가 자동으로 해시 된 후 자동으로 유효성 검사를 통과합니다. –

관련 문제