Domanda

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?

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top