2015-06-30 46 views
2

我使用CakePHP 2.6.7 在AppController.php

class AppController extends Controller { 

    public $components = array(
     'Session', 
     'Auth' => array(
      'authenticate' => array(
       'Form' => array(
        'fields' => array(
         'username' => 'email', //Default is 'username' in the userModel 
         'password' => 'password' //Default is 'password' in the userModel 
        ), 
        'userModel' => 'Admin', 
        'passwordHasher' => array(
         'className' => 'Simple', 
         'hashType' => 'sha256' 
        ) 
       ) 
      ), 
      'loginAction' => array(
       'controller' => 'admins', 
       'action' => 'login' 
      ), 
      'loginRedirect' => array('controller' => 'admins', 'action' => 'deshboard'), 
      'logoutRedirect' => array('controller' => 'admins', 'action' => 'login'), 
      'authError' => "You can't acces that page", 
      'authorize' => 'Controller' 
     ) 
    ); 

    public function beforeFilter() { 
     //parent::beforeFilter(); 
     $this->Auth->allow('index'); 
    } 

在AdminsController.php

<?php 

    class AdminsController extends AppController { 

     var $layout = 'admin'; 
     public function beforeFilter() { 
      parent::beforeFilter(); 
      $this->Auth->allow('create'); 
     } 
     function login() { 

      $this->loadModel('Admin'); 
      $this->layout = "admin-login"; 
      // if already logged in check this step 
      if ($this->Auth->loggedIn()) { 
       return $this->Auth->loginRedirect; //(array('action' => 'deshboard')); 
      } 
      // after submit login form check this step 
      if ($this->request->is('post')) { 

       if ($this->Auth->login()) { 
        echo 'ok<br/>'; 
        echo 'last query: '.$this->Admin->getLastQuery(); 
       exit; 
        return $this->Auth->loginRedirect; 
       } else { 
        echo 'Not ok<br/>'; 
        echo 'last query: '.$this->Admin->getLastQuery(); 
       exit; 
        $this->Session->setFlash('Invalid username/password combination OR you are blocked, try again'); 
        return $this->Auth->logoutRedirect; 
       } 

      } 
     } 
    } 

Model/admin.php

<?php 


    App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); 
    class Admin extends AppModel { 

     var $name = "admin"; 
     var $belongsTo = array('Role'); 
     public $validate = array(
      'email' => array(
       'rule' => 'isUnique', 
       'required' => true, 
       'message' => 'Email already exist' 
      ), 
      'password' => array(
       'rule' => array('minLength', '6'), 
       'message' => 'password must be minimum 6 characters long' 
      ) 
     ); 

     function hashPassword() { 
     if (!empty($this->data[$this->alias]['password'])) { 
       $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256')); 
       $this->data[$this->alias]['password'] = $passwordHasher->hash(
        $this->data[$this->alias]['password'] 
       ); 
      } 
     } 

     function beforeSave($options = array()) { 
      $this->hashPassword(); 
      return true; 
     } 

    } 

    ?> 

在查看/管理/ login.ctp

<div class="container-fluid"> 

    <div class="loginContainer"> 
     <?php echo $this->Session->flash(); ?> 
     <?php 
     echo $this->Form->create('Admin', array(
      'inputDefaults' => array(
       'label' => false, 
       'div' => false 
      ), 
      'class' => 'form-horizontal', 
      'role' => 'form', 
      'id'=>'loginForm', 
      'url' => array('controller' => 'admins', 'action' => 'login'), 
     )); 
     ?> 
      <div class="form-row row-fluid"> 
       <div class="span12"> 
        <div class="row-fluid"> 
         <label class="form-label span12" for="username"> 
          Email: 
          <span class="icon16 icomoon-icon-user-3 right gray marginR10"></span> 
         </label> 
         <?php 
         echo $this->Form->input('email', array(
          'class' => 'span12', 
          'id' => 'username', 
          'type' => 'text', 
         )); 
         ?> 
        </div> 
       </div> 
      </div> 

      <div class="form-row row-fluid"> 
       <div class="span12"> 
        <div class="row-fluid"> 
         <label class="form-label span12" for="password"> 
          Password: 
          <span class="icon16 icomoon-icon-locked right gray marginR10"></span> 
          <span class="forgot"><a href="#">Forgot your password?</a></span> 
         </label> 
         <?php 
         echo $this->Form->input('password', array(
          'class' => 'span12', 
          'id' => 'password', 
          'type' => 'password', 
         )); 
         ?> 
        </div> 
       </div> 
      </div> 
      <div class="form-row row-fluid">      
       <div class="span12"> 
        <div class="row-fluid"> 
         <div class="form-actions"> 
          <div class="span12 controls"> 
           <?php 
           echo $this->Form->button(
             'Login', array('class' => 'btn marginR10', 'type' => 'submit') 
           ); 
           ?> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
      <?php echo $this->Form->end(); ?> 
    </div> 

</div><!-- End .container-fluid --> 

問題是 $這個 - > auth->登錄()總是返回false。

I checked the last query after $this->auth->login() called. The query is: 
last query: SELECT `Admin`.`id`, `Admin`.`role_id`, `Admin`.`name`, `Admin`.`email`, `Admin`.`password`, `Admin`.`mobile`, `Admin`.`area`, `Admin`.`status`, `Admin`.`comment`, `Admin`.`created`, `Role`.`id`, `Role`.`name`, `Role`.`created`, `Role`.`modified` FROM `amrajegeachi`.`admins` AS `Admin` LEFT JOIN `amrajegeachi`.`roles` AS `Role` ON (`Admin`.`role_id` = `Role`.`id`) WHERE `Admin`.`email` = '[email protected]' LIMIT 1 

Where子句中缺少密碼。我想知道我的代碼有什麼問題。

+0

什麼是蛋糕版? – dav

+0

我目前的cakephp版本是2.6.7 –

+0

你是怎麼解決他的問題的? –

回答

0

在AppController的BeforeFilter行動組變量,

$this->set('isLoggedIn',$this->Auth->loggedIn()); 

檢查中使用同一個變量其他控制器,它不會讓問題。