001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.User;
019    import com.liferay.portal.model.UserGroup;
020    import com.liferay.portal.security.membershippolicy.UserGroupMembershipPolicyUtil;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.base.UserGroupServiceBaseImpl;
024    import com.liferay.portal.service.permission.GroupPermissionUtil;
025    import com.liferay.portal.service.permission.PortalPermissionUtil;
026    import com.liferay.portal.service.permission.TeamPermissionUtil;
027    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
028    import com.liferay.portal.service.permission.UserPermissionUtil;
029    import com.liferay.portlet.expando.model.ExpandoBridge;
030    
031    import java.io.Serializable;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    import java.util.Map;
036    
037    /**
038     * Provides the remote service for accessing, adding, deleting, and updating
039     * user groups. Its methods include permission checks.
040     *
041     * @author Charles May
042     */
043    public class UserGroupServiceImpl extends UserGroupServiceBaseImpl {
044    
045            /**
046             * Adds the user groups to the group.
047             *
048             * @param groupId the primary key of the group
049             * @param userGroupIds the primary keys of the user groups
050             */
051            @Override
052            public void addGroupUserGroups(long groupId, long[] userGroupIds)
053                    throws PortalException {
054    
055                    GroupPermissionUtil.check(
056                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
057    
058                    userGroupLocalService.addGroupUserGroups(groupId, userGroupIds);
059            }
060    
061            /**
062             * Adds the user groups to the team
063             *
064             * @param teamId the primary key of the team
065             * @param userGroupIds the primary keys of the user groups
066             */
067            @Override
068            public void addTeamUserGroups(long teamId, long[] userGroupIds)
069                    throws PortalException {
070    
071                    TeamPermissionUtil.check(
072                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
073    
074                    userGroupLocalService.addTeamUserGroups(teamId, userGroupIds);
075            }
076    
077            /**
078             * Adds a user group.
079             *
080             * <p>
081             * This method handles the creation and bookkeeping of the user group,
082             * including its resources, metadata, and internal data structures.
083             * </p>
084             *
085             * @param      name the user group's name
086             * @param      description the user group's description
087             * @return     the user group
088             * @deprecated As of 6.2.0, replaced by {@link #addUserGroup(String, String,
089             *             ServiceContext)}
090             */
091            @Deprecated
092            @Override
093            public UserGroup addUserGroup(String name, String description)
094                    throws PortalException {
095    
096                    return addUserGroup(name, description, null);
097            }
098    
099            /**
100             * Adds a user group.
101             *
102             * <p>
103             * This method handles the creation and bookkeeping of the user group,
104             * including its resources, metadata, and internal data structures.
105             * </p>
106             *
107             * @param  name the user group's name
108             * @param  description the user group's description
109             * @param  serviceContext the service context to be applied (optionally
110             *         <code>null</code>). Can set expando bridge attributes for the
111             *         user group.
112             * @return the user group
113             */
114            @Override
115            public UserGroup addUserGroup(
116                            String name, String description, ServiceContext serviceContext)
117                    throws PortalException {
118    
119                    PortalPermissionUtil.check(
120                            getPermissionChecker(), ActionKeys.ADD_USER_GROUP);
121    
122                    User user = getUser();
123    
124                    UserGroup userGroup = userGroupLocalService.addUserGroup(
125                            user.getUserId(), user.getCompanyId(), name, description,
126                            serviceContext);
127    
128                    UserGroupMembershipPolicyUtil.verifyPolicy(userGroup);
129    
130                    return userGroup;
131            }
132    
133            /**
134             * Deletes the user group.
135             *
136             * @param userGroupId the primary key of the user group
137             */
138            @Override
139            public void deleteUserGroup(long userGroupId) throws PortalException {
140                    UserGroupPermissionUtil.check(
141                            getPermissionChecker(), userGroupId, ActionKeys.DELETE);
142    
143                    userGroupLocalService.deleteUserGroup(userGroupId);
144            }
145    
146            /**
147             * Fetches the user group with the primary key.
148             *
149             * @param  userGroupId the primary key of the user group
150             * @return the user group with the primary key
151             */
152            @Override
153            public UserGroup fetchUserGroup(long userGroupId) throws PortalException {
154                    UserGroup userGroup = userGroupLocalService.fetchUserGroup(userGroupId);
155    
156                    if (userGroup != null) {
157                            UserGroupPermissionUtil.check(
158                                    getPermissionChecker(), userGroupId, ActionKeys.VIEW);
159                    }
160    
161                    return userGroup;
162            }
163    
164            /**
165             * Returns the user group with the primary key.
166             *
167             * @param  userGroupId the primary key of the user group
168             * @return the user group with the primary key
169             */
170            @Override
171            public UserGroup getUserGroup(long userGroupId) throws PortalException {
172                    UserGroupPermissionUtil.check(
173                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
174    
175                    return userGroupLocalService.getUserGroup(userGroupId);
176            }
177    
178            /**
179             * Returns the user group with the name.
180             *
181             * @param  name the user group's name
182             * @return the user group with the name
183             */
184            @Override
185            public UserGroup getUserGroup(String name) throws PortalException {
186                    User user = getUser();
187    
188                    UserGroup userGroup = userGroupLocalService.getUserGroup(
189                            user.getCompanyId(), name);
190    
191                    long userGroupId = userGroup.getUserGroupId();
192    
193                    UserGroupPermissionUtil.check(
194                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
195    
196                    return userGroup;
197            }
198    
199            @Override
200            public List<UserGroup> getUserGroups(long companyId)
201                    throws PortalException {
202    
203                    return filterUserGroups(userGroupLocalService.getUserGroups(companyId));
204            }
205    
206            /**
207             * Returns all the user groups to which the user belongs.
208             *
209             * @param  userId the primary key of the user
210             * @return the user groups to which the user belongs
211             */
212            @Override
213            public List<UserGroup> getUserUserGroups(long userId)
214                    throws PortalException {
215    
216                    UserPermissionUtil.check(
217                            getPermissionChecker(), userId, ActionKeys.VIEW);
218    
219                    List<UserGroup> userGroups = userGroupLocalService.getUserUserGroups(
220                            userId);
221    
222                    return filterUserGroups(userGroups);
223            }
224    
225            /**
226             * Removes the user groups from the group.
227             *
228             * @param groupId the primary key of the group
229             * @param userGroupIds the primary keys of the user groups
230             */
231            @Override
232            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
233                    throws PortalException {
234    
235                    GroupPermissionUtil.check(
236                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
237    
238                    userGroupLocalService.unsetGroupUserGroups(groupId, userGroupIds);
239            }
240    
241            /**
242             * Removes the user groups from the team.
243             *
244             * @param teamId the primary key of the team
245             * @param userGroupIds the primary keys of the user groups
246             */
247            @Override
248            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
249                    throws PortalException {
250    
251                    TeamPermissionUtil.check(
252                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
253    
254                    userGroupLocalService.unsetTeamUserGroups(teamId, userGroupIds);
255            }
256    
257            /**
258             * Updates the user group.
259             *
260             * @param      userGroupId the primary key of the user group
261             * @param      name the user group's name
262             * @param      description the the user group's description
263             * @return     the user group
264             * @deprecated As of 6.2.0, replaced by {@link #updateUserGroup(long,
265             *             String, String, ServiceContext)}
266             */
267            @Deprecated
268            @Override
269            public UserGroup updateUserGroup(
270                            long userGroupId, String name, String description)
271                    throws PortalException {
272    
273                    UserGroup oldUserGroup = userGroupPersistence.findByPrimaryKey(
274                            userGroupId);
275    
276                    ExpandoBridge oldExpandoBridge = oldUserGroup.getExpandoBridge();
277    
278                    Map<String, Serializable> oldExpandoAttributes =
279                            oldExpandoBridge.getAttributes();
280    
281                    UserGroup userGroup = updateUserGroup(
282                            userGroupId, name, description, null);
283    
284                    UserGroupMembershipPolicyUtil.verifyPolicy(
285                            userGroup, oldUserGroup, oldExpandoAttributes);
286    
287                    return userGroup;
288            }
289    
290            /**
291             * Updates the user group.
292             *
293             * @param  userGroupId the primary key of the user group
294             * @param  name the user group's name
295             * @param  description the the user group's description
296             * @param  serviceContext the service context to be applied (optionally
297             *         <code>null</code>). Can set expando bridge attributes for the
298             *         user group.
299             * @return the user group
300             */
301            @Override
302            public UserGroup updateUserGroup(
303                            long userGroupId, String name, String description,
304                            ServiceContext serviceContext)
305                    throws PortalException {
306    
307                    UserGroupPermissionUtil.check(
308                            getPermissionChecker(), userGroupId, ActionKeys.UPDATE);
309    
310                    User user = getUser();
311    
312                    return userGroupLocalService.updateUserGroup(
313                            user.getCompanyId(), userGroupId, name, description,
314                            serviceContext);
315            }
316    
317            protected List<UserGroup> filterUserGroups(List<UserGroup> userGroups)
318                    throws PortalException {
319    
320                    List<UserGroup> filteredGroups = new ArrayList<>();
321    
322                    for (UserGroup userGroup : userGroups) {
323                            if (UserGroupPermissionUtil.contains(
324                                            getPermissionChecker(), userGroup.getUserGroupId(),
325                                            ActionKeys.VIEW)) {
326    
327                                    filteredGroups.add(userGroup);
328                            }
329                    }
330    
331                    return filteredGroups;
332            }
333    
334    }