1

まず、下手な英語で申し訳ありません。おやすみ/昼/午後 (お住まいの地域によって異なります) ここで検索できることをお尋ねしたら申し訳ありません。コントローラーで認証を確認する必要があるため、マスター コントローラーを実装し、すべての実際のコントローラーをそれに拡張します。マスター コントローラーで認証を確認し、うまく行っていますが、認証されていないユーザーをリダイレクトしようとするとクラッシュします。Webで検索すると、「init、preDispatchなど」のメソッドは存在せず、「construct」メソッドだけであることに気付いたので、試してみましたが、constructにはイベントマネージャーがないため、ここで停止します。 ..これは私のコードです:

public function __construct(){
    $r = new SimpleRouteStack();
    $r->addRoute('logoff', Literal::factory(array(
                                                'route'=>'/suporte/logoff',
                                                'defaults' => array(
                                                    'action'     => 'logoff',
                                                  'controller' => 'Suporte\Controller\Index',
                                                )
                                        )
                            )
    );
    $e = new MvcEvent();
    $e->setRouter($r);
    $this->setEvent($e);
    $this->getEvent()->setResponse(new Response());
    $this->getEventManager()->attach('*',array($this,'mvcPreDispatch'),100);

public function mvcPreDispatch(){
    $uri = explode('/',substr($_SERVER['REQUEST_URI'],1));
    $uri[2] = !isset($uri[2]) ? "index" : $uri[2];
    $auth = new AuthenticationService();
    $identity = $auth->getStorage()->read();
    $acl = $identity[2];

    if (!$auth->hasIdentity()){                                     
        $this->redirect()->toRoute('suporte/logoff');
    }elseif (   !$acl->hasResource($uri[0].'/'.$uri[1])             
                                        ||                          
                !$acl->isAllowed($identity[1],                      
                                $uri[0].'/'.$uri[1],                
                                $uri[2]                             
                            ) 
            )
                $this->redirect()->toRoute('logoff');
        else{
            /* $this->layout()->id = $identity[0]->getId();
            $this->layout()->nome = $identity[0]->getNome();
            $this->layout()->cargo = $identity[0]->getCargo(); */
            echo "permitido";
            //$this->layout()->recursos = $this->acl->getResources();
        }
}
4

2 に答える 2

0

Suporte\Controller\logoffマスタークラスも拡張します。はいの場合、無限ループになりますindexController>masterController(!loggedin)>logoutController>masterController(!loggedin)>logoutController>..

于 2012-10-03T06:11:26.000 に答える
0

「ITは魔術だ」とよく言ったものです!!! 今日起きたら、コンピューターの電源を入れて、コントローラーと BAZINGA をテストしてください!!! 正常に動作しています...なぜですか?質問する勇気はありません...私のコードは次のとおりです。

abstract class PadraoControllerSuporte extends AbstractActionController{
public function setEventManager(EventManagerInterface $events){
parent::setEventManager($events);
$controller = $this;
$events->attach('dispatch', function ($e) use ($controller) {
    if (is_callable(array($controller, 'verificaAuth'))){
        call_user_func(array($controller, 'verificaAuth'));
    }
}, 100);

}

public function verificaAuth(){
    $uri = explode('/',substr($_SERVER['REQUEST_URI'],1));
    $uri[2] = !isset($uri[2]) ? "index" : $uri[2];
    $auth = new AuthenticationService();
    $identity = $auth->getStorage()->read();
    $acl = $identity[2];
    if (!$auth->hasIdentity())                                      
        $this->redirect()->toRoute('suporte/logoff');
    elseif (    !$acl->hasResource($uri[0].'/'.$uri[1])                                 !$acl->isAllowed(   $identity[1],
                                    $uri[0].'/'.$uri[1],            
                                    $uri[2]                         
                                ) 
            )
                $this->redirect()->toRoute('logoff');
        else{
            /* $this->layout()->id = $identity[0]->getId();
            $this->layout()->nome = $identity[0]->getNome();
            $this->layout()->cargo = $identity[0]->getCargo(); */
            echo "permitido";
            //$this->layout()->recursos = $this->acl->getResources();
        }
}

私が理解したこと:「setEventManager」が重畳されたと思うので、「dispatch」イベントをリッスンし、verificaAuth関数を呼び出して(認証されていない場合)ログオフまたは認証を許可するようにリダイレクトします。これが役に立つことを願っています...すべてに感謝します!!!

于 2012-10-03T12:47:19.053 に答える