com.liferay.portal.service.impl.UserGroupServiceImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.service.impl;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.UserGroup;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.IndexerRegistryUtil;
import com.liferay.portal.kernel.search.Sort;
import com.liferay.portal.kernel.search.SortFactoryUtil;
import com.liferay.portal.kernel.security.membershippolicy.UserGroupMembershipPolicyUtil;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.permission.GroupPermissionUtil;
import com.liferay.portal.kernel.service.permission.PortalPermissionUtil;
import com.liferay.portal.kernel.service.permission.TeamPermissionUtil;
import com.liferay.portal.kernel.service.permission.UserGroupPermissionUtil;
import com.liferay.portal.kernel.service.permission.UserPermissionUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.MapUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.comparator.UserGroupIdComparator;
import com.liferay.portal.service.base.UserGroupServiceBaseImpl;
import com.liferay.portal.service.persistence.constants.UserGroupFinderConstants;
import com.liferay.portal.util.PropsValues;
import com.liferay.users.admin.kernel.util.UsersAdminUtil;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
/**
* Provides the remote service for accessing, adding, deleting, and updating
* user groups. Its methods include permission checks.
*
* @author Charles May
*/
public class UserGroupServiceImpl extends UserGroupServiceBaseImpl {
/**
* Adds the user groups to the group.
*
* @param groupId the primary key of the group
* @param userGroupIds the primary keys of the user groups
*/
@Override
public void addGroupUserGroups(long groupId, long[] userGroupIds)
throws PortalException {
GroupPermissionUtil.check(
getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
userGroupLocalService.addGroupUserGroups(groupId, userGroupIds);
}
@Override
public UserGroup addOrUpdateUserGroup(
String externalReferenceCode, String name, String description,
ServiceContext serviceContext)
throws PortalException {
PermissionChecker permissionChecker = getPermissionChecker();
UserGroup userGroup =
userGroupLocalService.fetchUserGroupByExternalReferenceCode(
externalReferenceCode, permissionChecker.getCompanyId());
if (userGroup == null) {
PortalPermissionUtil.check(
permissionChecker, ActionKeys.ADD_USER_GROUP);
}
else {
UserGroupPermissionUtil.check(
permissionChecker, userGroup.getUserGroupId(),
ActionKeys.UPDATE);
}
return userGroupLocalService.addOrUpdateUserGroup(
externalReferenceCode, permissionChecker.getUserId(),
permissionChecker.getCompanyId(), name, description,
serviceContext);
}
/**
* Adds the user groups to the team
*
* @param teamId the primary key of the team
* @param userGroupIds the primary keys of the user groups
*/
@Override
public void addTeamUserGroups(long teamId, long[] userGroupIds)
throws PortalException {
TeamPermissionUtil.check(
getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
userGroupLocalService.addTeamUserGroups(teamId, userGroupIds);
}
/**
* Adds a user group.
*
*
* This method handles the creation and bookkeeping of the user group,
* including its resources, metadata, and internal data structures.
*
*
* @param name the user group's name
* @param description the user group's description
* @param serviceContext the service context to be applied (optionally
* null). Can set expando bridge attributes for the
* user group.
* @return the user group
*/
@Override
public UserGroup addUserGroup(
String name, String description, ServiceContext serviceContext)
throws PortalException {
PortalPermissionUtil.check(
getPermissionChecker(), ActionKeys.ADD_USER_GROUP);
User user = getUser();
UserGroup userGroup = userGroupLocalService.addUserGroup(
user.getUserId(), user.getCompanyId(), name, description,
serviceContext);
UserGroupMembershipPolicyUtil.verifyPolicy(userGroup);
return userGroup;
}
/**
* Deletes the user group.
*
* @param userGroupId the primary key of the user group
*/
@Override
public void deleteUserGroup(long userGroupId) throws PortalException {
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroupId, ActionKeys.DELETE);
userGroupLocalService.deleteUserGroup(userGroupId);
}
/**
* Fetches the user group with the primary key.
*
* @param userGroupId the primary key of the user group
* @return the user group with the primary key
*/
@Override
public UserGroup fetchUserGroup(long userGroupId) throws PortalException {
UserGroup userGroup = userGroupLocalService.fetchUserGroup(userGroupId);
if (userGroup != null) {
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroupId, ActionKeys.VIEW);
}
return userGroup;
}
@Override
public UserGroup fetchUserGroupByExternalReferenceCode(
long companyId, String externalReferenceCode)
throws PortalException {
UserGroup userGroup =
userGroupLocalService.fetchUserGroupByExternalReferenceCode(
externalReferenceCode, companyId);
if (userGroup != null) {
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroup.getUserGroupId(),
ActionKeys.VIEW);
}
return userGroup;
}
@Override
public List getGtUserGroups(
long gtUserGroupId, long companyId, long parentUserGroupId, int size) {
return userGroupPersistence.filterFindByGtU_C_P(
gtUserGroupId, companyId, parentUserGroupId, 0, size,
new UserGroupIdComparator(true));
}
/**
* Returns the user group with the primary key.
*
* @param userGroupId the primary key of the user group
* @return the user group with the primary key
*/
@Override
public UserGroup getUserGroup(long userGroupId) throws PortalException {
UserGroup userGroup = userGroupLocalService.getUserGroup(userGroupId);
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroupId, ActionKeys.VIEW);
return userGroup;
}
/**
* Returns the user group with the name.
*
* @param name the user group's name
* @return the user group with the name
*/
@Override
public UserGroup getUserGroup(String name) throws PortalException {
User user = getUser();
UserGroup userGroup = userGroupLocalService.getUserGroup(
user.getCompanyId(), name);
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroup.getUserGroupId(),
ActionKeys.VIEW);
return userGroup;
}
@Override
public List getUserGroups(long companyId)
throws PortalException {
return filterUserGroups(userGroupLocalService.getUserGroups(companyId));
}
@Override
public List getUserGroups(
long companyId, String name, int start, int end) {
if (Validator.isNull(name)) {
return userGroupPersistence.filterFindByCompanyId(
companyId, start, end);
}
return userGroupPersistence.filterFindByC_LikeN(
companyId, name, start, end);
}
@Override
public int getUserGroupsCount(long companyId, String name) {
if (Validator.isNull(name)) {
return userGroupPersistence.filterCountByCompanyId(companyId);
}
return userGroupPersistence.filterCountByC_LikeN(companyId, name);
}
/**
* Returns all the user groups to which the user belongs.
*
* @param userId the primary key of the user
* @return the user groups to which the user belongs
*/
@Override
public List getUserUserGroups(long userId)
throws PortalException {
UserPermissionUtil.check(
getPermissionChecker(), userId, ActionKeys.VIEW);
List userGroups = userGroupLocalService.getUserUserGroups(
userId);
return filterUserGroups(userGroups);
}
/**
* Returns an ordered range of all the user groups that match the keywords.
*
*
* Useful when paginating results. Returns a maximum of end -
* start instances. start and end are not
* primary keys, they are indexes in the result set. Thus, 0
* refers to the first result in the set. Setting both start
* and end to {@link QueryUtil#ALL_POS} will return the full
* result set.
*
*
* @param companyId the primary key of the user group's company
* @param keywords the keywords (space separated), which may occur in the
* user group's name or description (optionally null)
* @param params the finder params (optionally null). For more
* information see {@link
* com.liferay.portal.kernel.service.persistence.UserGroupFinder}
* @param start the lower bound of the range of user groups to return
* @param end the upper bound of the range of user groups to return (not
* inclusive)
* @param orderByComparator the comparator to order the user groups
* (optionally null)
* @return the matching user groups ordered by comparator
* orderByComparator
* @see com.liferay.portal.kernel.service.persistence.UserGroupFinder
*/
@Override
public List search(
long companyId, String keywords, LinkedHashMap params,
int start, int end, OrderByComparator orderByComparator) {
if (isUseCustomSQL(params)) {
return userGroupFinder.filterFindByKeywords(
companyId, keywords, params, start, end, orderByComparator);
}
String orderByCol = orderByComparator.getOrderByFields()[0];
String orderByType = "asc";
if (!orderByComparator.isAscending()) {
orderByType = "desc";
}
Sort sort = SortFactoryUtil.getSort(
UserGroup.class, orderByCol, orderByType);
try {
return UsersAdminUtil.getUserGroups(
userGroupLocalService.search(
companyId, keywords, params, start, end, sort));
}
catch (Exception exception) {
throw new SystemException(exception);
}
}
/**
* Returns an ordered range of all the user groups that match the name and
* description.
*
*
* Useful when paginating results. Returns a maximum of end -
* start instances. start and end are not
* primary keys, they are indexes in the result set. Thus, 0
* refers to the first result in the set. Setting both start
* and end to {@link QueryUtil#ALL_POS} will return the full
* result set.
*
*
* @param companyId the primary key of the user group's company
* @param name the user group's name (optionally null)
* @param description the user group's description (optionally
* null)
* @param params the finder params (optionally null). For more
* information see {@link
* com.liferay.portal.kernel.service.persistence.UserGroupFinder}
* @param andOperator whether every field must match its keywords or just
* one field
* @param start the lower bound of the range of user groups to return
* @param end the upper bound of the range of user groups to return (not
* inclusive)
* @param orderByComparator the comparator to order the user groups
* (optionally null)
* @return the matching user groups ordered by comparator
* orderByComparator
* @see com.liferay.portal.kernel.service.persistence.UserGroupFinder
*/
@Override
public List search(
long companyId, String name, String description,
LinkedHashMap params, boolean andOperator, int start,
int end, OrderByComparator orderByComparator) {
if (isUseCustomSQL(params)) {
return userGroupFinder.filterFindByC_N_D(
companyId, name, description, params, andOperator, start, end,
orderByComparator);
}
String orderByCol = orderByComparator.getOrderByFields()[0];
String orderByType = "asc";
if (!orderByComparator.isAscending()) {
orderByType = "desc";
}
Sort sort = SortFactoryUtil.getSort(
UserGroup.class, orderByCol, orderByType);
try {
return UsersAdminUtil.getUserGroups(
userGroupLocalService.search(
companyId, name, description, params, andOperator, start,
end, sort));
}
catch (Exception exception) {
throw new SystemException(exception);
}
}
/**
* Returns the number of user groups that match the keywords
*
* @param companyId the primary key of the user group's company
* @param keywords the keywords (space separated), which may occur in the
* user group's name or description (optionally null)
* @param params the finder params (optionally null). For more
* information see {@link
* com.liferay.portal.kernel.service.persistence.UserGroupFinder}
* @return the number of matching user groups
* @see com.liferay.portal.kernel.service.persistence.UserGroupFinder
*/
@Override
public int searchCount(
long companyId, String keywords, LinkedHashMap params) {
if (isUseCustomSQL(params)) {
return userGroupFinder.filterCountByKeywords(
companyId, keywords, params);
}
return userGroupLocalService.searchCount(companyId, keywords, params);
}
/**
* Returns the number of user groups that match the name and description.
*
* @param companyId the primary key of the user group's company
* @param name the user group's name (optionally null)
* @param description the user group's description (optionally
* null)
* @param params the finder params (optionally null). For more
* information see {@link
* com.liferay.portal.kernel.service.persistence.UserGroupFinder}
* @param andOperator whether every field must match its keywords or just
* one field
* @return the number of matching user groups
* @see com.liferay.portal.kernel.service.persistence.UserGroupFinder
*/
@Override
public int searchCount(
long companyId, String name, String description,
LinkedHashMap params, boolean andOperator) {
if (isUseCustomSQL(params)) {
return userGroupFinder.filterCountByC_N_D(
companyId, name, description, params, andOperator);
}
return userGroupLocalService.searchCount(
companyId, name, description, params, andOperator);
}
/**
* Removes the user groups from the group.
*
* @param groupId the primary key of the group
* @param userGroupIds the primary keys of the user groups
*/
@Override
public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
throws PortalException {
GroupPermissionUtil.check(
getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
userGroupLocalService.unsetGroupUserGroups(groupId, userGroupIds);
}
/**
* Removes the user groups from the team.
*
* @param teamId the primary key of the team
* @param userGroupIds the primary keys of the user groups
*/
@Override
public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
throws PortalException {
TeamPermissionUtil.check(
getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
userGroupLocalService.unsetTeamUserGroups(teamId, userGroupIds);
}
@Override
public UserGroup updateExternalReferenceCode(
UserGroup userGroup, String externalReferenceCode)
throws PortalException {
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroup.getUserGroupId(),
ActionKeys.UPDATE);
return userGroupLocalService.updateExternalReferenceCode(
userGroup, externalReferenceCode);
}
/**
* Updates the user group.
*
* @param userGroupId the primary key of the user group
* @param name the user group's name
* @param description the the user group's description
* @param serviceContext the service context to be applied (optionally
* null). Can set expando bridge attributes for the
* user group.
* @return the user group
*/
@Override
public UserGroup updateUserGroup(
long userGroupId, String name, String description,
ServiceContext serviceContext)
throws PortalException {
UserGroupPermissionUtil.check(
getPermissionChecker(), userGroupId, ActionKeys.UPDATE);
User user = getUser();
return userGroupLocalService.updateUserGroup(
user.getCompanyId(), userGroupId, name, description,
serviceContext);
}
protected List filterUserGroups(List userGroups)
throws PortalException {
List filteredGroups = new ArrayList<>();
for (UserGroup userGroup : userGroups) {
if (UserGroupPermissionUtil.contains(
getPermissionChecker(), userGroup.getUserGroupId(),
ActionKeys.VIEW)) {
filteredGroups.add(userGroup);
}
}
return filteredGroups;
}
/**
* @see UserGroupLocalServiceImpl#isUseCustomSQL
*/
protected boolean isUseCustomSQL(LinkedHashMap params) {
if (MapUtil.isEmpty(params)) {
return false;
}
Indexer> indexer = IndexerRegistryUtil.nullSafeGetIndexer(
UserGroup.class);
if (!indexer.isIndexerEnabled() ||
!PropsValues.USER_GROUPS_SEARCH_WITH_INDEX) {
return true;
}
if (MapUtil.isEmpty(params)) {
return false;
}
for (String key : params.keySet()) {
if (ArrayUtil.contains(UserGroupFinderConstants.PARAM_KEYS, key)) {
return true;
}
}
return false;
}
}