java 在 Liferay 中获取具有角色的所有用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6261371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting all users with a Role in Liferay
提问by Henrik Paul
I'm new to Liferay development in general, so feel free to point out if I'm going about stuff totally the wrong way.
我是 Liferay 开发的新手,所以请随时指出我是否以完全错误的方式处理事情。
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
containsa certain value.
我正在尝试获取某个组中所有用户的 DynamicQuery 对象(我将使用此对象进一步过滤我将针对留言板执行的另一个查询)。该User
接口似乎有一个roleIds
属性,我也许能使用,因为我已经知道roleId
我很感兴趣,但如果我不能找到查询的正确方法roleIds
包含一定的价值。
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.
PS:我会直接询问确切的 SQL 查询,但我宁愿使用 Liferay 自己的连接池,而无需做一些奇怪的 ext 项目。
回答by AdrianRM
You don't need a DynamicQuery. These are the methods you are looking for in the classes that Dirk points out:
您不需要 DynamicQuery。这些是您在 Dirk 指出的类中寻找的方法:
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.
请记住,XXXLocalServiceUtil 类中的方法不会检查当前用户的权限。
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();
}
回答by Dirk
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.
访问 liferays 自己的对象的最简单方法是使用XXXServiceUtil
类(例如RoleServiceUtil.getUserRoles(userId)
)。因此,您很少需要直接处理任何 SQL。无论是RoleServiceUtil
或UserServiceUtil
可能有你需要的。
回答by Francisco Javier Barrena
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:
组织的角色存储在表 UserGroupRole 中,因此如果您想获取组织的所有者,则必须使用以下代码:
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!
干杯!