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.permission.ActionKeys;
022    import com.liferay.portal.service.base.UserGroupServiceBaseImpl;
023    import com.liferay.portal.service.permission.GroupPermissionUtil;
024    import com.liferay.portal.service.permission.PortalPermissionUtil;
025    import com.liferay.portal.service.permission.TeamPermissionUtil;
026    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
027    import com.liferay.portal.service.permission.UserPermissionUtil;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * The implementation of the user group remote service.
034     *
035     * @author Charles May
036     */
037    public class UserGroupServiceImpl extends UserGroupServiceBaseImpl {
038    
039            /**
040             * Adds the user groups to the group.
041             *
042             * @param  groupId the primary key of the group
043             * @param  userGroupIds the primary keys of the user groups
044             * @throws PortalException if a group or user group with the primary key
045             *         could not be found, or if the user did not have permission to
046             *         assign group members
047             * @throws SystemException if a system exception occurred
048             */
049            @Override
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            @Override
070            public void addTeamUserGroups(long teamId, long[] userGroupIds)
071                    throws PortalException, SystemException {
072    
073                    TeamPermissionUtil.check(
074                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
075    
076                    userGroupLocalService.addTeamUserGroups(teamId, userGroupIds);
077            }
078    
079            /**
080             * Adds a user group.
081             *
082             * <p>
083             * This method handles the creation and bookkeeping of the user group,
084             * including its resources, metadata, and internal data structures.
085             * </p>
086             *
087             * @param  name the user group's name
088             * @param  description the user group's description
089             * @return the user group
090             * @throws PortalException if the user group's information was invalid or if
091             *         the user did not have permission to add the user group
092             * @throws SystemException if a system exception occurred
093             */
094            @Override
095            public UserGroup addUserGroup(String name, String description)
096                    throws PortalException, SystemException {
097    
098                    PortalPermissionUtil.check(
099                            getPermissionChecker(), ActionKeys.ADD_USER_GROUP);
100    
101                    User user = getUser();
102    
103                    return userGroupLocalService.addUserGroup(
104                            user.getUserId(), user.getCompanyId(), name, description);
105            }
106    
107            /**
108             * Deletes the user group.
109             *
110             * @param  userGroupId the primary key of the user group
111             * @throws PortalException if a user group with the primary key could not be
112             *         found, if the user did not have permission to delete the user
113             *         group, or if the user group had a workflow in approved status
114             * @throws SystemException if a system exception occurred
115             */
116            @Override
117            public void deleteUserGroup(long userGroupId)
118                    throws PortalException, SystemException {
119    
120                    UserGroupPermissionUtil.check(
121                            getPermissionChecker(), userGroupId, ActionKeys.DELETE);
122    
123                    userGroupLocalService.deleteUserGroup(userGroupId);
124            }
125    
126            /**
127             * Returns the user group with the primary key.
128             *
129             * @param  userGroupId the primary key of the user group
130             * @return Returns the user group with the primary key
131             * @throws PortalException if a user group with the primary key could not be
132             *         found or if the user did not have permission to view the user
133             *         group
134             * @throws SystemException if a system exception occurred
135             */
136            @Override
137            public UserGroup getUserGroup(long userGroupId)
138                    throws PortalException, SystemException {
139    
140                    UserGroupPermissionUtil.check(
141                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
142    
143                    return userGroupLocalService.getUserGroup(userGroupId);
144            }
145    
146            /**
147             * Returns the user group with the name.
148             *
149             * @param  name the user group's name
150             * @return Returns the user group with the name
151             * @throws PortalException if a user group with the name could not be found
152             *         or if the user did not have permission to view the user group
153             * @throws SystemException if a system exception occurred
154             */
155            @Override
156            public UserGroup getUserGroup(String name)
157                    throws PortalException, SystemException {
158    
159                    User user = getUser();
160    
161                    UserGroup userGroup = userGroupLocalService.getUserGroup(
162                            user.getCompanyId(), name);
163    
164                    long userGroupId = userGroup.getUserGroupId();
165    
166                    UserGroupPermissionUtil.check(
167                            getPermissionChecker(), userGroupId, ActionKeys.VIEW);
168    
169                    return userGroup;
170            }
171    
172            /**
173             * Returns all the user groups to which the user belongs.
174             *
175             * @param  userId the primary key of the user
176             * @return the user groups to which the user belongs
177             * @throws PortalException if the current user did not have permission to
178             *         view the user or any one of the user group members
179             * @throws SystemException if a system exception occurred
180             */
181            @Override
182            public List<UserGroup> getUserUserGroups(long userId)
183                    throws PortalException, SystemException {
184    
185                    UserPermissionUtil.check(
186                            getPermissionChecker(), userId, ActionKeys.VIEW);
187    
188                    List<UserGroup> userGroups = userGroupLocalService.getUserUserGroups(
189                            userId);
190    
191                    return filterUserGroups(userGroups);
192            }
193    
194            /**
195             * Removes the user groups from the group.
196             *
197             * @param  groupId the primary key of the group
198             * @param  userGroupIds the primary keys of the user groups
199             * @throws PortalException if the user did not have permission to assign
200             *         group members
201             * @throws SystemException if a system exception occurred
202             */
203            @Override
204            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
205                    throws PortalException, SystemException {
206    
207                    GroupPermissionUtil.check(
208                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
209    
210                    userGroupLocalService.unsetGroupUserGroups(groupId, userGroupIds);
211            }
212    
213            /**
214             * Removes the user groups from the team.
215             *
216             * @param  teamId the primary key of the team
217             * @param  userGroupIds the primary keys of the user groups
218             * @throws PortalException if the user did not have permission to assign
219             *         team members
220             * @throws SystemException if a system exception occurred
221             */
222            @Override
223            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
224                    throws PortalException, SystemException {
225    
226                    TeamPermissionUtil.check(
227                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
228    
229                    userGroupLocalService.unsetTeamUserGroups(teamId, userGroupIds);
230            }
231    
232            /**
233             * Updates the user group.
234             *
235             * @param  userGroupId the primary key of the user group
236             * @param  name the user group's name
237             * @param  description the the user group's description
238             * @return the user group
239             * @throws PortalException if a user group with the primary key was not
240             *         found, if the new information was invalid, or if the user did not
241             *         have permission to update the user group information
242             * @throws SystemException if a system exception occurred
243             */
244            @Override
245            public UserGroup updateUserGroup(
246                            long userGroupId, String name, String description)
247                    throws PortalException, SystemException {
248    
249                    UserGroupPermissionUtil.check(
250                            getPermissionChecker(), userGroupId, ActionKeys.UPDATE);
251    
252                    User user = getUser();
253    
254                    return userGroupLocalService.updateUserGroup(
255                            user.getCompanyId(), userGroupId, name, description);
256            }
257    
258            protected List<UserGroup> filterUserGroups(List<UserGroup> userGroups)
259                    throws PortalException {
260    
261                    List<UserGroup> filteredGroups = new ArrayList<UserGroup>();
262    
263                    for (UserGroup userGroup : userGroups) {
264                            if (UserGroupPermissionUtil.contains(
265                                            getPermissionChecker(), userGroup.getUserGroupId(),
266                                            ActionKeys.VIEW)) {
267    
268                                    filteredGroups.add(userGroup);
269                            }
270                    }
271    
272                    return filteredGroups;
273            }
274    
275    }