留言板

Checking user role in velocity template

Adam Causey,修改在16 年前。

Checking user role in velocity template

Junior Member 帖子: 70 加入日期: 07-6-13 最近的帖子
How can I determine if the user is in a certain role using Velocity? More specifically, I'd like to check if they have the Administrator role.
thumbnail
Jorge Ferrer,修改在16 年前。

RE: Checking user role in velocity template

Liferay Legend 帖子: 2871 加入日期: 06-8-31 最近的帖子
Hi A C,

The following wiki article explains how to access Liferay's services from a velocity template in a theme:
http://wiki.liferay.com/index.php/Access_to_Liferay_services_in_Velocity


You can then use UserService.hasUserRole(..)
Patrick Stackpoole,修改在13 年前。

RE: Checking user role in velocity template

New Member 帖子: 21 加入日期: 10-6-10 最近的帖子
Although I'm replying to a post that's over 2 years old, I had the same question and wanted to post a solution I found that worked.



#set ($isAdmin = false)

#if ( $is_signed_in )

	#set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
	#set ($usrRoles = $rService.getUserRoles( $user_id ))
	#foreach( $usrRole in $usrRoles )
    		#if ( $usrRole.getName() == "Administrator" )
			#set ($isAdmin = true)
    		#end
#end

cacaca cacaca,修改在11 年前。

RE: Checking user role in velocity template

New Member 帖子: 14 加入日期: 13-4-12 最近的帖子
Patrick Stackpoole:
Although I'm replying to a post that's over 2 years old, I had the same question and wanted to post a solution I found that worked.



#set ($isAdmin = false)

#if ( $is_signed_in )

	#set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
	#set ($usrRoles = $rService.getUserRoles( $user_id ))
	#foreach( $usrRole in $usrRoles )
    		#if ( $usrRole.getName() == "Administrator" )
			#set ($isAdmin = true)
    		#end
#end

thumbnail
Fernando Fernandez,修改在9 年前。

RE: Checking user role in velocity template

Expert 帖子: 396 加入日期: 07-8-22 最近的帖子
BTW, if somebody wants to check Site Roles also, you have to add something like:

#set ($rService = $serviceLocator.findService("com.liferay.portal.service.UserGroupRoleLocalService"))
#set ($usrRoles = $rService.getUserGroupRoles( $user.getUserId() ) )
#foreach( $usrRole in $usrRoles )
	#if($usrRole.getRole().getName() == "SiteRoleOne" || $usrRole.getRole().getName() == "SiteRoleTwo")
		#set ($isAdmin = true)
	#end
	#set ($userNameAndRoles = $userNameAndRoles+" "+$usrRole.getRole().getName())
#end
thumbnail
Achmed Tyrannus Albab,修改在8 年前。

RE: Checking user role in velocity template

Regular Member 帖子: 158 加入日期: 10-3-5 最近的帖子
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-->			 			  			
thumbnail
Onochie Ojekwe,修改在7 年前。

RE: Checking user role in velocity template

New Member 帖子: 17 加入日期: 11-4-8 最近的帖子
Cleaner than VM, Nice! Thanks
Avinash Kashid,修改在5 年前。

RE: Checking user role in velocity template

New Member 帖子: 5 加入日期: 14-9-22 最近的帖子
Thanks. It help me.
thumbnail
Jairo Luna,修改在13 年前。

RE: Checking user role in velocity template

Junior Member 帖子: 57 加入日期: 10-7-21 最近的帖子
May be you can use:


$permissionChecker.isCompanyAdmin($company_id)
Ankit _,修改在11 年前。

RE: Checking user role in velocity template

Junior Member 帖子: 46 加入日期: 12-10-18 最近的帖子
Hi, this is old post, but i am still not getting the result that i want.

I am using liferay 6.2.0 M2 , and i created site and assign the owner and admin rights to that user. I just want if user is admin i want to show dockbar.
and i tried

#if ( $is_signed_in )
    #set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
    #set ($usrRoles = $rService.getUserRoles( $user_id ))
    #foreach( $usrRole in $usrRoles )
            #if ( $usrRole.getName() == "Administrator" )
            	#dockbar()
            #end
    #end
#end

and this also

$permissionChecker.isCompanyAdmin($company_id)

but both way i can't get my dockbar for that admin user, i can get dockbar for test user only..
thumbnail
James Falkner,修改在11 年前。

RE: Checking user role in velocity template

Liferay Legend 帖子: 1399 加入日期: 10-9-17 最近的帖子
Make sure you uncheck the 'cacheable' option when editing the template.
Ankit _,修改在11 年前。

RE: Checking user role in velocity template

Junior Member 帖子: 46 加入日期: 12-10-18 最近的帖子
Hi James,
Thanks for your reply.
James Falkner:
Make sure you uncheck the 'cacheable' option when editing the template.

but I am trying to do in theme, so i am writing it in navigation.vm file. I cleared all the server cache, but i can't figure it out.


EDIT :
I tried this one also
#if ($permissionChecker.isOmniadmin())
#dockbar()
#end

but still it getting only test user to show dockbar, not for site admin/other site owner(apply after site creation)

Thanks,
Ankit
Ankit _,修改在11 年前。

RE: Checking user role in velocity template

Junior Member 帖子: 46 加入日期: 12-10-18 最近的帖子
sorry for post my theme problem to velocity template thread,
mean while i got my solution for site admin can see dockbar throught theme by changing in portal_normal.vm as below

#if ($is_signed_in &amp;&amp; $permissionChecker.isGroupAdmin($group_id))
	    #dockbar()
	#end


Thanks,
Ankit
Devendra Patel,修改在11 年前。

Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat

Junior Member 帖子: 71 加入日期: 11-9-26 最近的帖子
Hi Ankit

Rather than using service locator for fetching you can use this code :

#foreach($role in $user.getRoles())
#if($role.getName() == "Administrator")
True
#end
#end

Regards,
Dev Patel
Devendra Patel,修改在11 年前。

Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat

Junior Member 帖子: 71 加入日期: 11-9-26 最近的帖子
Hi Ankit

Rather than using service locator for fetching you can use this code :

#foreach($role in $user.getRoles())
#if($role.getName() == "Administrator")
True
#end
#end

Regards,
Dev Patel
Devendra Patel,修改在11 年前。

Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat

Junior Member 帖子: 71 加入日期: 11-9-26 最近的帖子
Hi Ankit

Rather than using service locator for fetching you can use this code :

#foreach($role in $user.getRoles())
#if($role.getName() == "Administrator")
True
#end
#end

Regards,
Dev Patel
Devendra Patel,修改在11 年前。

Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat

Junior Member 帖子: 71 加入日期: 11-9-26 最近的帖子
Hi Ankit

Rather than using service locator for fetching you can use this code :

#foreach($role in $user.getRoles())
#if($role.getName() == "Administrator")
True
#end
#end

Regards,
Dev Patel
Devendra Patel,修改在11 年前。

Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat

Junior Member 帖子: 71 加入日期: 11-9-26 最近的帖子
Hi Ankit

Rather than using service locator for fetching you can use this code :

#foreach($role in $user.getRoles())
#if($role.getName() == "Administrator")
True
#end
#end

Regards,
Dev Patel