Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
How to check the role of a user through API
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
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
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
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
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
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
Attachments:
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
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
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
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
Hi Suresh,
I think this is the method you are looking for:
It should find all community roles of the group that you specify. I found it used in AdvancedPermissionChecker.java
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
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;
}
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;
}
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.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™