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