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