Fórumok

Checking user role in velocity template

Adam Causey, módosítva 16 év-val korábban

Checking user role in velocity template

Junior Member Bejegyzések: 70 Csatlakozás dátuma: 2007.06.13. Legújabb bejegyzések
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, módosítva 16 év-val korábban

RE: Checking user role in velocity template

Liferay Legend Bejegyzések: 2871 Csatlakozás dátuma: 2006.08.31. Legújabb bejegyzések
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, módosítva 13 év-val korábban

RE: Checking user role in velocity template

New Member Bejegyzések: 21 Csatlakozás dátuma: 2010.06.10. Legújabb bejegyzések
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, módosítva 10 év-val korábban

RE: Checking user role in velocity template

New Member Bejegyzések: 14 Csatlakozás dátuma: 2013.04.12. Legújabb bejegyzések
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, módosítva 9 év-val korábban

RE: Checking user role in velocity template

Expert Bejegyzések: 396 Csatlakozás dátuma: 2007.08.22. Legújabb bejegyzések
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, módosítva 8 év-val korábban

RE: Checking user role in velocity template

Regular Member Bejegyzések: 158 Csatlakozás dátuma: 2010.03.05. Legújabb bejegyzések
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, módosítva 7 év-val korábban

RE: Checking user role in velocity template

New Member Bejegyzések: 17 Csatlakozás dátuma: 2011.04.08. Legújabb bejegyzések
Cleaner than VM, Nice! Thanks
Avinash Kashid, módosítva 5 év-val korábban

RE: Checking user role in velocity template

New Member Bejegyzések: 5 Csatlakozás dátuma: 2014.09.22. Legújabb bejegyzések
Thanks. It help me.
thumbnail
Jairo Luna, módosítva 13 év-val korábban

RE: Checking user role in velocity template

Junior Member Bejegyzések: 57 Csatlakozás dátuma: 2010.07.21. Legújabb bejegyzések
May be you can use:


$permissionChecker.isCompanyAdmin($company_id)
Ankit _, módosítva 11 év-val korábban

RE: Checking user role in velocity template

Junior Member Bejegyzések: 46 Csatlakozás dátuma: 2012.10.18. Legújabb bejegyzések
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, módosítva 11 év-val korábban

RE: Checking user role in velocity template

Liferay Legend Bejegyzések: 1399 Csatlakozás dátuma: 2010.09.17. Legújabb bejegyzések
Make sure you uncheck the 'cacheable' option when editing the template.
Ankit _, módosítva 11 év-val korábban

RE: Checking user role in velocity template

Junior Member Bejegyzések: 46 Csatlakozás dátuma: 2012.10.18. Legújabb bejegyzések
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 _, módosítva 11 év-val korábban

RE: Checking user role in velocity template

Junior Member Bejegyzések: 46 Csatlakozás dátuma: 2012.10.18. Legújabb bejegyzések
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, módosítva 11 év-val korábban

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

Junior Member Bejegyzések: 71 Csatlakozás dátuma: 2011.09.26. Legújabb bejegyzések
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, módosítva 11 év-val korábban

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

Junior Member Bejegyzések: 71 Csatlakozás dátuma: 2011.09.26. Legújabb bejegyzések
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, módosítva 11 év-val korábban

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

Junior Member Bejegyzések: 71 Csatlakozás dátuma: 2011.09.26. Legújabb bejegyzések
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, módosítva 11 év-val korábban

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

Junior Member Bejegyzések: 71 Csatlakozás dátuma: 2011.09.26. Legújabb bejegyzések
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, módosítva 11 év-val korábban

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

Junior Member Bejegyzések: 71 Csatlakozás dátuma: 2011.09.26. Legújabb bejegyzések
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