Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Ishan Sahore
User roles in velocity template
September 14, 2012 12:00 AM
Answer

Ishan Sahore

Rank: Junior Member

Posts: 56

Join Date: April 25, 2012

Recent Posts

Hi all,

How can we get the current logged in user's role in velocity variables?
Role may be single or may be multiple for a user.


Any help is appreciated.


Thanks & Regards,
Ishan Sahore
Harish Kumar
RE: User roles in velocity template
September 14, 2012 12:33 AM
Answer

Harish Kumar

Rank: Expert

Posts: 337

Join Date: July 31, 2010

Recent Posts

Hi Ishan

#set($roleLocalService=$serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))

Now make a call to $roleLocalService.getUserRoles($userId)

HTH
Pinkesh Gandhi
RE: User roles in velocity template
September 14, 2012 1:09 AM
Answer

Pinkesh Gandhi

Rank: Regular Member

Posts: 100

Join Date: January 26, 2012

Recent Posts

If you want to use the above solution into your webcontent template, then please make sure the following property set blank into your porta-ext.properties, otherwise you could not be able to use $serviceLocator in your velocity template.

1
2 #
3    # Input a comma delimited list of variables which are restricted from the
4    # context in Velocity based Journal templates.
5    #
6    journal.template.velocity.restricted.variables=
Ishan Sahore
RE: User roles in velocity template
September 17, 2012 9:23 PM
Answer

Ishan Sahore

Rank: Junior Member

Posts: 56

Join Date: April 25, 2012

Recent Posts

Harish Kumar:
Hi Ishan

#set($roleLocalService=$serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))

Now make a call to $roleLocalService.getUserRoles($userId)

HTH



Hi Harish,

Thanks for your help.
Could you elaborate more, i.e. how to make a call to $roleLocalService.getUserRoles($userId) ?
I have to use this code in my portal_normal.vm file.
Based on the user's roles I have to show/hide the dockbar, etc.
Harish Kumar
RE: User roles in velocity template
September 17, 2012 9:59 PM
Answer

Harish Kumar

Rank: Expert

Posts: 337

Join Date: July 31, 2010

Recent Posts

Hi Ishan

It depends on your requirement. For example

1#set ($hasUserGroupRole = $userLocalService.hasUserGroupUser($userGroup.getUserGroupId(),$user.getUserId()))
2#if ($is_signed_in && $hasUserGroupRole))
3    #dockbar()
4#end


In the above code, dockbar will be visible only to logged in user that belongs to specific user group.

HTH
Alex alex
RE: User roles in velocity template
December 29, 2012 1:53 PM
Answer

Alex alex

Rank: New Member

Posts: 13

Join Date: October 16, 2012

Recent Posts

Hi, i have the same problem.
I tried this code in my portal_normal.vm :
#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

But this works only if i log in the portal as Administrator, the variable $usrRoles is wholly empty if i log in as normal user or power user.
Where am i wrong?
Thanks a lot.