0

我已按照本教程http://ivangospodinow.com/zend-framework-2-acl-setup-in-5-minutes-tutorial/在我的项目中解决 Acl。我必须修改我的 Module.php,然后我必须请求当前连接用户的角色。

所以我的问题是,如何在我的 Module.php 中获取当前连接用户的身份?

我想做类似的事情:

$userRole = $this->getServiceLocator()->get('AuthService')->getStorage()->read()->statut

我的实际代码:

public function checkAcl(MvcEvent $e) {
$route = $e -> getRouteMatch() -> getMatchedRouteName();
///////// current connected user's role
$userRole = 'guest';

if (!$e -> getViewModel() -> acl -> isAllowed($userRole, $route)) {
    $response = $e -> getResponse();
    //location to page or what ever
    $response -> getHeaders() -> addHeaderLine('Location', $e -> getRequest() -> getBaseUrl() . '/404');
    $response -> setStatusCode(303);

}

}

谢谢

4

1 回答 1

1

MvcEvent 可以访问Application,而后者又可以访问ServiceManager,所以...

public function checkAcl(MvcEvent $e) {
    $route = $e -> getRouteMatch() -> getMatchedRouteName();
    ///////// current connected user's role
    // get the main app
    $app = $e->getApplication();
    // get the service manager 
    $sm = $app->getServiceManager();
    // get role from auth service
    $userRole = $sm->get('AuthService')->getStorage()->read()->statut

    if (!$e -> getViewModel() -> acl -> isAllowed($userRole, $route)) {
        $response = $e -> getResponse();
        //location to page or what ever
        $response -> getHeaders() -> addHeaderLine('Location', $e -> getRequest() -> getBaseUrl() . '/404');
        $response -> setStatusCode(303);

    }
}
于 2013-05-29T14:08:24.797 回答