2011-04-26 131 views
0

我按照CakephpTV上的教程进行AuthComponent注册。昨天一切运转良好,但今天并非如此。Cakephp注册问题

当我试图添加新帐户它说密码不匹配,并在密码字段有散列密码,并在密码确认有正常(不哈希)密码。

我UserController类:

class UsersController extends AppController 
{ 
    function beforeFilter() { 
     parent::beforeFilter(); 
     $this->Auth->allow('new_user'); 

     if($this->action == 'add' || $this->action == 'edit') { 
      $this->Auth->authenticate = $this->User; 
     } 
    } 

    function new_user() 
    { 
     if (!empty($this->data)) {    
      if ($this->User->save($this->data)) {     
       $this->Session->setFlash('Rejestracja zakończona pomyślnie');     
       $this->redirect(array('action' => 'index'));    
      }   
     } 
    } 

    function login() { 

    } 

    function logout() { 
     $this->redirect($this->Auth->logout()); 
    } 
} 

我的用户模型:

class User extends AppModel { 
    var $name = 'User'; 
    var $validate = array (
     'email' => array(
      'Please supply a valid email address.' => array(
       'rule' => 'email', 
       'message' => 'Please supply a valid email address.' 
      ), 
      'Already exists' => array(
       'rule' => 'isUnique', 
       'message' => 'Must be unique' 
      ) 
     ), 
     'password' => array (
      'aThis field must have between 6 and 40 alphanumeric characters.' => array(
       'rule' => '/^[^\'"]{6,40}$/i', 
       'message' => 'aThis field must have between 6 and 40 alphanumeric characters.' 
      ), 
      'Do not match' => array(
       'rule' => 'matchPasswords', 
       'message' => 'Do not match' 
      ) 
     ) 
); 

    function matchPasswords($data) { 
    if($data['password'] == $this->data['User']['password_confirmation']) { 
     return TRUE; 
    } 
    $this->invalidate('password_confirmation', 'DO not match'); 
    return FALSE; 
    } 

    function hashPasswords($data) { 
    if(isset($this->data['User']['password'])) { 
     $this->data['User']['password'] = Security::hash($this->data['User']['password'], NULL, TRUE); 
     return $data; 
    } 
    return $data; 
    } 

    function beforeSave() { 
    $this->hashPasswords(NULL, TRUE); 
    $this->data['User']['registration_date'] = date("Y-m-d H:i:s"); 
    return TRUE; 
    } 
} 

我new_user.ctp:

echo $this->Form->create('User', array('action' => 'new_user')); 
    echo $this->Form->input('email'); 
    echo $this->Form->input('password'); 
    echo $this->Form->input('password_confirmation',array('type' => 'password')); 
    echo $this->Form->end('Zarejestruj'); 

我的AppController类:

class AppController extends Controller { 
    var $components = array('Auth','Session'); 

    function beforeFilter() { 
     $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
     $this->Auth->allow('index','view','display','new_user'); 
     $this->Auth->authError = 'Zaloguj się aby zobaczyć tą stronę.'; 
     $this->Auth->loginError = 'Wprowadzono niepoprawne dane.'; 
     $this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home'); 
     $this->Auth->loginRedirect = array('controller'=>'pages','action'=>'home'); 
    } 
} 

回答

3

CakePHP在User模型中自动散列名为password的字段。这就是为什么当表单失败时你会得到一个已经散列的密码。你可能想要检查你找回的散列是否与你期望得到的散列相同。

除此之外,在您的功能matchPasswords您检查$this->data['password'],而我认为应该是$this->data['User']['password']

+0

没有帮助。 :( – latata 2011-04-27 16:11:42