001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 SystemException if a system exception occurred
172             */
173            public List<UserGroup> getUserUserGroups(long userId)
174                    throws PortalException, SystemException {
175    
176                    UserPermissionUtil.check(
177                            getPermissionChecker(), userId, ActionKeys.VIEW);
178    
179                    List<UserGroup> userGroups = userGroupLocalService.getUserUserGroups(
180                            userId);
181    
182                    return filterUserGroups(userGroups);
183            }
184    
185            /**
186             * Removes the user groups from the group.
187             *
188             * @param  groupId the primary key of the group
189             * @param  userGroupIds the primary keys of the user groups
190             * @throws PortalException if the user did not have permission to assign
191             *         group members
192             * @throws SystemException if a system exception occurred
193             */
194            public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
195                    throws PortalException, SystemException {
196    
197                    GroupPermissionUtil.check(
198                            getPermissionChecker(), groupId, ActionKeys.ASSIGN_MEMBERS);
199    
200                    userGroupLocalService.unsetGroupUserGroups(groupId, userGroupIds);
201            }
202    
203            /**
204             * Removes the user groups from the team.
205             *
206             * @param  teamId the primary key of the team
207             * @param  userGroupIds the primary keys of the user groups
208             * @throws PortalException if the user did not have permission to assign
209             *         team members
210             * @throws SystemException if a system exception occurred
211             */
212            public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
213                    throws PortalException, SystemException {
214    
215                    TeamPermissionUtil.check(
216                            getPermissionChecker(), teamId, ActionKeys.ASSIGN_MEMBERS);
217    
218                    userGroupLocalService.unsetTeamUserGroups(teamId, userGroupIds);
219            }
220    
221            /**
222             * Updates the user group.
223             *
224             * @param  userGroupId the primary key of the user group
225             * @param  name the user group's name
226             * @param  description the the user group's description
227             * @return the user group
228             * @throws PortalException if a user group with the primary key was not
229             *         found, if the new information was invalid, or if the user did not
230             *         have permission to update the user group information
231             * @throws SystemException if a system exception occurred
232             */
233            public UserGroup updateUserGroup(
234                            long userGroupId, String name, String description)
235                    throws PortalException, SystemException {
236    
237                    UserGroupPermissionUtil.check(
238                            getPermissionChecker(), userGroupId, ActionKeys.UPDATE);
239    
240                    User user = getUser();
241    
242                    return userGroupLocalService.updateUserGroup(
243                            user.getCompanyId(), userGroupId, name, description);
244            }
245    
246            protected List<UserGroup> filterUserGroups(List<UserGroup> userGroups)
247                    throws PortalException {
248    
249                    List<UserGroup> filteredGroups = new ArrayList<UserGroup>();
250    
251                    for (UserGroup userGroup : userGroups) {
252                            if (UserGroupPermissionUtil.contains(
253                                            getPermissionChecker(), userGroup.getUserGroupId(),
254                                            ActionKeys.VIEW)) {
255    
256                                    filteredGroups.add(userGroup);
257                            }
258                    }
259    
260                    return filteredGroups;
261            }
262    
263    }