2013-04-10 56 views
0

存储之前散列这是AppController代码:密码没有在数据库

public $components = array(
    'Session', 
    'Auth' => array(
     'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'admins', 'action' => 'index'), 
     'authorize' => array('Controller') 
    ) 
); 

public function beforeFilter() 
{ 
    $this->Auth->loginAction = array('controller' => 'admins', 'action' => 'login'); 
} 

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

这是AdminsController代码:

public $name = 'Admins'; 

public function beforeFilter() 
{ 
    parent::beforeFilter(); 

    $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
    $this->Auth->allow('signup'); 
} 

public function index() 
{ 
    $this->Admin->recursive = 0; 
    $this->set('admins', $this->Admin->find('all')); 
} 

public function login() 
{ 
    $this->set('title_for_layout', 'Polkadot - Admin Login'); 

    if($this->request->is('post')) 
    { 
     if($this->Auth->login()) 
     { 
      $this->Session->setFlash('Login successfull!'); 
      $this->redirect($this->Auth->redirect()); 
     } 
     else 
     { 
      $this->Session->setFlash('Login failed!'); 
     } 
    } 
} 

public function signup() 
{ 
    $this->set('title_for_layout', 'Polkadot - Admin Sign Up'); 

    if($this->request->is('post')) 
    { 
     if($this->Admin->save($this->request->data)) 
     { 
      $this->Session->setFlash('Account created successfully!'); 
      $this->redirect(array('action' => 'index')); 
     } 
     else 
     { 
      $this->Session->setFlash('Account could not be created, please try again!'); 
     } 
    } 
} 

我可以看到帐户创建,但在密码数据库是纯文本。当我尝试登录时,它失败。我做错了什么?

我想在管理员登录时打开Dashboard控制器的index操作。帐户注册使用四个输入字段:名称(文本),电子邮件(文本),密码(密码)和密码确认(密码)。因此,登录表单使用两个字段:电子邮件(文本)和密码(密码)。

使用的CakePHP版本是2.3.1

所有帮助表示感谢!

[编辑]:以下是错误日志

2013-04-10 19:31:13 Error: [MissingControllerException] Controller class CssController could not be found. 
Exception Attributes: array (
    'class' => 'CssController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/css/cake.generic.css 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
2013-04-10 19:31:13 Error: [MissingControllerException] Controller class ImgController could not be found. 
Exception Attributes: array (
    'class' => 'ImgController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/img/cake.power.gif 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
2013-04-10 19:31:25 Error: [MissingControllerException] Controller class CssController could not be found. 
Exception Attributes: array (
    'class' => 'CssController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/css/cake.generic.css 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
2013-04-10 19:31:25 Error: [MissingControllerException] Controller class ImgController could not be found. 
Exception Attributes: array (
    'class' => 'ImgController', 
    'plugin' => NULL, 
) 
Request URL: /polkapanel/img/cake.power.gif 
Stack Trace: 
#0 C:\wamp\www\polkapanel\app\webroot\index.php(109): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 
#1 {main} 
+0

控制器中没有beforeSave()函数,它应该放在模型上(AppModel或User.ctp,取决于你想要的位置管理密码) – Nunser 2013-04-10 18:49:32

回答

2

你必须把beforeSave功能在您的Admin型号:

admin.php的(或者无论你怎么称呼它):

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

啊,我犯了一个愚蠢的错误。所以,现在密码在被存储在数据库中之前被散列。但仍然登录失败。在login()函数中,在使用存储的密码进行验证之前,是否必须再次散列管理员输入的密码?如果是的话,我该怎么做? – 2013-04-10 19:18:09

+0

你可以把错误日志?您不需要在'login'方法中验证密码,CakePHP会自动执行它 – 2013-04-10 19:22:09

+0

如何查看错误日志?抱歉,我是CakePHP的新手。 – 2013-04-10 19:24:50

相关问题