6

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.

Henrik Paul
  • 66,919
  • 31
  • 85
  • 96

3 Answers3

8

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();
}
AdrianRM
  • 2,622
  • 2
  • 25
  • 42
  • 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.... – Prem Mar 20 '12 at 07:23
  • You can utilize `UserLocalServiceUtil.getRoleUsers(roleId)` – Pankaj Kathiriya Apr 21 '13 at 07:41
7

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.

Dirk
  • 1,893
  • 1
  • 19
  • 26
1

The roles of an Organizations are stored in the table UserGroupRole, so if you want to get the owner of an Organization you must use the following code:

boolean isOrgOwner = 
    UserGroupRoleLocalServiceUtil.hasUserGroupRole(
        usr.getUserId(), 
        this.currentOrganization.getGroupId(), 
        RoleConstants.ORGANIZATION_OWNER);

If you want to retrieve all the Organization Owners of an organization:

List<User> administrators = new LinkedList<>();

List<UserGroupRole> allOrganizationAdministrators = 
    UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
        this.currentOrganization.getGroupId(), roleId);

for (UserGroupRole userGroupRoleTemp : allOrganizationAdministrators) {
    administrators.add(userGroupRoleTemp.getUser());
}

Cheers!