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