Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Morad Ahmad
How to check if a user has a role within current community/site (Liferay 6.
January 9, 2012 12:04 PM
Answer

Morad Ahmad

Rank: Junior Member

Posts: 29

Join Date: June 16, 2010

Recent Posts

I defined a community role "SiteContentManager" which allows users to add specific content to a site, where they have this role. I need to check if the user has this role in a portlet. I tried different ways without success:

1. List<Role> userGroupRoles = RoleServiceUtil.getUserGroupRoles(user.getUserId(), communityId);
if (userGroupRoles != null) {
for (Role role : userGroupRoles) {
System.out.println(role.getName());
if (role.getName().equals(SITE_ADMIN_ROLE))
return true;
}
}

2. groupRoles = RoleLocalServiceUtil.getGroupRoles(communityId);
System.out.println("group roles: ");
if (groupRoles != null) {...}

3. RoleServiceUtil.hasUserRole(user.getUserId(), communityId, SITE_ADMIN_ROLE, true);

???

Thanks.
Jitendra Rajput
RE: How to check if a user has a role within current community/site (Lifera
January 9, 2012 9:20 PM
Answer

Jitendra Rajput

Rank: Liferay Master

Posts: 647

Join Date: January 7, 2011

Recent Posts

Have you tried by passing false in second approach .?

1RoleServiceUtil.hasUserRole(user.getUserId(), communityId, SITE_ADMIN_ROLE, false);


Similar kind of problem encountered in below thread ..look into it ..May help you.

http://www.liferay.com/community/forums/-/message_boards/message/4002510
Ricardo Piccoli
RE: How to check if a user has a role within current community/site (Lifera
March 12, 2013 7:43 AM
Answer

Ricardo Piccoli

Rank: New Member

Posts: 6

Join Date: February 14, 2013

Recent Posts

Any success?
Ricardo Piccoli
RE: How to check if a user has a role within current community/site (Lifera
March 13, 2013 11:15 AM
Answer

Ricardo Piccoli

Rank: New Member

Posts: 6

Join Date: February 14, 2013

Recent Posts

I was able to do that using:

UserGroupRoleLocalServiceUtil.hasUserGroupRole(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), "my-custom-role", true);

or

UserGroupRoleLocalServiceUtil.hasUserGroupRole(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), RoleConstants.SITE_ADMINISTRATOR, true);

This last "true", is because I don't add users but groups to sites.