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