Foren

Velocity - getUserRoles( $user_id ) doesn't work with Power User

Alex alex, geändert vor 11 Jahren.

Velocity - getUserRoles( $user_id ) doesn't work with Power User

New Member Beiträge: 13 Beitrittsdatum: 16.10.12 Neueste Beiträge
Hi,
I'm trying to modify the portal-normal.vm in order to make a logo clickable or not according to the current user role.
I found that the code for getting the Current User role is like the follow:
#set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
#set ($usrRoles = $rService.getUserRoles( $user_id ))
#foreach( $usrRole in $usrRoles )
#If ($usrRole.getName() == "MyRole")
do something
#end
#end

The issues i'm facing is this:
1) When i enter the portal as Administrator the $usrRoles is full with the Administrator's roles
2) When i enter the portal as normal (PowerUser, User) the $usrRoles variable is wholly EMPTY.

In this way i cannot check if a normal user1 has the role1 (so he can click on the logo) and if a normal user2 has the role2 (and he cannot click on the logo).
Why this works just for Administrator role?
Is there anything i missed with code?
Thanks a lot
Alex alex, geändert vor 11 Jahren.

RE: Velocity - getUserRoles( $user_id ) doesn't work with Power User

New Member Beiträge: 13 Beitrittsdatum: 16.10.12 Neueste Beiträge
Can anylone help me please?
thumbnail
tarun Sharma, geändert vor 11 Jahren.

RE: Velocity - getUserRoles( $user_id ) doesn't work with Power User

New Member Beiträge: 2 Beitrittsdatum: 17.09.10 Neueste Beiträge
Hello Alex,

Can you try below code to get user roles.I have used in 6.1 to fetch userRoles in theme.


#set ($usrRoles = $user.getRoles())
#foreach( $usrRole in $usrRoles )
#If ($usrRole.getName() == "MyRole")
do something
#end
#end
Alex alex, geändert vor 11 Jahren.

RE: Velocity - getUserRoles( $user_id ) doesn't work with Power User

New Member Beiträge: 13 Beitrittsdatum: 16.10.12 Neueste Beiträge
Really thanks, it seems to work... So getUserRoles... is an administration function... while $user.getRoles() is the right method to call...
Thanks a lot.
thumbnail
Achmed Tyrannus Albab, geändert vor 8 Jahren.

RE: Velocity - getUserRoles( $user_id ) doesn't work with Power User

Regular Member Beiträge: 158 Beitrittsdatum: 05.03.10 Neueste Beiträge
In case anyone was looking for a way to do it in FreeMarker, this is how i did it.
If anyone else could simplify and make it better, please do. Thanks.

		
<#assign usrRoles = user.getRoles()>
<#list usrRoles as usrRole>
	<#if usrRole.getName() == "Administrator">
		//Roll some .. dough
	<!--#if--> 
<!--#list-->