Getting all users with a Role in Liferay
Asked Answered
B

3

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.

Bullion answered 7/6, 2011 at 6:31 Comment(0)
M
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.

Microgroove answered 7/6, 2011 at 10:11 Comment(0)
D
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();
}
Dogmatist answered 28/6, 2011 at 16:45 Comment(2)
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....Foster
You can utilize UserLocalServiceUtil.getRoleUsers(roleId)Plumbum
M
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.

Microgroove answered 7/6, 2011 at 10:11 Comment(0)
O
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!

Outer answered 1/7, 2014 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.