如何在Liferay主题中获得角色?

时间:2013-07-01 10:57:05

标签: liferay liferay-6 liferay-theme liferay-velocity

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

2 个答案:

答案 0 :(得分:2)

UserLocalServicehasRoleUser方法,可用于查明用户是否属于某个特定角色。 下面的代码可以放在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,   正如在this等资源中可能会发现的那样,您将遇到以下异常,

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)

答案 1 :(得分:2)

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

#set ($user_roles = $user.getRoles())
#foreach($role in $user_roles)
   $role.name<br />
#end