Getting all users with a Role in Liferay

14,941

Solution 1

The easiest way to access liferays own objects is by using the XXXServiceUtil classes (e.g. RoleServiceUtil.getUserRoles(userId)). Thus you rarely have to deal with any SQL directly. Either the RoleServiceUtil or UserServiceUtil might have what you need.

Solution 2

You don't need a DynamicQuery. These are the methods you are looking for in the classes that Dirk points out:

long[] UserServiceUtil.getRoleUserIds(long roleId)

or

long[] UserLocalServiceUtil.getRoleUserIds(long roleId)
List<User> UserLocalServiceUtil.getRoleUsers(long roleId)

Remember that the methods in the classes XXXLocalServiceUtil are not checking the permissions of the current user.

EDIT: If you are looking for all users with a given role within a given community:

long companyId= _X_; //Perhaps CompanyThreadLocal.getCompanyId() if you don't have it anywhere else?
Role role=RoleLocalServiceUtil.getRole(companyId, "Example Role");
Group group=GroupLocalServiceUtil.getGroup(companyId, "Example Community");
List<UserGroupRole> userGroupRoles = UserGroupRoleLocalServiceUtil.
                       getUserGroupRolesByGroupAndRole(groupId, role.getRoleId());
for(UserGroupRole userGroupRole:userGroupRoles){
    User oneUser=userGroupRole.getUser();
}
Share:
14,941
Henrik Paul
Author by

Henrik Paul

Currently programming mainly in PHP in the spare time, but writing Java for a living. Started writing 5-liners of BASIC on a C64 when I was about 6, and hooked in programming ever since.

Updated on July 12, 2022

Comments

  • Henrik Paul
    Henrik Paul almost 2 years

    I'm new to Liferay development in general, so feel free to point out if I'm going about stuff totally the wrong way.

    I'm trying to get a DynamicQuery object of all users within a certain group (I'll use this object to further filter another query I'll do against the message board). The User interface seems to have a roleIds property that I might be able to use, since I already know the roleId I'm interested in. But I can't find the proper way to query if roleIds contains a certain value.

    Any ideas on what I want to do?

    PS: I would have the exact SQL query I could ask directly, but I'd rather use Liferay's own connection pool, without needing to do some weird ext project thingy.

  • Prem
    Prem about 12 years
    I want to get the Users who has assigned specific Organization Roles, i got the Role id of the Role but not able to get the USers associated with that Role id. I also got that the entries are inside the USerGroupTable. Can you please help me....
  • Pankaj Kathiriya
    Pankaj Kathiriya about 11 years
    You can utilize UserLocalServiceUtil.getRoleUsers(roleId)