2013-01-31 49 views
1

我正嘗試使用Auth中構建的cakephp進行用戶登錄。我設法驗證了一個用戶註冊(它與登錄位於同一個視圖),但沒有讓登錄工作。

我試圖登錄時得到的是我的'無效的用戶名或密碼,請重試'錯誤。我已經閱讀了博客教程,但是我對cake/php很陌生,並且只對1.3中的混亂項目起作用,這些項目起訴他們自己的原始身份驗證。

MarshallsController.php

class MarshalsController extends AppController { 
public $helpers  = array('Html', 'Form'); 
public $uses  = array("Marshal", "User"); 
public $components = array("RequestHandler","Session", "Auth"); 

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('register', 'login'); 
} 

public function index() { 
    $this->set('users', $this->User->find('all', 
      array(
       'conditions'=>array(
        'User.marshall_id'=>$Marshall['Marshall']['id'] 
       ) 
      ))); 
} 

//Run when Marshal attempts to register for login page 
public function register(){ 
    if ($this->request->is('post')) { 
     $this->Marshal->create(); 
     if ($this->Marshal->save($this->request->data)) { 
      //if new marshall has been saved fetch all their data 
      $marshal = $this->Marshal->find('first', 
       array(
        'conditions'=>array(
         "Marshal.email"  => $this->data['Marshal']['email'], 
        ) 
       ) 
      ); 
      if(!empty($marshal)){ 
       //set marshal session data to track logged in users and their data 
       $this->Session->write("Marshal",$marshal); 
      } 
      $this->Session->setFlash(__('The Marshal has been saved')); 
      //redirect user to logged in page 
      $this->redirect(array('controller' => 'pages', 'action' => 'home')); 
     } else { 
      $this->Session->setFlash(__('The Marshal could not be saved. Please, try again.')); 
      echo $this->render('login'); 
      exit(); 
     } 
    } 
    else{ 
     //if Marshal has not attempted to login redirect the back to the login/register page 
     echo $this->render('login'); 
     exit(); 
    } 
} 


public function login() { 

    //if user has atempted a login 
    if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      //If login detials are correct get user data 
      $marshal = $this->Marshal->find('first', 
       array(
        'conditions'=>array(
         "Marshal.email"  => $this->data['Marshal']['email'], 
        ) 
       ) 
      ); 
      if(!empty($marshal)){ 
       //set marshal session data to track logged in users and their data 
       $this->Session->write("Marshal",$marshal); 
      } 
      //redirect user to the logged in page 
      $this->redirect($this->Auth->redirect()); 
     } else { 
      $this->Session->setFlash(__('Invalid username or password, try again')); 
      debug($this->Auth->request->data); 
     } 

元帥模型

class Marshal extends AppModel { 

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

public $hasMany = array(
    'User' => array(
     'className'  => 'User', 
     'foreignKey' => 'marshal_id', 
     'conditions' => array('User.status' => '1'), 
    ) 
); 

public $validate = array(
    'first_name' => array(
     'required' => array(
      'rule' => array('notEmpty'), 
      'message' => 'A first name is required' 
     ) 
    ), 
    'last_name' => array(
     'required' => array(
      'rule' => array('notempty'), 
      'message' => 'A last name is required' 
     ) 
    ), 
    'password' => array(
     'required' => array(
      'rule' => array('minLength', '8'), 
      'message' => 'Minimum 8 characters long' 
     ) 
    ), 
    'email' => 'email' 
); 

}

login.ctp

<div class="row"> 
<?php echo $this->Session->flash('auth'); ?> 
<div class="sixcol"> 
    <?php 
    echo $this->Form->create('Marshal', array('action' => 'login')); 
    echo $this->Form->inputs(array(
     'legend' => __('Login'), 
     'email', 
     'password' 
    )); 
    echo $this->Form->end('Login'); 
    ?> 
</div> 
<div class="sixcol last"> 
    <?php 
    echo $this->Form->create('Marshal', array('action' => 'register')); 
    echo $this->Form->inputs(array(
     'legend' => __('register'), 
     'first_name', 
     'last_name', 
     'email', 
     'password' 
    )); 
    echo $this->Form->end('Register'); 
    ?> 
</div> 

回答

5

默認情況下,CakePHP的使用用戶名和密碼字段,但您擁有的電子郵件,而不是用戶名。你需要指定它:

public $components = array(
      'Auth' => array('authenticate' => array('Form' => array('userModel' => 'User', 
            'fields' => array(
                 'username' => 'email', 
                 'password' => 'password' 
                 ) 
               ) 
          ), 
        'authorize' => array('Controller'), 
        'loginAction' => array('controller' => 'users', 'action' => 'login'), 
        'loginRedirect' => array('controller' => 'home', 'action' => 'index'), 
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'), 
        'authError' => 'You don\'t have access here.', 
      ), 
     ); 

這是我的工作示例,隨時可以根據需要進行更改。

+0

此外,默認情況Auth使用用戶模型,但您使用的是馬歇爾模型,並沒有相應地指定'userModel'選項。 – ADmad

+0

我知道它默認爲用戶模型,但在文檔中找不到如何更改。我設法獲得登錄和設置會話數據,謝謝你的例子:) –

+0

感謝您的這個提示! –

0

你也可以查看安全散列方法,並與數據庫中的密碼進行比較:

Security::setHash('sha1'); 

(SHA1或MD5)

比較密碼:

Security::hash($password,"sha1", true);