2012-11-21 35 views
0

我在登錄表單$ this-> Auth-> login()總是返回false。我爲用戶帳戶使用不同的表名。下面的是我的代碼無法使用不同的表在cakephp中登錄

HgAdminscontroller.php

<?php 
    // app/Controller/UsersController.php 
    class HgAdminsController extends AppController 
{ 
    public function beforeFilter() 
    { 
     parent::beforeFilter(); 
     //$this->Auth->allow('add', 'logout'); 
    } 
    public function index() { 
     $this->HgAdmin->recursive = 0; 
     $this->set('users', $this->paginate()); 
    } 
    /* 
public function add() { 
if ($this->request->is('post')) { 
$this->HgAdmin->create(); 
if ($this->HgAdmin->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.')); 
} 
} 
}*/ 
    public function login() { 

    if ($this->request->is('post')) { 
    echo AuthComponent::password($this->data['password']); 
    debug($this->data); 
     if ($this->Auth->login()) { 
     $this->redirect($this->Auth->redirect()); 
     } else { 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 
    } 
    public function logout() { 
     $this->redirect($this->Auth->logout()); 
    } 
} 
?> 

appController.php 
App::uses('Controller', 'Controller'); 

/** 
* Application Controller 
* 
* Add your application-wide methods in the class below, your controllers 
* will inherit them. 
* 
* @package  app.Controller 
* @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller 
*/ 
    class AppController extends Controller { 
    public $components = array(
     'Session', 
     'Auth' => array(
     'loginRedirect' => array('controller' => 'posts', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home') 
     ) 
    ); 
    public function beforeFilter() { 
     $this->Auth->allow('index', 'view'); 
//  Security::setHash('md5'); 
     $this->Auth->userModel = 'hg_admins'; 
     $this->Auth->loginAction = array('controller' => 'HgAdmins', 'action' => 'login'); 
    } 
//... 
} 

HgAdmin.php //model 
<?php 
class HgAdmin extends AppModel { 
    var $name= "hg_admin"; 
    public $validate = array(
     'username' => array(
     'required' => array(
     'rule' => array('notEmpty'), 
     'message' => 'A username is required' 
     ) 
    ), 
     'password' => array(
     'required' => array(
     'rule' => array('notEmpty'), 
     'message' => 'A password is required' 
     ) 
    ) 
    ); 

?> 

Login view 

<h2>Login Here</h2> 
<?php echo $this->Session->flash('auth'); 
echo $this->form->create('HgAdmin'); 
echo $this->Form->input("username"); 
echo $this->Form->input("password"); 
echo $this->form->end(__("Login")); 
?> 

我檢查數據庫的密碼值是與從AuthComponent($這 - >數據[「密碼」])::正在添加密碼;所以不知道爲什麼它是騙人的。如果我使用cakephp的添加功能添加用戶,用戶將添加相同的密碼,但無法登錄。

回答

3
public $components = array(
'Auth' => array(
    'loginAction' => array(
     'controller' => 'writers', 
     'action' => 'login' 

    ), 
    'loginRedirect' => array(
     'controller' => 'home', 
     'action' => 'index' 
    ), 
    'logoutRedirect' =>array (
     'controller' => 'home', 
     'action' => 'index' 
    ), 
    'authError' => 'You have to login to access this page', 
    'authenticate' => array(
     'Form' => array(
      'userModel' =>'Writer', 
      'fields' => array('username' => 'email') 
     ) 
    ), 
    'scope' =>array('User.status' => '1'), 
    'authorize' => array('Controller') 
), 
    'Session', 
); 

你必須要使用在你想申請認證即我ahve使用作家在db.Check改變我的表作家模型的名字!

+0

我已經改變了我的appcontroller,如你所說,但同樣的信息出現'無效的用戶名或密碼,再試一次'。 –

+0

在添加用戶之前,您是否使用過AuthComponent :: password()來加密密碼,然後將其保存在數據庫中? – huzefam

+0

我發現它在login.ctp中存在的問題。我剛換了 echo $ this-> form-> create('HgAdmin');到 echo $ this-> Form-> create('HgAdmin');將爲我工作,正如你所說的在usermodel提供modelname而不是表名,謝謝 –

0

@huzefam答案是正確的,但我只是想添加任何使用Bcrypt身份驗證的情況。這裏我使用Accounts表。應該添加這樣的東西。希望這個幫助