2016-10-15 9 views
0

Cakephp 3.3 및 BeforeFilter/Auth 리디렉션 문제에 대해 누구든지 이해할 수 있습니다.CakePHP3 BeforeFilter & Auth Redirect

기본 인증 구성 요소를 사용하고 있습니다. 세션 변수 (등록)를 추가로 확인하고 해당 변수가 설정되지 않은 경우 원하는 등록을 설정하도록 선택한 페이지로 리디렉션되는 사용자 지정 구성 요소를 만들었습니다. 내가 구성 요소 접근하기로 결정 그 이유는,

<?php 

namespace App\Controller\Component; 

use Cake\Controller\Component; 
use Cake\Network\Request; 


class RegistrationCheckComponent extends Component 
{ 

private $_allowedActions = []; 
private $_superUserBypass = false; 

public $components = ['Auth']; 

public function superUserBypass($val = false) { 
    $this->_superUserBypass = $val; 
} 

public function allow(Array $allowedActions = []) { 
    $this->_allowedActions = $allowedActions; 
} 

public function verify() { 

    if($this->_superUserBypass) { 
     return true; 
    } 

    $session = $this->request->session(); 
    //if Auth Registration is not set 
    if(!$session->read('Auth.Registration')) { 
     //if requested action is not in the array of allowed actions, redirect to select registration 
     if(!in_array($this->request->param('action'), $this->_allowedActions)) { 
      return $this->redirect(); 
     }; 
     return true; 
    } 
    return true; 

} 

public function redirect() { 
    $controller = $this->_registry->getController(); 
    return $controller->redirect($this->config('redirect')); 
} 

} 

는 모든 컨트롤러의 설정되는 등록 변수를 필요 :

여기 내 사용자 지정 구성 요소입니다.

등록 변수가, 나는 다음으로, beforeFilter 기능 포함 설정하는 데 필요한 컨트롤러에서
$this->loadComponent('RegistrationCheck', ['redirect' => ['controller' => 'Users', 'action' => 'registrations']]); 

: : 이제

public function beforeFilter(Event $event) { 
    parent::beforeFilter($event); 
    return $this->RegistrationCheck->verify(); 
} 

를, 내가 한 구성 요소는 그러나이 라인으로의 AppController에로드 일부 통합 테스트를 정의했다, 여기에 그들 중 하나입니다 : 내 RegistrationCheck 구성 요소를 구현 한 후

public function testUnauthenticatedEdit() 
{ 
    $this->get('/teams/edit'); 
    $this->assertRedirect(['controller' => 'Users', 'action' => 'login']); 
} 

그래서, 내가 통합 테스트를 실행했습니다. 나는 시험을 통과 할 것으로 기대하고 있었지만, 그렇지 않았습니다. 흥미로운 점은 내가 기대했던 것처럼 사용자 -> 로그인보다는 실제로 사용자 -> 등록으로 리다이렉트를 리턴한다는 것입니다.

Auth 구성 요소가 리디렉션되기 전에 RegistrationCheck 리디렉션이 진행 중입니다. Auth 세트가없는 등록으로의 리다이렉션은 로그인으로 리다이렉션을 끝낼 것이기 때문에 엄청난 거래라고 확신하지는 않지만 그것을 무시하는 것은 잘못된 것 같습니다 ... 또한, 좀 더 이해하고 싶습니다. 실제로 무슨 일이 일어나고 있는지.

누구나 Auth 구성 요소가 RegistrationCheck 구성 요소보다 먼저 처리되도록하는 코드 변경을 제안 할 수 있습니까?

미리 감사드립니다.

답변