2016-10-15 5 views
0

Cakephp 3.3とBeforeFilter/Authリダイレクトの問題については誰もが理解できます。CakePHP3 BeforeFilter&Authリダイレクト

私はデフォルトの認証コンポーネントを使用しています。私は、セッション変数(Registration)をさらにチェックするカスタムコンポーネントを作成し、その変数が設定されていない場合、目的のRegistrationを設定するように設計されたページにリダイレクトします。ここで

は私のカスタムコンポーネントです:私は、コンポーネントのアプローチで行くことに決めた理由

<?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']]); 

:コンポーネントは、しかし、このラインでのAppControllerにロードされている今

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

、私はしました定義されたいくつかの統合テストを持っていた、ここではそれらの一つだ:私は私のRegistrationCheck部品を実装した後

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

だから、私は、統合テストを実行しました。私はテストが合格することを期待していた、それはしなかった。興味深いのは、私が期待していたように、Users->ログインではなく、Users-> Registrationへのリダイレクトを実際に返したことです。

Authコンポーネントがリダイレクトされる前に、RegistrationCheckリダイレクトが実行されているように見えます。 Authの設定がない登録へのリダイレクトはログインにリダイレクトされることになるので、それは大したことではありませんが、それを無視するのは間違っているようです...また、もう少し理解したいと思います実際に何が起こっているのか

AuthコンポーネントがRegistrationCheckコンポーネントの前に確実に処理されるように、誰でも自分のコードに変更を提案できますか?

ありがとうございます。

答えて

関連する問題