简体   繁体   English

如何使用Zend framework2获得用户角色?

[英]How do I get the user role using Zend framework2?

I am using Zend Framework 2 and having trouble getting the user role. 我正在使用Zend Framework 2,但无法获得用户角色。 I am using zfc user and Zfc Rbac. 我正在使用zfc用户和Zfc Rbac。 The roles currently work however I would like to get the values to use in an if statement (in the controller). 这些角色目前可以工作,但是我想在if语句(在控制器中)中使用要使用的值。 What is the function to call these? 调用这些的功能是什么? I have the developer tool turned on so I can see the role assigned but can't figure out out to call it. 我已打开开发人员工具,因此我可以看到分配的角色,但无法弄清楚如何调用它。

Thanks Matt 谢谢马特

如果已进行设置,则可以在ZfcRbac中正确识别,则可以使用授权服务(ZfcRbac \\ Service \\ AuthorizationService)获取身份及其角色。

$authorizationService->getIdentity()->getRoles();

I was used the below code and was able to get the role of the logged in user. 我使用了下面的代码,并且能够获得登录用户的角色。

 $viewmodel = new ViewModel();
 $authorize = $this->getServiceLocator()->get('UserRbac\Identity\IdentityRoleProvider');
 $roles = $authorize->getIdentityRoles();
 echo $roles[0];
    $viewmodel->setVariable("roles", $roles);
return $viewmodel;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM