2012-11-20 2 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

양식 설정 에서 "username"을 (를) 다시 매핑 하시겠습니까? 로그인 양식에 필드 이름으로 "username"이 여전히 있습니까?

다시 매핑을 떨어 뜨리거나 첫 번째가 수행해야 데이터베이스 필드 이름 것 ​​같다 있기 때문에 "User.login" 로 형태로 필드를 변경 :

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

을 당신은에서 을 변경해야합니다 :

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

하거나하기 - : -

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

에 이 라인을 완전히 버리십시오. 정의 된 기본 AuthComponent 값을 취합니다.