2012-01-05 3 views
2

Auth 구성 요소를 사용하여 로그인 할 때 Cake 2.0.5에서 Cake는 모든 관련 모델을 검색하는 것처럼 보일 것입니다. 많은 협회에서 로그인하는 데 오랜 시간이 걸립니다.CakePHP 2.0.5 Auth 및 User Model 재귀적인 문제

이 문제는 this ticket에서 처음 발견되었지만 주어진 "솔루션"이 많이 의미하지는 않으며 설명서에서 다른 것을 찾을 수 없습니다.

2.0의 FormAuthenticate 클래스를 사용하면 적절하다고 판단되는 재귀 수준을 서브 클래스로 만들고 을 추가 할 수 있습니다.

경험이 있고 수정이 있습니까?

아래 - 샘플 코드 :

표준 로그인 방법 :

public function login() { 
    $this->User->recursive = -1; // does nothing 

    if ($this->Auth->login()) { 
     $this->redirect($this->Auth->redirect()); 
    } else { 
      $this->Session->setFlash('Invalid username or password.'); 
    } 

} 

그리고 쿼리 케이크 내 앱 생산 :

SELECT `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`created`, `User`.`modified`, `Band`.`id`, `Band`.`name`, `Band`.`genre`, `Band`.`location`, `Band`.`influences`, `Band`.`founded`, `Band`.`bio`, `Band`.`created`, `Band`.`modified`, `Band`.`status`, `Band`.`website`, `Band`.`email`, `Band`.`contact_number`, `Band`.`user_id`, `Member`.`id`, `Member`.`user_id`, `Member`.`first_name`, `Member`.`last_name`, `Member`.`display_name`, `Member`.`dob`, `Member`.`gender`, `Member`.`bio`, `Member`.`influences`, `Member`.`band_id` FROM `users` AS `User` LEFT JOIN `bands` AS `Band` ON (`Band`.`user_id` = `User`.`id`) LEFT JOIN `members` AS `Member` ON (`Member`.`user_id` = `User`.`id`) WHERE `User`.`username` = 'admin' AND `User`.`password` = 'dcec839a9258631138974cbccd81219f1d5dfcfa' LIMIT 1 

당신이 모든 필드를 검색하는 것 볼 수 있듯이, 모든 모델에 가입하십시오. 내 앱에는 2 개의 추가 연결 만 가능하지만 매우 복잡한 앱에서는 이것이 어떻게 문제가되는지 알 수 있습니다.

정말 언제나 ​​사용자 테이블이어야합니다. recursive 설정은 아무 것도하지 않는 것처럼 보입니다.

답변

1

Mark가 제안하는 것은 FormAuthenticate 클래스를 확장하거나 본질적으로 재정의하는 것입니다.

이 코드의 기본 구조 인 새 파일 app/Controller/Component/Auth/ExtendedFormAuthenticate.php

만들기 - 나는 재귀 수준이 _findUser 방법에 설정되어있는 중요한 비트에 남아했습니다

App::uses('FormAuthenticate', 'Controller/Component/Auth'); 

class ExtendedFormAuthenticate extends FormAuthenticate 
{ 
    public function authenicate(CakeRequest $request, CakeResponse $response) { 
    // foo 
    } 

    protected function _findUser($username, $password) 
    { 
    // bar 

     $result = ClassRegistry::init($userModel)->find('first', array(
      'conditions' => $conditions, 
      'recursive' => -1 
     )); 

    // fooBar 
    } 
} 

나는 많은 것을 가지고 Gist를 만들었습니다 : https://gist.github.com/1565672

오, 거의 잊어 버렸습니다. 확장 클래스를 사용하려면 AuthComponent를 설정해야합니다.

public $components = array(
    'Auth'=> array(
    'authenticate' => array(
     'ExtendedForm' 
    ) 
), 
); 
+0

완벽한 보인다; 내가 집에 갈 때 나는 그것을 시험해 볼 것이다. 케이크가이 기능을 처음 도입했을 때 유용하지 않은 것처럼 보였을뿐만 아니라 클래스를 "수정"하도록 확장해야하는 경우가 조금 이상합니다. 덕분에 – Ross

2

인증 구성 요소에 재귀 옵션을 사용할 수 있습니다.

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array('recursive' => -1) 
     ) 
    ) 

또는 에서

으로, beforeFilter 방법 :

$this->Auth->authenticate = array('Form' => array('recursive' => -1)); 
+0

이 작동하지 않습니다. – Connel