2013-12-15 119 views
0

我的問題是我有一個不同的用戶表模型名稱:飼養員。我的登錄頁面總是說不正確的用戶名或密碼,因爲輸入的密碼沒有被散列。Cakephp身份驗證:2.4.2

這裏是視圖:

<?php 
    echo $this->Form->create(); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('password'); 
    echo $this->Form->end('Log in'); 
    echo $this->Html->link('Register', '/breeders/register'); 
?> 

這裏是AppController的:

類AppController的延伸控制器{

public $helpers = array('Html', 'Form', 'Session'); 
public $components = array('Session', 'Paginator', 
    'Auth' => array(
    'loginAction' => array('controller' => 'breeders', 'action' => 'login'), 
    'loginRedirect' => array('controller' => 'breeders', 'action' => 'desk'), 
    'logoutRedirect' => array('controller' => 'breeders', 'action' => 'login'), 
    'authorize' => array('Controller'), 
    'authenticate' => array(
     'Form' => array(
     'fields' => array(
      'username' => 'login', 
      'password' => 'password'), 
     'passwordHasher' => array(
      'className' => 'Simple', 
      'hashType' => 'sha256' 
     ) 
     ) 
    ) 
    ) 
); 

public function isAuthorized($user) 
{ 
    return true; 
} 

public function beforeFilter() { 

    $this->Auth->allow('login', 'logout', 'register', 'profile'); 
} 
} 

我的登錄方法:

public function login() { 
    $this->set('title_for_layout', __('Connection')); 

    if ($this->Session->read('Auth.Breeder')) { 
    return $this->redirect('/'); 
    } 

    if ($this->request->is('post')) { 
    if ($this->Auth->login()) { 
     return $this->redirect($this->Auth->redirectUrl()); 
    } 
    } 
} 

的d的beforeSave方法模型:

public function beforeSave($options = array()) { 

    if (!$this->id) { 
    $passwordHasher = new SimplePasswordHasher(); 
    $this->data['Breeder']['password'] = $passwordHasher->hash(
     $this->data['Breeder']['password'] 
    ); 
    } 
    return true; 
} 

我不知道我需要添加到使密碼被散列。任何幫助將受到歡迎。

回答

0

,因爲我無法無需人工哈希做到這一點,我在Controller登錄功能使用此代碼:

public function login() { 

    $this->set('title_for_layout', __('Connection')); 

    if ($this->Session->read('Auth.Breeder')) { 
    return $this->redirect('/'); 
    } 

    if ($this->request->is('post')) { 
    $passwordHasher = new SimplePasswordHasher(); 
    $this->request->data['Breeder']['password'] = $passwordHasher->hash(
     $this->request->data['Breeder']['password'] 
    ); 
    $breeder = $this->Breeder->find('first', array('conditions' => array('Breeder.login' => $this->request->data['Breeder']['username']), 'fields' => array('id'))); 

    $this->request->data['Breeder'] = array_merge($this->request->data['Breeder'], array('id' => $breeder['Breeder']['id'])); 

    if ($this->Auth->login($this->request->data['Breeder'])) { 
     return $this->redirect($this->Auth->redirectUrl()); 
    } 
    } 
} 

如果任何人有一個更好的解決方案,請不要猶豫,把它寫。

0

你爲什麼不使用

陣列( '的usermodel'=>'育種者)更改用戶模式

參考http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers

+0

看來,我已經在採取它關閉測試幾種可能性,但我曾嘗試過。現在我只是添加它,因爲我的登錄函數工作(我手動散列密碼,所以它應該哈希兩次)我想這不是解決方案。不管怎樣,謝謝你 – Charline