1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.ListUtil;
20  import com.liferay.portal.kernel.util.MapUtil;
21  import com.liferay.portal.model.Group;
22  import com.liferay.portal.model.Organization;
23  import com.liferay.portal.model.UserGroup;
24  import com.liferay.portal.security.permission.ActionKeys;
25  import com.liferay.portal.security.permission.PermissionChecker;
26  import com.liferay.portal.service.ServiceContext;
27  import com.liferay.portal.service.base.GroupServiceBaseImpl;
28  import com.liferay.portal.service.permission.GroupPermissionUtil;
29  import com.liferay.portal.service.permission.PortalPermissionUtil;
30  import com.liferay.portal.service.permission.RolePermissionUtil;
31  
32  import java.util.Iterator;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  
36  /**
37   * <a href="GroupServiceImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class GroupServiceImpl extends GroupServiceBaseImpl {
42  
43      public Group addGroup(
44              String name, String description, int type, String friendlyURL,
45              boolean active, ServiceContext serviceContext)
46          throws PortalException, SystemException {
47  
48          PortalPermissionUtil.check(
49              getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
50  
51          return groupLocalService.addGroup(
52              getUserId(), null, 0, name, description, type, friendlyURL, active,
53              serviceContext);
54      }
55  
56      public Group addGroup(
57              long liveGroupId, String name, String description, int type,
58              String friendlyURL, boolean active, ServiceContext serviceContext)
59          throws PortalException, SystemException {
60  
61          GroupPermissionUtil.check(
62              getPermissionChecker(), liveGroupId, ActionKeys.UPDATE);
63  
64          return groupLocalService.addGroup(
65              getUserId(), null, 0, liveGroupId, name, description, type,
66              friendlyURL, active, serviceContext);
67      }
68  
69      public void addRoleGroups(long roleId, long[] groupIds)
70          throws PortalException, SystemException {
71  
72          RolePermissionUtil.check(
73              getPermissionChecker(), roleId, ActionKeys.UPDATE);
74  
75          groupLocalService.addRoleGroups(roleId, groupIds);
76      }
77  
78      public void deleteGroup(long groupId)
79          throws PortalException, SystemException {
80  
81          GroupPermissionUtil.check(
82              getPermissionChecker(), groupId, ActionKeys.DELETE);
83  
84          groupLocalService.deleteGroup(groupId);
85      }
86  
87      public Group getGroup(long groupId)
88          throws PortalException, SystemException {
89  
90          return groupLocalService.getGroup(groupId);
91      }
92  
93      public Group getGroup(long companyId, String name)
94          throws PortalException, SystemException {
95  
96          return groupLocalService.getGroup(companyId, name);
97      }
98  
99      public List<Group> getManageableGroups(String actionId, int max)
100         throws PortalException, SystemException {
101 
102         PermissionChecker permissionChecker = getPermissionChecker();
103 
104         if (permissionChecker.isCompanyAdmin()) {
105             return groupLocalService.search(
106                 permissionChecker.getCompanyId(), null, null, null, 0, max);
107         }
108 
109         List<Group> groups = userPersistence.getGroups(
110             permissionChecker.getUserId(), 0, max);
111 
112         groups = ListUtil.copy(groups);
113 
114         Iterator<Group> itr = groups.iterator();
115 
116         while (itr.hasNext()) {
117             Group group = itr.next();
118 
119             if (!GroupPermissionUtil.contains(
120                     permissionChecker, group.getGroupId(), actionId)) {
121 
122                 itr.remove();
123             }
124         }
125 
126         return groups;
127     }
128 
129     public List<Group> getOrganizationsGroups(
130         List<Organization> organizations) {
131 
132         return groupLocalService.getOrganizationsGroups(organizations);
133     }
134 
135     public Group getUserGroup(long companyId, long userId)
136         throws PortalException, SystemException {
137 
138         return groupLocalService.getUserGroup(companyId, userId);
139     }
140 
141     public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
142         throws PortalException, SystemException {
143 
144         return groupLocalService.getUserGroupsGroups(userGroups);
145     }
146 
147     public List<Group> getUserOrganizationsGroups(
148             long userId, int start, int end)
149         throws PortalException, SystemException {
150 
151         return groupLocalService.getUserOrganizationsGroups(userId, start, end);
152     }
153 
154     public boolean hasUserGroup(long userId, long groupId)
155         throws SystemException {
156 
157         return groupLocalService.hasUserGroup(userId, groupId);
158     }
159 
160     public List<Group> search(
161             long companyId, String name, String description, String[] params,
162             int start, int end)
163         throws SystemException {
164 
165         LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
166             params);
167 
168         return groupLocalService.search(
169             companyId, name, description, paramsObj, start, end);
170     }
171 
172     public int searchCount(
173             long companyId, String name, String description, String[] params)
174         throws SystemException {
175 
176         LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
177             params);
178 
179         return groupLocalService.searchCount(
180             companyId, name, description, paramsObj);
181     }
182 
183     public void setRoleGroups(long roleId, long[] groupIds)
184         throws PortalException, SystemException {
185 
186         RolePermissionUtil.check(
187             getPermissionChecker(), roleId, ActionKeys.UPDATE);
188 
189         groupLocalService.setRoleGroups(roleId, groupIds);
190     }
191 
192     public void unsetRoleGroups(long roleId, long[] groupIds)
193         throws PortalException, SystemException {
194 
195         RolePermissionUtil.check(
196             getPermissionChecker(), roleId, ActionKeys.UPDATE);
197 
198         groupLocalService.unsetRoleGroups(roleId, groupIds);
199     }
200 
201     public Group updateFriendlyURL(long groupId, String friendlyURL)
202         throws PortalException, SystemException {
203 
204         GroupPermissionUtil.check(
205             getPermissionChecker(), groupId, ActionKeys.UPDATE);
206 
207         return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
208     }
209 
210     public Group updateGroup(
211             long groupId, String name, String description, int type,
212             String friendlyURL, boolean active, ServiceContext serviceContext)
213         throws PortalException, SystemException {
214 
215         GroupPermissionUtil.check(
216             getPermissionChecker(), groupId, ActionKeys.UPDATE);
217 
218         return groupLocalService.updateGroup(
219             groupId, name, description, type, friendlyURL, active,
220             serviceContext);
221     }
222 
223     public Group updateGroup(long groupId, String typeSettings)
224         throws PortalException, SystemException {
225 
226         GroupPermissionUtil.check(
227             getPermissionChecker(), groupId, ActionKeys.UPDATE);
228 
229         return groupLocalService.updateGroup(groupId, typeSettings);
230     }
231 
232     public Group updateWorkflow(
233             long groupId, boolean workflowEnabled, int workflowStages,
234             String workflowRoleNames)
235         throws PortalException, SystemException {
236 
237         GroupPermissionUtil.check(
238             getPermissionChecker(), groupId, ActionKeys.MANAGE_STAGING);
239 
240         return groupLocalService.updateWorkflow(
241             groupId, workflowEnabled, workflowStages, workflowRoleNames);
242     }
243 
244 }