Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Adam C
Checking user role in velocity template
30. Januar 2008 11:50
Antwort

Adam C

Rang: Junior Member

Nachrichten: 70

Eintrittsdatum: 13. Juni 2007

Neue Beiträge

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.
Jorge Ferrer
RE: Checking user role in velocity template
19. Februar 2008 09:00
Antwort

Jorge Ferrer

LIFERAY STAFF

Rang: Liferay Legend

Nachrichten: 2768

Eintrittsdatum: 31. August 2006

Neue Beiträge

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
RE: Checking user role in velocity template
4. November 2010 07:07
Antwort

Patrick Stackpoole

Rang: New Member

Nachrichten: 21

Eintrittsdatum: 10. Juni 2010

Neue Beiträge

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.

 1
 2
 3#set ($isAdmin = false)
 4
 5#if ( $is_signed_in )
 6
 7    #set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
 8    #set ($usrRoles = $rService.getUserRoles( $user_id ))
 9    #foreach( $usrRole in $usrRoles )
10            #if ( $usrRole.getName() == "Administrator" )
11            #set ($isAdmin = true)
12            #end
13#end
Jairo Luna
RE: Checking user role in velocity template
8. November 2010 16:46
Antwort

Jairo Luna

Rang: Junior Member

Nachrichten: 57

Eintrittsdatum: 21. Juli 2010

Neue Beiträge

May be you can use:

1
2$permissionChecker.isCompanyAdmin($company_id)
Ankit _
RE: Checking user role in velocity template
14. Februar 2013 06:34
Antwort

Ankit _

Rang: Junior Member

Nachrichten: 46

Eintrittsdatum: 18. Oktober 2012

Neue Beiträge

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
 1
 2#if ( $is_signed_in )
 3    #set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
 4    #set ($usrRoles = $rService.getUserRoles( $user_id ))
 5    #foreach( $usrRole in $usrRoles )
 6            #if ( $usrRole.getName() == "Administrator" )
 7                #dockbar()
 8            #end
 9    #end
10#end

and this also
1
2$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..
James Falkner
RE: Checking user role in velocity template
14. Februar 2013 08:23
Antwort

James Falkner

Community Moderator

Rang: Liferay Legend

Nachrichten: 1406

Eintrittsdatum: 17. September 2010

Neue Beiträge

Make sure you uncheck the 'cacheable' option when editing the template.
Ankit _
RE: Checking user role in velocity template
14. Februar 2013 21:32
Antwort

Ankit _

Rang: Junior Member

Nachrichten: 46

Eintrittsdatum: 18. Oktober 2012

Neue Beiträge

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
1#if ($permissionChecker.isOmniadmin())
2#dockbar()
3#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 _
RE: Checking user role in velocity template
14. Februar 2013 21:49
Antwort

Ankit _

Rang: Junior Member

Nachrichten: 46

Eintrittsdatum: 18. Oktober 2012

Neue Beiträge

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

1#if ($is_signed_in && $permissionChecker.isGroupAdmin($group_id))
2        #dockbar()
3    #end


Thanks,
Ankit
Devendra Patel
Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat
28. Februar 2013 21:30
Antwort

Devendra Patel

Rang: Junior Member

Nachrichten: 71

Eintrittsdatum: 25. September 2011

Neue Beiträge

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
Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat
28. Februar 2013 21:30
Antwort

Devendra Patel

Rang: Junior Member

Nachrichten: 71

Eintrittsdatum: 25. September 2011

Neue Beiträge

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
Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat
28. Februar 2013 21:30
Antwort

Devendra Patel

Rang: Junior Member

Nachrichten: 71

Eintrittsdatum: 25. September 2011

Neue Beiträge

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
Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat
28. Februar 2013 21:30
Antwort

Devendra Patel

Rang: Junior Member

Nachrichten: 71

Eintrittsdatum: 25. September 2011

Neue Beiträge

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
Re: [Liferay Forums][4. General] RE: Checking user role in velocity templat
28. Februar 2013 21:30
Antwort

Devendra Patel

Rang: Junior Member

Nachrichten: 71

Eintrittsdatum: 25. September 2011

Neue Beiträge

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
cacaca cacaca
RE: Checking user role in velocity template
8. Mai 2013 23:54
Antwort

cacaca cacaca

Rang: New Member

Nachrichten: 14

Eintrittsdatum: 12. April 2013

Neue Beiträge

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.

 1
 2
 3#set ($isAdmin = false)
 4
 5#if ( $is_signed_in )
 6
 7    #set ($rService = $serviceLocator.findService("com.liferay.portal.service.RoleService"))
 8    #set ($usrRoles = $rService.getUserRoles( $user_id ))
 9    #foreach( $usrRole in $usrRoles )
10            #if ( $usrRole.getName() == "Administrator" )
11            #set ($isAdmin = true)
12            #end
13#end
Fernando Fernandez
RE: Checking user role in velocity template
3. November 2014 12:24
Antwort

Fernando Fernandez

Rang: Junior Member

Nachrichten: 56

Eintrittsdatum: 22. August 2007

Neue Beiträge

BTW, if somebody wants to check Site Roles also, you have to add something like:
 1
 2#set ($rService = $serviceLocator.findService("com.liferay.portal.service.UserGroupRoleLocalService"))
 3#set ($usrRoles = $rService.getUserGroupRoles( $user.getUserId() ) )
 4#foreach( $usrRole in $usrRoles )
 5    #if($usrRole.getRole().getName() == "SiteRoleOne" || $usrRole.getRole().getName() == "SiteRoleTwo")
 6        #set ($isAdmin = true)
 7    #end
 8    #set ($userNameAndRoles = $userNameAndRoles+" "+$usrRole.getRole().getName())
 9#end
Achmed Tyrannus Albab
RE: Checking user role in velocity template
29. Februar 2016 02:09
Antwort

Achmed Tyrannus Albab

Rang: Regular Member

Nachrichten: 118

Eintrittsdatum: 5. März 2010

Neue Beiträge

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.

1       
2<#assign usrRoles = user.getRoles()>
3<#list usrRoles as usrRole>
4    <#if usrRole.getName() == "Administrator">
5        //Roll some .. dough
6    </#if>
7</#list>