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