How to check the role of a user through API

1692364, modified 16 Years ago. Junior Member Posts: 47 Join Date: 12/3/08 Recent Posts
Hi,

I want to check whether the user has a role of "community administrator" using API.

I could get a method from RoleLocalServiceImpl.java

public boolean hasUserRole(
long userId, long companyId, String name, boolean inherited)

I have assigned a user as Community Administrator

I have tried checking

RoleLocalServiceUtil.hasUserRole(userId, companyId, "Community Administrator", false); which returned me false, which should be true.

Also I tried

RoleLocalServiceUtil.hasUserRole(userId, companyId, "Community Administrator", true); which also returned me false.

I observed that this works fine for Regular roles.

Can anyone look into this?

Regards
Suresh Midde
thumbnail
31821, modified 16 Years ago. Expert Posts: 416 Join Date: 5/15/07 Recent Posts
Hi,

Did you try with hasUserRole(long userId, long roleId) method in the same class? You will only need the userId and the role Id.

On the other hand, did you double check via web, that this user actually has this role?

good luck
1692364, modified 16 Years ago. Junior Member Posts: 47 Join Date: 12/3/08 Recent Posts
Hi ,

I had a weird problem,when I have verified my database table users_roles there was no mapping between my userid which I have associated with the role of "Community Administrator".

My userid has only mapping to the role ids of "Power User" and "User"

But from my application, I have assigned my user as Community Administrator.I dont know what is the problem?

Screenshot attached
thumbnail
31821, modified 16 Years ago. Expert Posts: 416 Join Date: 5/15/07 Recent Posts
I am not sure but I think role assignemetc can bi done both direct o indirectly.

I mean that a role can be assigned to a user personally, but it can be assigned throgh a user group, sommunity or so...

Which in database level mean that:

1case: relation is user -- user_roles -- roles

2 case: user -- users_usergroups -- usergrouprole


But double check it table ralation because I may be wrong

Hope it helps
1692364, modified 16 Years ago. Junior Member Posts: 47 Join Date: 12/3/08 Recent Posts
Hi ,

It seems that we cannot use the RoleLocalServiceImpl methods for retrieving Community Roles .

Instead, from my inspection, I found a way to do this.

Retrieve a list of roles associated with the user id using the method

UserGroupRoleLocalServiceUtil.getUserGroupRoles(userid) which returns you a list<userGroupRole>

like

List<UserGroupRoles> userGroupRoles = UserGroupRoleLocalServiceUtil.getUserGroupRoles(selUser.getUserId());


for (UserGroupRole userGroupRole : userGroupRoles) {
int roleType = userGroupRole.getRole().getType();

if (roleType == RoleConstants.TYPE_COMMUNITY) {
communityRoles.add(userGroupRole);
}
}

if(communityRoles != null) {
for(int j=0;j<communityRoles.size();j++){
UserGroupRole comm = (UserGroupRole)communityRoles.get(j);
// check for roleId that corresponds to the "Community Administrator"

comm.getRoleId()

}
}



If anyone can suggest me, a simpler one to achieve this, I will be happy

Regards
Suresh Midde
thumbnail
1395288, modified 16 Years ago. Liferay Legend Posts: 2047 Join Date: 10/7/08 Recent Posts
Hi Suresh,

I think this is the method you are looking for:

List<role> userGroupRoles = RoleLocalServiceUtil.getUserGroupRoles(userId, groupId);</role>


It should find all community roles of the group that you specify. I found it used in AdvancedPermissionChecker.java
3661460, modified 15 Years ago. New Member Post: 1 Join Date: 7/28/09 Recent Posts
Not sure if this is any simpler than the previous post but this is what I used.

public boolean checkIfUserIsAdmin(RenderRequest req) {
try {
User user = UserLocalServiceUtil.getUserById(Long.parseLong(req.getRemoteUser()));
List<Role> roles = user.getRoles();

for ( int i=0;i<roles.size();i++ ) {
if ( roles.get(i).getName().indexOf("Administrator") >= 0 ){
return true;
}
}
} catch(Exception err) {}
return false;
}
Lee Jordan, modified 5 Years ago. Expert Posts: 449 Join Date: 5/26/15 Recent Posts
Just commenting here because in trying to find a similar working situation in 2020 I'm being directed to threads from 2010. Liferay documentation is letting theme developers down badly.