Fórum

Checking user role in velocity template

Adam Causey, modificado 16 Anos atrás.

Checking user role in velocity template

Junior Member Postagens: 70 Data de Entrada: 13/06/07 Postagens Recentes
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, modificado 16 Anos atrás.

RE: Checking user role in velocity template

Liferay Legend Postagens: 2871 Data de Entrada: 31/08/06 Postagens Recentes
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, modificado 13 Anos atrás.

RE: Checking user role in velocity template

New Member Postagens: 21 Data de Entrada: 10/06/10 Postagens Recentes
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, modificado 10 Anos atrás.

RE: Checking user role in velocity template

New Member Postagens: 14 Data de Entrada: 12/04/13 Postagens Recentes
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, modificado 9 Anos atrás.

RE: Checking user role in velocity template

Expert Postagens: 396 Data de Entrada: 22/08/07 Postagens Recentes
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, modificado 8 Anos atrás.

RE: Checking user role in velocity template

Regular Member Postagens: 158 Data de Entrada: 05/03/10 Postagens Recentes
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, modificado 7 Anos atrás.

RE: Checking user role in velocity template

New Member Postagens: 17 Data de Entrada: 08/04/11 Postagens Recentes
Cleaner than VM, Nice! Thanks
Avinash Kashid, modificado 5 Anos atrás.

RE: Checking user role in velocity template

New Member Postagens: 5 Data de Entrada: 22/09/14 Postagens Recentes
Thanks. It help me.
thumbnail
Jairo Luna, modificado 13 Anos atrás.

RE: Checking user role in velocity template

Junior Member Postagens: 57 Data de Entrada: 21/07/10 Postagens Recentes
May be you can use:


$permissionChecker.isCompanyAdmin($company_id)
Ankit _, modificado 11 Anos atrás.

RE: Checking user role in velocity template

Junior Member Postagens: 46 Data de Entrada: 18/10/12 Postagens Recentes
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, modificado 11 Anos atrás.

RE: Checking user role in velocity template

Liferay Legend Postagens: 1399 Data de Entrada: 17/09/10 Postagens Recentes
Make sure you uncheck the 'cacheable' option when editing the template.
Ankit _, modificado 11 Anos atrás.

RE: Checking user role in velocity template

Junior Member Postagens: 46 Data de Entrada: 18/10/12 Postagens Recentes
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 _, modificado 11 Anos atrás.

RE: Checking user role in velocity template

Junior Member Postagens: 46 Data de Entrada: 18/10/12 Postagens Recentes
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, modificado 11 Anos atrás.

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

Junior Member Postagens: 71 Data de Entrada: 26/09/11 Postagens Recentes
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, modificado 11 Anos atrás.

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

Junior Member Postagens: 71 Data de Entrada: 26/09/11 Postagens Recentes
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, modificado 11 Anos atrás.

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

Junior Member Postagens: 71 Data de Entrada: 26/09/11 Postagens Recentes
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, modificado 11 Anos atrás.

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

Junior Member Postagens: 71 Data de Entrada: 26/09/11 Postagens Recentes
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, modificado 11 Anos atrás.

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

Junior Member Postagens: 71 Data de Entrada: 26/09/11 Postagens Recentes
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