1
14
15 package com.liferay.portal.service.impl;
16
17 import com.liferay.portal.DuplicateUserGroupException;
18 import com.liferay.portal.NoSuchUserGroupException;
19 import com.liferay.portal.RequiredUserGroupException;
20 import com.liferay.portal.UserGroupNameException;
21 import com.liferay.portal.kernel.exception.PortalException;
22 import com.liferay.portal.kernel.exception.SystemException;
23 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
24 import com.liferay.portal.kernel.search.Indexer;
25 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
26 import com.liferay.portal.kernel.util.OrderByComparator;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.lar.PortletDataHandlerKeys;
30 import com.liferay.portal.lar.UserIdStrategy;
31 import com.liferay.portal.model.Group;
32 import com.liferay.portal.model.ResourceConstants;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.model.UserGroup;
35 import com.liferay.portal.model.UserGroupConstants;
36 import com.liferay.portal.security.permission.PermissionCacheUtil;
37 import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
38
39 import java.util.ArrayList;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43
44
49 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
50
51 public void addGroupUserGroups(long groupId, long[] userGroupIds)
52 throws SystemException {
53
54 groupPersistence.addUserGroups(groupId, userGroupIds);
55
56 PermissionCacheUtil.clearCache();
57 }
58
59 public UserGroup addUserGroup(
60 long userId, long companyId, String name, String description)
61 throws PortalException, SystemException {
62
63
65 validate(0, companyId, name);
66
67 long userGroupId = counterLocalService.increment();
68
69 UserGroup userGroup = userGroupPersistence.create(userGroupId);
70
71 userGroup.setCompanyId(companyId);
72 userGroup.setParentUserGroupId(
73 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
74 userGroup.setName(name);
75 userGroup.setDescription(description);
76
77 userGroupPersistence.update(userGroup, false);
78
79
81 groupLocalService.addGroup(
82 userId, UserGroup.class.getName(), userGroup.getUserGroupId(),
83 String.valueOf(userGroupId), null, 0, null, true, null);
84
85
87 resourceLocalService.addResources(
88 companyId, 0, userId, UserGroup.class.getName(),
89 userGroup.getUserGroupId(), false, false, false);
90
91 return userGroup;
92 }
93
94 public void clearUserUserGroups(long userId) throws SystemException {
95 userPersistence.clearUserGroups(userId);
96
97 PermissionCacheUtil.clearCache();
98 }
99
100 public void copyUserGroupLayouts(long userGroupId, long userIds[])
101 throws PortalException, SystemException {
102
103 for (long userId : userIds) {
104 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
105 copyUserGroupLayouts(userGroupId, userId);
106 }
107 }
108 }
109
110 public void copyUserGroupLayouts(long userGroupIds[], long userId)
111 throws PortalException, SystemException {
112
113 for (long userGroupId : userGroupIds) {
114 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
115 copyUserGroupLayouts(userGroupId, userId);
116 }
117 }
118 }
119
120 public void copyUserGroupLayouts(long userGroupId, long userId)
121 throws PortalException, SystemException {
122
123 UserGroup userGroup = userGroupLocalService.getUserGroup(userGroupId);
124 User user = userPersistence.findByPrimaryKey(userId);
125
126 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
127
128 if (userGroup.hasPrivateLayouts()) {
129 long sourceGroupId = userGroup.getGroup().getGroupId();
130 long targetGroupId = user.getGroup().getGroupId();
131
132 byte[] bytes = layoutLocalService.exportLayouts(
133 sourceGroupId, true, parameterMap, null, null);
134
135 UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
136 bytes);
137
138 layoutLocalService.importLayouts(
139 userId, targetGroupId, true, parameterMap, ubais);
140 }
141
142 if (userGroup.hasPublicLayouts()) {
143 long sourceGroupId = userGroup.getGroup().getGroupId();
144 long targetGroupId = user.getGroup().getGroupId();
145
146 byte[] bytes = layoutLocalService.exportLayouts(
147 sourceGroupId, false, parameterMap, null, null);
148
149 UnsyncByteArrayInputStream ubais = new UnsyncByteArrayInputStream(
150 bytes);
151
152 layoutLocalService.importLayouts(
153 userId, targetGroupId, false, parameterMap, ubais);
154 }
155 }
156
157 public void deleteUserGroup(long userGroupId)
158 throws PortalException, SystemException {
159
160 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
161 userGroupId);
162
163 if (userLocalService.getUserGroupUsersCount(userGroupId, true) > 0) {
164 throw new RequiredUserGroupException();
165 }
166
167
169 clearUserUserGroups(userGroupId);
170
171
173 Group group = userGroup.getGroup();
174
175 groupLocalService.deleteGroup(group.getGroupId());
176
177
179 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
180 userGroupId);
181
182
184 resourceLocalService.deleteResource(
185 userGroup.getCompanyId(), UserGroup.class.getName(),
186 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
187
188
190 userGroupPersistence.remove(userGroupId);
191
192
194 PermissionCacheUtil.clearCache();
195 }
196
197 public UserGroup getUserGroup(long userGroupId)
198 throws PortalException, SystemException {
199
200 return userGroupPersistence.findByPrimaryKey(userGroupId);
201 }
202
203 public UserGroup getUserGroup(long companyId, String name)
204 throws PortalException, SystemException {
205
206 return userGroupPersistence.findByC_N(companyId, name);
207 }
208
209 public List<UserGroup> getUserGroups(long companyId)
210 throws SystemException {
211
212 return userGroupPersistence.findByCompanyId(companyId);
213 }
214
215 public List<UserGroup> getUserGroups(long[] userGroupIds)
216 throws PortalException, SystemException {
217
218 List<UserGroup> userGroups = new ArrayList<UserGroup>(
219 userGroupIds.length);
220
221 for (long userGroupId : userGroupIds) {
222 UserGroup userGroup = getUserGroup(userGroupId);
223
224 userGroups.add(userGroup);
225 }
226
227 return userGroups;
228 }
229
230 public List<UserGroup> getUserUserGroups(long userId)
231 throws SystemException {
232
233 return userPersistence.getUserGroups(userId);
234 }
235
236 public boolean hasGroupUserGroup(long groupId, long userGroupId)
237 throws SystemException {
238
239 return groupPersistence.containsUserGroup(groupId, userGroupId);
240 }
241
242 public List<UserGroup> search(
243 long companyId, String name, String description,
244 LinkedHashMap<String, Object> params, int start, int end,
245 OrderByComparator obc)
246 throws SystemException {
247
248 return userGroupFinder.findByC_N_D(
249 companyId, name, description, params, start, end, obc);
250 }
251
252 public int searchCount(
253 long companyId, String name, String description,
254 LinkedHashMap<String, Object> params)
255 throws SystemException {
256
257 return userGroupFinder.countByC_N_D(
258 companyId, name, description, params);
259 }
260
261 public void setUserUserGroups(long userId, long[] userGroupIds)
262 throws PortalException, SystemException {
263
264 copyUserGroupLayouts(userGroupIds, userId);
265
266 userPersistence.setUserGroups(userId, userGroupIds);
267
268 Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
269
270 indexer.reindex(userId);
271
272 PermissionCacheUtil.clearCache();
273 }
274
275 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
276 throws SystemException {
277
278 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
279 userGroupIds, groupId);
280
281 groupPersistence.removeUserGroups(groupId, userGroupIds);
282
283 PermissionCacheUtil.clearCache();
284 }
285
286 public UserGroup updateUserGroup(
287 long companyId, long userGroupId, String name,
288 String description)
289 throws PortalException, SystemException {
290
291 validate(userGroupId, companyId, name);
292
293 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
294 userGroupId);
295
296 userGroup.setName(name);
297 userGroup.setDescription(description);
298
299 userGroupPersistence.update(userGroup, false);
300
301 return userGroup;
302 }
303
304 protected Map<String, String[]> getLayoutTemplatesParameters() {
305 Map<String, String[]> parameterMap =
306 new LinkedHashMap<String, String[]>();
307
308 parameterMap.put(
309 PortletDataHandlerKeys.CATEGORIES,
310 new String[] {Boolean.TRUE.toString()});
311 parameterMap.put(
312 PortletDataHandlerKeys.DATA_STRATEGY,
313 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
314 parameterMap.put(
315 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
316 new String[] {Boolean.FALSE.toString()});
317 parameterMap.put(
318 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
319 new String[] {Boolean.FALSE.toString()});
320 parameterMap.put(
321 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
322 new String[] {PortletDataHandlerKeys.
323 LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME});
324 parameterMap.put(
325 PortletDataHandlerKeys.PERMISSIONS,
326 new String[] {Boolean.TRUE.toString()});
327 parameterMap.put(
328 PortletDataHandlerKeys.PORTLET_DATA,
329 new String[] {Boolean.TRUE.toString()});
330 parameterMap.put(
331 PortletDataHandlerKeys.PORTLET_DATA_ALL,
332 new String[] {Boolean.TRUE.toString()});
333 parameterMap.put(
334 PortletDataHandlerKeys.PORTLET_SETUP,
335 new String[] {Boolean.TRUE.toString()});
336 parameterMap.put(
337 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
338 new String[] {Boolean.TRUE.toString()});
339 parameterMap.put(
340 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
341 new String[] {PortletDataHandlerKeys.
342 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
343 parameterMap.put(
344 PortletDataHandlerKeys.THEME,
345 new String[] {Boolean.FALSE.toString()});
346 parameterMap.put(
347 PortletDataHandlerKeys.USER_ID_STRATEGY,
348 new String[] {UserIdStrategy.CURRENT_USER_ID});
349 parameterMap.put(
350 PortletDataHandlerKeys.USER_PERMISSIONS,
351 new String[] {Boolean.FALSE.toString()});
352
353 return parameterMap;
354 }
355
356 protected void validate(long userGroupId, long companyId, String name)
357 throws PortalException, SystemException {
358
359 if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
360 (name.indexOf(StringPool.COMMA) != -1) ||
361 (name.indexOf(StringPool.STAR) != -1)) {
362
363 throw new UserGroupNameException();
364 }
365
366 try {
367 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
368
369 if (userGroup.getUserGroupId() != userGroupId) {
370 throw new DuplicateUserGroupException();
371 }
372 }
373 catch (NoSuchUserGroupException nsuge) {
374 }
375 }
376
377 }