2

如何在 Liferay 主题中获取登录用户的角色?如何检查用户是否属于特定角色?

4

2 回答 2

2

UserLocalService具有hasRoleUser可用于确定 User 是否属于特定角色的方法。下面的代码可以放在navigation.vm文件templates夹下的文件中。

#set($UserLocalServiceUtil = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#if ($UserLocalServiceUtil.hasRoleUser(roleID, $user.getUserId())) // It takes roleID as input to check.
  //Proceed with whatever you want to 
#else
  //Proceed with something else

注意:com.liferay.portal.service.UserLocalService如果您使用,而不是com.liferay.portal.service.UserLocalServiceUtil, 可能会在某些类似资源中找到,那么您将遇到以下异常,

ERROR com.liferay.portal.kernel.bean.BeanLocatorException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
  bean named 'com.liferay.portal.service.UserLocalServiceUtil' is defined
  com.liferay.portal.kernel.bean.BeanLocatorException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.liferay.portal.servi
ce.UserLocalServiceUtil' is defined

另一种方式是,

#set($role=$serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))
$role.getUserRoles($user_id)
于 2013-07-01T11:01:03.863 回答
2

只需遍历 init.vm 中定义的 $user 对象

#set ($user_roles = $user.getRoles())
#foreach($role in $user_roles)
   $role.name<br />
#end
于 2013-07-28T07:18:36.393 回答