2015-06-10 253 views
0

我是新的cakephp,我有1個問題,請幫助我! 我有1表名quan_tri_viens相同的用戶,但我不使用表的用戶問題登錄cakephp

我閱讀,並按照指示 http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

,但我不能在

型號標誌:QuanTriViensController

<?php 
App::uses('AppController', 'Controller'); 

class QuanTriViensController extends AppController { 

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

    public function login() { 
     if ($this->request->is('post')) 
     { 
      if ($this->Auth->login()) 
      { 
       return $this->redirect($this->Auth->redirectUrl()); 
      } 
     $this->Session->setFlash(__('Invalid username or password, try again')); 
     } 
    } 

    public function logout() { 
     return $this->redirect($this->Auth->logout()); 
    } 



    public function view($id = null) { 
     $this->QuanTriVien->id = $id; 
     if (!$this->QuanTriVien->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $this->set('quantrivien', $this->QuanTriVien->read(null, $id)); 
    } 

    public function index() { 
     $this->QuanTriVien->recursive = 0; 
     $this->set('quantriviens', $this->paginate()); 
    } 
    public function add() { 
     if ($this->request->is('post')) { 
      $this->QuanTriVien->create(); 
      if ($this->QuanTriVien->save($this->request->data)) { 
       $this->Session->setFlash(__('The user has been saved')); 
       return $this->redirect(array('action' => 'index')); 
      } 
      $this->Session->setFlash(
       __('The user could not be saved. Please, try again.') 
      ); 
     } 
    } 
} 

AppController

<?php 
class AppController extends Controller { 
    public $components = array(
     'Session', 
     'Auth' => array(
      'loginRedirect' => array(
       'controller' => 'tins', 
       'action' => 'index' 
      ), 
      'logoutRedirect' => array(
       'controller' => 'pages', 
       'action' => 'display', 
       'home' 
      ), 
      'authenticate' => array(
       'Form' => array(
        'passwordHasher' => 'Blowfish' 
       ) 
      ) 
     ) 
    ); 

    public function beforeFilter() { 
     $this->Auth->allow('index', 'view','login'); 
    } 

} 

login.ctp

<div class="users form"> 
<?php echo $this->Session->flash('auth'); ?> 
<?php echo $this->Form->create('QuanTriVien'); ?> 
    <fieldset> 
     <legend> 
      <?php echo __('Please enter your username and password'); ?> 
     </legend> 
     <?php echo $this->Form->input('username'); 
     echo $this->Form->input('password'); 
    ?> 
    </fieldset> 
<?php echo $this->Form->end(__('Login')); ?> 
</div> 

型號:QuanTriVien.php

<?php 
App::uses('AppModel', 'Model'); 
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); 
class QuanTriVien extends AppModel { 
    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' 
      ) 
     ), 
     'role' => array(
      'valid' => array(
       'rule' => array('inList', array('admin', 'author')), 
       'message' => 'Please enter a valid role', 
       'allowEmpty' => false 
      ) 
     ) 
    ); 

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

} 

型號:AppModel.php

<?php 

App::uses('Model', 'Model'); 
class AppModel extends Model { 

public $components = array(
     'Session', 
     'Auth' => array(
      'loginRedirect' => array(
       'controller' => 'posts', 
       'action' => 'index' 
      ), 
      'logoutRedirect' => array(
       'controller' => 'pages', 
       'action' => 'display', 
       'home' 
      ), 
      'authenticate' => array(
       'Form' => array(
        'passwordHasher' => 'Blowfish' 
       ) 
      ) 
     ) 
    ); 

    public function beforeFilter() { 
     $this->Auth->allow('index', 'view'); 
    } 
} 

末:無效的用戶名或密碼,請重試 witd:ID :123 pass:123 - > $ 2a $ 10 $ y2RsvsN5N0COAdnAEhNeW.BYNTfqk.RBISReRHb.a12qrEKTYb6Ui

+0

默認模型Auth組件使用的是'User'。您需要將'userModel'配置爲'QuanTriVien' – tigrang

+0

我知道它,但我不知道它在哪裏:D –

+0

您使用的是什麼版本的CakePHP?總是很好,包括這一點。 – Dave

回答

1

添加userModel設置使用比「用戶」以外的型號爲您的登錄/用戶:

'authenticate' => array(
    'Form' => array(
     'passwordHasher' => 'Blowfish', 
     'userModel' => 'QuanTriVien' // <-- ADD THIS 
    ) 
) 

詳細here