1

首先,对不起我糟糕的英语。晚安/白天/下午(取决于您所在的位置) 抱歉,如果我问一些可以在这里搜索的内容,我搜索了,甚至找到了,但也许我不明白。我需要在我的控制器中检查身份验证,因此,我实现了一个主控制器并将我所有的真实控制器扩展到它。在我的主控制器中,我检查身份验证并做得很好,但是当我尝试重定向未经身份验证的用户时,它崩溃了!在网上搜索,我意识到“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

我以前怎么说“这就是巫术”!!!当我今天醒来时,打开我的电脑,测试控制器和 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 回答