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