2012-11-20 66 views
2

當我嘗試對其進行身份驗證時,它說Your username or password was incorrect.即使我剛創建用戶。當我嘗試debug($this->Auth->login())時,我收到消息false。我的代碼有什麼問題?CakePHP 2.2.3身份驗證錯誤

模型 - UserModel.php:

<?php 
App::uses('AppModel', 'Model', 'AuthComponent', 'Controller/Component'); 
/** 
* User Model 
* 
* @property Group $Group 
* @property WashMachine $WashMachine 
*/ 
class User extends AppModel { 

/** 
* Validation rules 
* 
* @var array 
*/ 
    public function beforeSave($options = array()) { 
     $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 
     return true; 
    } 

    public function bindNode($user) { 
     return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']); 
    } 

    public $validate = array(
     'username' => array(
      'notempty' => array(
       'rule' => array('notempty'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
     'name' => array(
      'notempty' => array(
       'rule' => array('notempty'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
     'email' => array(
      'email' => array(
       'rule' => array('email'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
     'password' => array(
      'notempty' => array(
       'rule' => array('notempty'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
     'group_id' => array(
      'numeric' => array(
       'rule' => array('numeric'), 
       //'message' => 'Your custom message here', 
       //'allowEmpty' => false, 
       //'required' => false, 
       //'last' => false, // Stop validation after this rule 
       //'on' => 'create', // Limit validation to 'create' or 'update' operations 
      ), 
     ), 
    ); 

    //The Associations below have been created with all possible keys, those that are not needed can be removed 

/** 
* belongsTo associations 
* 
* @var array 
*/ 
    public $belongsTo = array(
     'Group' => array(
      'className' => 'Group', 
      'foreignKey' => 'group_id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ) 
    ); 

/** 
* hasMany associations 
* 
* @var array 
*/ 
    public $hasMany = array(
     'WashMachine' => array(
      'className' => 'WashMachine', 
      'foreignKey' => 'user_id', 
      'dependent' => false, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'exclusive' => '', 
      'finderQuery' => '', 
      'counterQuery' => '' 
     ) 
    ); 

} 

的AppController.php

<?php 
/** 
* Application level Controller 
* 
* This file is application-wide controller file. You can put all 
* application-wide controller-related methods here. 
* 
* PHP 5 
* 
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) 
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) 
* 
* Licensed under The MIT License 
* Redistributions of files must retain the above copyright notice. 
* 
* @copyright  Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) 
* @link   http://cakephp.org CakePHP(tm) Project 
* @package  app.Controller 
* @since   CakePHP(tm) v 0.2.9 
* @license  MIT License (http://www.opensource.org/licenses/mit-license.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(
     'Acl', 
     'Auth' => array(
      'authorize' => array(
       'Actions' => array('actionPath' => 'controllers') 
      ) 
     ), 
     'Session' 
    ); 

    public $helpers = array('Html', 'Form', 'Session'); 

    function beforeFilter() { 

     //Configure AuthComponent 
     $this->Auth->authorize = array(
      'Controller', 
      'Actions' => array('actionPath' => 'controllers') 
     ); 
     $this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'login', 'password' => 'password'))); 
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false); 
     $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false); 
     $this->Auth->loginRedirect = array('controller' => 'products', 'action' => 'index', 'admin' => false, 'plugin' => false); 

    } 
    function isAuthorized($user) { 
     // return false; 
     return $this->Auth->loggedIn(); 
    } 

} 

的UserController.php

<?php 
App::uses('AppController', 'Controller'); 
/** 
* Users Controller 
* 
* @property User $User 
*/ 
class UsersController extends AppController { 
    function beforeFilter(){ 
     parent::beforeFilter(); 
     $this->Auth->allow('*'); 
    } 

    public function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       $this->redirect($this->Auth->redirect()); 
      } else { 
       $this->Session->setFlash('Your username or password was incorrect.'); 
      } 
     } 
     if ($this->Session->read('Auth.User')) { 
      $this->Session->setFlash('You are logged in!'); 
      $this->redirect('/', null, false); 
     } 
    } 

    public function logout() { 
     $this->Session->setFlash('Good-Bye'); 
     $this->redirect($this->Auth->logout()); 
    } 
/** 
* index method 
* 
* @return void 
*/ 
    public function index() { 
     $this->User->recursive = 0; 
     $this->set('users', $this->paginate()); 
    } 

/** 
* view method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function view($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     $this->set('user', $this->User->read(null, $id)); 
    } 

/** 
* add method 
* 
* @return void 
*/ 
    public function add() { 
     if ($this->request->is('post')) { 
      $this->User->create(); 
      if ($this->User->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.')); 
      } 
     } 
     $groups = $this->User->Group->find('list'); 
     $this->set(compact('groups')); 
    } 

/** 
* edit method 
* 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function edit($id = null) { 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->request->is('post') || $this->request->is('put')) { 
      if ($this->User->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.')); 
      } 
     } else { 
      $this->request->data = $this->User->read(null, $id); 
     } 
     $groups = $this->User->Group->find('list'); 
     $this->set(compact('groups')); 
    } 

/** 
* delete method 
* 
* @throws MethodNotAllowedException 
* @throws NotFoundException 
* @param string $id 
* @return void 
*/ 
    public function delete($id = null) { 
     if (!$this->request->is('post')) { 
      throw new MethodNotAllowedException(); 
     } 
     $this->User->id = $id; 
     if (!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 
     if ($this->User->delete()) { 
      $this->Session->setFlash(__('User deleted')); 
      $this->redirect(array('action' => 'index')); 
     } 
     $this->Session->setFlash(__('User was not deleted')); 
     $this->redirect(array('action' => 'index')); 
    } 
} 

的Login.cp視圖:

<h2>Login</h2> 
<?php 
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'login'))); 
echo $this->Form->input('User.username'); 
echo $this->Form->input('User.password'); 
echo $this->Form->end('Login'); 
?> 

回答

1

爲什麼你在表單設置 中重新映射「用戶名」登錄「,但你的登錄表單仍然包含」用戶名「作爲字段名稱?

落重映射或更改的領域在你的方式來「User.login」 因爲你似乎有一個數據庫字段名,第一要做到:

'Form' => array('fields' => array('username' => 'username', 'password' => 'password'))); 
0

你必須從改變 : -

$this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'login', 'password' => 'password'))); 

到: -

$this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'username', 'password' => 'password'))); 

,或者你ç完全放棄此行,因爲它將採用定義的默認AuthComponent值