留言板

UserLocalServiceUtil.getRoleUsers does not return related user group users

Balázs Csönge,修改在9 年前。

UserLocalServiceUtil.getRoleUsers does not return related user group users

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
Hi,
I using Liferay 6.2 CE.
I created 3 users.
I created one user group with user2 and user3.
I created a regular role and assigned user1 and user2 directly to it (under Roles/Assign Member/Users), then assigned the group to this role (under Roles/Assign Member/User Groups).

When a java code executing the following command
List<user> roleUsers = UserLocalServiceUtil.getRoleUsers(roleId);</user>

it returns only with 2 users (the directly assigned user1 and user2).

Is it a bug or there is a philosophy behind this behaviour?

Regards

Balázs
thumbnail
David H Nebinger,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Balázs Csönge:
Is it a bug or there is a philosophy behind this behaviour?


Neither. You are invoking the API asking for all users that have the role assigned directly, and that's what you're getting.

Evaluating roles inherited by group membership, org membership, etc. is much more complex than just getting the list of role users.
Balázs Csönge,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
So, if workflow engine working with role names, but liferay API doesn't return users which are related to that role through a user group - role relation, then we have to get the list of related user groups to the role, then have to get list of users in those groups.
I lost in the API, and cannot find which method of which class can return the list of user groups related to a specific role.
Please, can u tell me the answer to this?

If I know a user group name, then I can get user list of it in the following way:
Long companyId = Long.parseLong((String)execution.getVariable("companyId"));
				UserGroup ug = UserGroupLocalServiceUtil.getUserGroup(companyId,"howCanIGetThisNameBasedOnRoleNameOrRoleId");
				List<user> ugul = UserLocalServiceUtil.getUserGroupUsers(ug.getUserGroupId());</user>
thumbnail
David H Nebinger,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Workflow does not use the vanilla APIs the way you are.

UserLocalServiceUtil.getRoleUsers() just returns the list of users that directly have that role assigned. In Liferay you can get a role from many different places.

Workflow uses all of the correct places to determine the list of users that have the appropriate role.

Perhaps if you share your problem instead of what things you think you've found, we can help you figure out the solution instead of dancing around it.
Balázs Csönge,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
We use Activiti workflow engine integrated to Liferay CE 6.2 (downloaded from the liferay market).
The details of the integration are unknown for me, just have an idea, they are using the mentioned UserLocalServiceUtil.getRoleUsers() method, because a user task is not assigned to those users, who are related to the role via user group-role relation.

In our project they planned to relate users to user groups and user groups to roles, but they do not want to make direct user-role relations. In this case workflows are not working, becuse I have to give role name list for a user task. So I have to make a custom java code, which will have to get user group names based on the given role names, then get list of users based on list of user group names.

The question is, which method can return the list of user groups if I can pass role name or id as an input parameter?
thumbnail
David H Nebinger,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
The good news is that the code for the activiti plugin is available.

A cursory review of the code reveals the following:
public static List<user> findUsersByGroup(long companyId, String groupName) {
		// first - try to parse group to identify - it is regular group or org/community group
		String[] parsedName = groupName.split("/");
		List<com.liferay.portal.model.user> users = null;
		List<user> result = new ArrayList<user>();
		
		try {
			if (parsedName.length == 1 || Long.valueOf(parsedName[0]) == companyId) {
				if (parsedName.length &gt; 1) {
					groupName = parsedName[1];
					if (parsedName.length &gt; 2) {
						groupName = StringUtils.join(ArrayUtils.subarray(parsedName, 1, parsedName.length), "/");
					}
				}
				// regular group
				Role role = RoleLocalServiceUtil.getRole(companyId, groupName);
				users = UserLocalServiceUtil.getRoleUsers(role.getRoleId());
				
				for (com.liferay.portal.model.User user : users) {
					result.add(new UserImpl(user));
				}
			} else {
				long groupId = Long.valueOf(parsedName[0]);
				groupName = parsedName[1];
				
				if (parsedName.length &gt; 2) {
					groupName = StringUtils.join(ArrayUtils.subarray(parsedName, 1, parsedName.length), "/");
				}
				
				Role role = RoleLocalServiceUtil.getRole(companyId, groupName);
				List<usergrouprole> userRoles = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(groupId, role.getRoleId());
				
				for (UserGroupRole userRole : userRoles) {
					result.add(new UserImpl(userRole.getUser()));
				}
			}
		} catch (Exception ex) {
			_log.warn("Cannot get group users", ex);
		}
		
		return result;
	}</usergrouprole></user></user></com.liferay.portal.model.user></user>


