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             * @throws PortalException if the user did not have permission to assign
051             *         group members
052             */
053            @Override
054            public void addGroupUserGroups(long groupId, long[] userGroupIds)
055                    throws PortalException {
056    
057                    GroupPermissionUtil.check(
058                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
059    
060                    userGroupLocalService.addGroupUserGroups(groupId, userGroupIds);
061            }
062    
063            /**
064             * Adds the user groups to the team
065             *
066             * @param  teamId the primary key of the team
067             * @param  userGroupIds the primary keys of the user groups
068             * @throws PortalException if the user did not have permission to assign
069             *         team members
070             */
071            @Override
072            public void addTeamUserGroups(long teamId, long[] userGroupIds)
073                    throws PortalException {
074    
075                    TeamPermissionUtil.check(
076                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
077    
078                    userGroupLocalService.addTeamUserGroups(teamId, userGroupIds);
079            }
080    
081            /**
082             * Adds a user group.
083             *
084             * <p>
085             * This method handles the creation and bookkeeping of the user group,
086             * including its resources, metadata, and internal data structures.
087             * </p>
088             *
089             * @param      name the user group's name
090             * @param      description the user group's description
091             * @return     the user group
092             * @throws     PortalException if the user group's information was invalid
093             *             or if the user did not have permission to add the user group
094             * @deprecated As of 6.2.0, replaced by {@link #addUserGroup(String, String,
095             *             ServiceContext)}
096             */
097            @Deprecated
098            @Override
099            public UserGroup addUserGroup(String name, String description)
100                    throws PortalException {
101    
102                    return addUserGroup(name, description, null);
103            }
104    
105            /**
106             * Adds a user group.
107             *
108             * <p>
109             * This method handles the creation and bookkeeping of the user group,
110             * including its resources, metadata, and internal data structures.
111             * </p>
112             *
113             * @param  name the user group's name
114             * @param  description the user group's description
115             * @param  serviceContext the service context to be applied (optionally
116             *         <code>null</code>). Can set expando bridge attributes for the
117             *         user group.
118             * @return the user group
119             * @throws PortalException if the user group's information was invalid or if
120             *         the user did not have permission to add the user group
121             */
122            @Override
123            public UserGroup addUserGroup(
124                            String name, String description, ServiceContext serviceContext)
125                    throws PortalException {
126    
127                    PortalPermissionUtil.check(
128                            getPermissionChecker(), ActionKeys.ADD_USER_GROUP);
129    
130                    User user = getUser();
131    
132                    UserGroup userGroup = userGroupLocalService.addUserGroup(
133                            user.getUserId(), user.getCompanyId(), name, description,
134                            serviceContext);
135    
136                    UserGroupMembershipPolicyUtil.verifyPolicy(userGroup);
137    
138                    return userGroup;
139            }
140    
141            /**
142             * Deletes the user group.
143             *
144             * @param  userGroupId the primary key of the user group
145             * @throws PortalException if a user group with the primary key could not be
146             *         found, if the user did not have permission to delete the user
147             *         group, or if the user group had a workflow in approved status
148             */
149            @Override
150            public void deleteUserGroup(long userGroupId) throws PortalException {
151                    UserGroupPermissionUtil.check(
152                            getPermissionChecker(), userGroupId, ActionKeys.DELETE);
153    
154                    userGroupLocalService.deleteUserGroup(userGroupId);
155            }
156    
157            /**
158             * Fetches the user group with the primary key.
159             *
160             * @param  userGroupId the primary key of the user group
161             * @return the user group with the primary key
162             * @throws PortalException if the user did not have permission to view the
163             *         user group
164             */
165            @Override
166            public UserGroup fetchUserGroup(long userGroupId) throws PortalException {
167                    UserGroupPermissionUtil.check(
168                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
169    
170                    return userGroupLocalService.fetchUserGroup(userGroupId);
171            }
172    
173            /**
174             * Returns the user group with the primary key.
175             *
176             * @param  userGroupId the primary key of the user group
177             * @return the user group with the primary key
178             * @throws PortalException if a user group with the primary key could not be
179             *         found or if the user did not have permission to view the user
180             *         group
181             */
182            @Override
183            public UserGroup getUserGroup(long userGroupId) throws PortalException {
184                    UserGroupPermissionUtil.check(
185                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
186    
187                    return userGroupLocalService.getUserGroup(userGroupId);
188            }
189    
190            /**
191             * Returns the user group with the name.
192             *
193             * @param  name the user group's name
194             * @return the user group with the name
195             * @throws PortalException if a user group with the name could not be found
196             *         or if the user did not have permission to view the user group
197             */
198            @Override
199            public UserGroup getUserGroup(String name) throws PortalException {
200                    User user = getUser();
201    
202                    UserGroup userGroup = userGroupLocalService.getUserGroup(
203                            user.getCompanyId(), name);
204    
205                    long userGroupId = userGroup.getUserGroupId();
206    
207                    UserGroupPermissionUtil.check(
208                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
209    
210                    return userGroup;
211            }
212    
213            @Override
214            public List<UserGroup> getUserGroups(long companyId)
215                    throws PortalException {
216    
217                    return filterUserGroups(userGroupLocalService.getUserGroups(companyId));
218            }
219    
220            /**
221             * Returns all the user groups to which the user belongs.
222             *
223             * @param  userId the primary key of the user
224             * @return the user groups to which the user belongs
225             * @throws PortalException if the current user did not have permission to
226             *         view the user or any one of the user group members
227             */
228            @Override
229            public List<UserGroup> getUserUserGroups(long userId)
230                    throws PortalException {
231    
232                    UserPermissionUtil.check(
233                            getPermissionChecker(), userId, ActionKeys.VIEW);
234    
235                    List<UserGroup> userGroups = userGroupLocalService.getUserUserGroups(
236                            userId);
237    
238                    return filterUserGroups(userGroups);
239            }
240    
241            /**
242             * Removes the user groups from the group.
243             *
244             * @param  groupId the primary key of the group
245             * @param  userGroupIds the primary keys of the user groups
246             * @throws PortalException if the user did not have permission to assign
247             *         group members
248             */
249            @Override
250            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
251                    throws PortalException {
252    
253                    GroupPermissionUtil.check(
254                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
255    
256                    userGroupLocalService.unsetGroupUserGroups(groupId, userGroupIds);
257            }
258    
259            /**
260             * Removes the user groups from the team.
261             *
262             * @param  teamId the primary key of the team
263             * @param  userGroupIds the primary keys of the user groups
264             * @throws PortalException if the user did not have permission to assign
265             *         team members
266             */
267            @Override
268            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
269                    throws PortalException {
270    
271                    TeamPermissionUtil.check(
272                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
273    
274                    userGroupLocalService.unsetTeamUserGroups(teamId, userGroupIds);
275            }
276    
277            /**
278             * Updates the user group.
279             *
280             * @param      userGroupId the primary key of the user group
281             * @param      name the user group's name
282             * @param      description the the user group's description
283             * @return     the user group
284             * @throws     PortalException if a user group with the primary key was not
285             *             found, if the new information was invalid, or if the user did
286             *             not have permission to update the user group information
287             * @deprecated As of 6.2.0, replaced by {@link #updateUserGroup(long,
288             *             String, String, ServiceContext)}
289             */
290            @Deprecated
291            @Override
292            public UserGroup updateUserGroup(
293                            long userGroupId, String name, String description)
294                    throws PortalException {
295    
296                    UserGroup oldUserGroup = userGroupPersistence.findByPrimaryKey(
297                            userGroupId);
298    
299                    ExpandoBridge oldExpandoBridge = oldUserGroup.getExpandoBridge();
300    
301                    Map<String, Serializable> oldExpandoAttributes =
302                            oldExpandoBridge.getAttributes();
303    
304                    UserGroup userGroup = updateUserGroup(
305                            userGroupId, name, description, null);
306    
307                    UserGroupMembershipPolicyUtil.verifyPolicy(
308                            userGroup, oldUserGroup, oldExpandoAttributes);
309    
310                    return userGroup;
311            }
312    
313            /**
314             * Updates the user group.
315             *
316             * @param  userGroupId the primary key of the user group
317             * @param  name the user group's name
318             * @param  description the the user group's description
319             * @param  serviceContext the service context to be applied (optionally
320             *         <code>null</code>). Can set expando bridge attributes for the
321             *         user group.
322             * @return the user group
323             * @throws PortalException if a user group with the primary key was not
324             *         found, if the new information was invalid, or if the user did not
325             *         have permission to update the user group information
326             */
327            @Override
328            public UserGroup updateUserGroup(
329                            long userGroupId, String name, String description,
330                            ServiceContext serviceContext)
331                    throws PortalException {
332    
333                    UserGroupPermissionUtil.check(
334                            getPermissionChecker(), userGroupId, ActionKeys.UPDATE);
335    
336                    User user = getUser();
337    
338                    return userGroupLocalService.updateUserGroup(
339                            user.getCompanyId(), userGroupId, name, description,
340                            serviceContext);
341            }
342    
343            protected List<UserGroup> filterUserGroups(List<UserGroup> userGroups)
344                    throws PortalException {
345    
346                    List<UserGroup> filteredGroups = new ArrayList<>();
347    
348                    for (UserGroup userGroup : userGroups) {
349                            if (UserGroupPermissionUtil.contains(
350                                            getPermissionChecker(), userGroup.getUserGroupId(),
351                                            ActionKeys.VIEW)) {
352    
353                                    filteredGroups.add(userGroup);
354                            }
355                    }
356    
357                    return filteredGroups;
358            }
359    
360    }