如何在Module.php中获取当前连接的用户身份

时间:2013-05-29 14:03:17

标签: zend-framework2 acl

我已按照本教程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);

}

}

谢谢

1 个答案:

答案 0 :(得分:1)

MvcEvent可以访问ApplicationServiceManager可以访问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); } } ,所以......

{{1}}