So it looks like the user group role will be taken into account as long as the group name is in the right format...
Balázs Csönge,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
I am getting so depressed. Maybe because of my poor english knowledge, but I feel, I am asking a very exact question and cannot get an exact answer.
The code snipet does not contain all necessary information to solve the problem. For example, how do you mean "group name is in the right format"?

But start it again...
I have a role, named "Portal Content Reviwer".
I have a user group named "test", which is assigned to the mentioned role.
I have 2 users, which are assigned to the mentioned user group.
Step1.
I can get role via API based on its name.
				Long companyId = Long.parseLong((String)execution.getVariable("companyId"));
				Long roleId = RoleLocalServiceUtil.getRole(Long.parseLong((String)execution.getVariable("companyId")), "Portal Content Reviewer").getRoleId();

Step2. (the missing chain item)
I still not know how can get the list of user groups related to this role. (In my case the user group "test").
Step3.
After I can get a user group name, then I can list users related to it.
				UserGroup ug = UserGroupLocalServiceUtil.getUserGroup(companyId,"test");
				List<user> ugul = UserLocalServiceUtil.getUserGroupUsers(ug.getUserGroupId()); 
</user>


I tried the following for Step2:
				Long groupId = Long.parseLong((String)execution.getVariable("groupId"));
				List<usergrouprole> userRoles = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(groupId, roleId);
</usergrouprole>

The result was an empty list. So it means that groupId must be something totally different from what I should use at that method. Or I using not the right method.

The question still is, which method can return the list of user groups if I can pass role name or id as an input parameter?
Balázs Csönge,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us (答复)

Regular Member 帖子: 107 加入日期: 14-11-10 最近的帖子
I GOT IT!!!!!!! emoticon

All about the software reengeneering...emoticon
After the examination of liferay database tables and those content, I found which tables are "playing".
USER_ (it stores my 3 users)
ROLE_ (it stores my "Portal Content Reviewer" role)
USERS_ROLES (it stores direct relations between users and roles)
USERGROUP (it stores the user group definition)
USERS_USERGROUPS (it stores user group - user relations)
GROUP_ (it stores a magic record which references to the USERGROUP record, its CLASSPK field holds the id of the USERGROUP record)
GROUP_ROLES (it stores relations between roles and magic group records)

Based on these table names I started scanning the API class names and luckily found the mystic one. So the answer to my question is, if someone want to get user group list based on role name/id, then need 2 steps (instead one as I imagined).
- GroupLocalServiceUtil.getRoleGroups(roleId) returns list of magic groups records
- UserGroup ug = UserGroupLocalServiceUtil.getUserGroup(group.getClassPK()); returns a user group for 1 magic group record.

And the code for testing:
				Long companyId = Long.parseLong((String)execution.getVariable("companyId"));
				Long roleId = RoleLocalServiceUtil.getRole(Long.parseLong((String)execution.getVariable("companyId")), "Portal Content Reviewer").getRoleId();
				List<group> groups = GroupLocalServiceUtil.getRoleGroups(roleId);
				for (Group group: groups) {
					UserGroup ug = UserGroupLocalServiceUtil.getUserGroup(group.getClassPK());
					List<user> ugul = UserLocalServiceUtil.getUserGroupUsers(ug.getUserGroupId());
					ugul.size();
				}
</user></group>


And a final personal comment: Many days spent to figure out which class and which method should be used, just because API documentation contains ZERO descriptions about classes, methods and parameters. emoticon
thumbnail
David H Nebinger,修改在9 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Balázs Csönge:
I GOT IT!!!!!!! emoticon


Congrats!

And a final personal comment: Many days spent to figure out which class and which method should be used, just because API documentation contains ZERO descriptions about classes, methods and parameters. emoticon


Join the club emoticon
divya goyal,修改在7 年前。

RE: UserLocalServiceUtil.getRoleUsers does not return related user group us

New Member 帖子: 7 加入日期: 14-11-11 最近的帖子
Hi David,

Just one question, can we assign the usergroup to a particular group using java liferay API? If yes please suggest the API name.

Use case: The user is that if a user logs into Portal, and if the user is part of some user group, we want to assign the usergroup to predefined liferay regular roles using Liferay API. So that manual step of assigning the usergroup to role can be taken out. And we can automate this step.

Please suggest trying to use many Liferay API but yet to found the solution.

Regards
Divya