留言板

User roles in velocity template

Ishan Sahore,修改在11 年前。

User roles in velocity template

Junior Member 帖子: 56 加入日期: 12-4-26 最近的帖子
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
thumbnail
Harish Kumar,修改在11 年前。

RE: User roles in velocity template

Expert 帖子: 483 加入日期: 10-7-31 最近的帖子
Hi Ishan

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

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

HTH
Ishan Sahore,修改在11 年前。

RE: User roles in velocity template

Junior Member 帖子: 56 加入日期: 12-4-26 最近的帖子
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.
thumbnail
Harish Kumar,修改在11 年前。

RE: User roles in velocity template

Expert 帖子: 483 加入日期: 10-7-31 最近的帖子
Hi Ishan

It depends on your requirement. For example

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


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

HTH
Alex alex,修改在11 年前。

RE: User roles in velocity template

New Member 帖子: 13 加入日期: 12-10-16 最近的帖子
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.
thumbnail
Pinkesh Gandhi,修改在11 年前。

RE: User roles in velocity template

Junior Member 帖子: 99 加入日期: 12-1-27 最近的帖子
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.


 #
    # Input a comma delimited list of variables which are restricted from the
    # context in Velocity based Journal templates.
    #
    journal.template.velocity.restricted.variables=