1

we want to check in a Liferay-Theme for 6.1.1 GA2, if the currently signed in user has a role e.g. "myRole".

The role is defined and assigned to the user, by adding the user to a group where this role is assigned.

When i check the user roles in control panel, the group is shown correctly for that user in the user list. Editing the user, the Roles-Panel shows in "Inherited Roles" the assigned group.

In theme a simple

#set ($usrRoles = $user.getRoles())
#foreach( $usrRole in $usrRoles )
    <p>$usrRole.getName()</p>
#end

only returns the directly assigned roles not the from group inherited roles.

Is there another method to get these roles? Or is this just one more bug in Liferay?

kidata
  • 165
  • 1
  • 2
  • 15

1 Answers1

2

If you are using liferay usergroup you can retrieve associated roles through UserGroupRoleLocalService.

Add this code after your:

#set($userGroupRoleLocalService= $serviceLocator.findService("com.liferay.portal.service.UserGroupRoleLocalService"))

#set($ugUsrRoles = $userGroupRoleLocalService.getUserGroupRoles($user.getUserId()))

#foreach( $ugUsrRole in $ugUsrRoles )
    <p>$ugUsrRole.getRole().getName()</p>
#end

This is an interesting list of all the liferay objects available in velocity: Access Objects from Velocity

Sticcio
  • 196
  • 5
  • Thx! The list of Velocity Objects is very useful - but where i can find the methods of these objects? I guess only in the source docs? – kidata Sep 26 '13 at 12:26
  • 1
    yes, unfortunately the only documentation is the source code or javadoc. I usually take the java class which corresponds to the velocity variable and then try its methods in my IDE – Sticcio Sep 26 '13 at 13:13