001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.DuplicateUserGroupException;
018 import com.liferay.portal.NoSuchUserGroupException;
019 import com.liferay.portal.RequiredUserGroupException;
020 import com.liferay.portal.UserGroupNameException;
021 import com.liferay.portal.kernel.exception.PortalException;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
024 import com.liferay.portal.kernel.lar.UserIdStrategy;
025 import com.liferay.portal.kernel.search.Indexer;
026 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027 import com.liferay.portal.kernel.util.CharPool;
028 import com.liferay.portal.kernel.util.OrderByComparator;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.kernel.workflow.WorkflowConstants;
031 import com.liferay.portal.model.Group;
032 import com.liferay.portal.model.ResourceConstants;
033 import com.liferay.portal.model.Team;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.model.UserGroup;
036 import com.liferay.portal.model.UserGroupConstants;
037 import com.liferay.portal.security.ldap.LDAPUserGroupTransactionThreadLocal;
038 import com.liferay.portal.security.permission.PermissionCacheUtil;
039 import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
040 import com.liferay.portal.util.PropsValues;
041
042 import java.io.File;
043
044 import java.util.ArrayList;
045 import java.util.LinkedHashMap;
046 import java.util.List;
047 import java.util.Map;
048
049
054 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
055
056
063 public void addGroupUserGroups(long groupId, long[] userGroupIds)
064 throws SystemException {
065
066 groupPersistence.addUserGroups(groupId, userGroupIds);
067
068 PermissionCacheUtil.clearCache();
069 }
070
071
078 public void addTeamUserGroups(long teamId, long[] userGroupIds)
079 throws SystemException {
080
081 teamPersistence.addUserGroups(teamId, userGroupIds);
082
083 PermissionCacheUtil.clearCache();
084 }
085
086
104 public UserGroup addUserGroup(
105 long userId, long companyId, String name, String description)
106 throws PortalException, SystemException {
107
108
109
110 validate(0, companyId, name);
111
112 long userGroupId = counterLocalService.increment();
113
114 UserGroup userGroup = userGroupPersistence.create(userGroupId);
115
116 userGroup.setCompanyId(companyId);
117 userGroup.setParentUserGroupId(
118 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
119 userGroup.setName(name);
120 userGroup.setDescription(description);
121 userGroup.setAddedByLDAPImport(
122 LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
123
124 userGroupPersistence.update(userGroup, false);
125
126
127
128 groupLocalService.addGroup(
129 userId, UserGroup.class.getName(), userGroup.getUserGroupId(),
130 String.valueOf(userGroupId), null, 0, null, false, true, null);
131
132
133
134 resourceLocalService.addResources(
135 companyId, 0, userId, UserGroup.class.getName(),
136 userGroup.getUserGroupId(), false, false, false);
137
138 return userGroup;
139 }
140
141
152 public void clearUserUserGroups(long userId) throws SystemException {
153 userPersistence.clearUserGroups(userId);
154
155 PermissionCacheUtil.clearCache();
156 }
157
158
169 public void copyUserGroupLayouts(long userGroupId, long userIds[])
170 throws PortalException, SystemException {
171
172 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
173
174 File[] files = exportLayouts(userGroupId, parameterMap);
175
176 try {
177 for (long userId : userIds) {
178 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
179 importLayouts(userId, parameterMap, files[0], files[1]);
180 }
181 }
182 }
183 finally {
184 if (files[0] != null) {
185 files[0].delete();
186 }
187
188 if (files[1] != null) {
189 files[1].delete();
190 }
191 }
192 }
193
194
204 public void copyUserGroupLayouts(long userGroupIds[], long userId)
205 throws PortalException, SystemException {
206
207 for (long userGroupId : userGroupIds) {
208 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
209 copyUserGroupLayouts(userGroupId, userId);
210 }
211 }
212 }
213
214
224 public void copyUserGroupLayouts(long userGroupId, long userId)
225 throws PortalException, SystemException {
226
227 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
228
229 File[] files = exportLayouts(userGroupId, parameterMap);
230
231 try {
232 importLayouts(userId, parameterMap, files[0], files[1]);
233 }
234 finally {
235 if (files[0] != null) {
236 files[0].delete();
237 }
238
239 if (files[1] != null) {
240 files[1].delete();
241 }
242 }
243 }
244
245
253 @Override
254 public UserGroup deleteUserGroup(long userGroupId)
255 throws PortalException, SystemException {
256
257 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
258 userGroupId);
259
260 return deleteUserGroup(userGroup);
261 }
262
263
271 @Override
272 public UserGroup deleteUserGroup(UserGroup userGroup)
273 throws PortalException, SystemException {
274
275 int count = userLocalService.getUserGroupUsersCount(
276 userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
277
278 if (count > 0) {
279 throw new RequiredUserGroupException();
280 }
281
282
283
284 clearUserUserGroups(userGroup.getUserGroupId());
285
286
287
288 Group group = userGroup.getGroup();
289
290 groupLocalService.deleteGroup(group);
291
292
293
294 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
295 userGroup.getUserGroupId());
296
297
298
299 resourceLocalService.deleteResource(
300 userGroup.getCompanyId(), UserGroup.class.getName(),
301 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
302
303
304
305 userGroupPersistence.remove(userGroup);
306
307
308
309 PermissionCacheUtil.clearCache();
310
311 return userGroup;
312 }
313
314
323 public UserGroup getUserGroup(long companyId, String name)
324 throws PortalException, SystemException {
325
326 return userGroupPersistence.findByC_N(companyId, name);
327 }
328
329
336 public List<UserGroup> getUserGroups(long companyId)
337 throws SystemException {
338
339 return userGroupPersistence.findByCompanyId(companyId);
340 }
341
342
350 public List<UserGroup> getUserGroups(long[] userGroupIds)
351 throws PortalException, SystemException {
352
353 List<UserGroup> userGroups = new ArrayList<UserGroup>(
354 userGroupIds.length);
355
356 for (long userGroupId : userGroupIds) {
357 UserGroup userGroup = getUserGroup(userGroupId);
358
359 userGroups.add(userGroup);
360 }
361
362 return userGroups;
363 }
364
365
372 public List<UserGroup> getUserUserGroups(long userId)
373 throws SystemException {
374
375 return userPersistence.getUserGroups(userId);
376 }
377
378
387 public boolean hasGroupUserGroup(long groupId, long userGroupId)
388 throws SystemException {
389
390 return groupPersistence.containsUserGroup(groupId, userGroupId);
391 }
392
393
402 public boolean hasTeamUserGroup(long teamId, long userGroupId)
403 throws SystemException {
404
405 return teamPersistence.containsUserGroup(teamId, userGroupId);
406 }
407
408 public List<UserGroup> search(
409 long companyId, String keywords,
410 LinkedHashMap<String, Object> params, int start, int end,
411 OrderByComparator obc)
412 throws SystemException {
413
414 return userGroupFinder.findByKeywords(
415 companyId, keywords, params, start, end, obc);
416 }
417
418
448 public List<UserGroup> search(
449 long companyId, String name, String description,
450 LinkedHashMap<String, Object> params, int start, int end,
451 OrderByComparator obc)
452 throws SystemException {
453
454 return userGroupFinder.findByC_N_D(
455 companyId, name, description, params, false, start, end, obc);
456 }
457
458 public int searchCount(
459 long companyId, String keywords,
460 LinkedHashMap<String, Object> params)
461 throws SystemException {
462
463 return userGroupFinder.countByKeywords(companyId, keywords, params);
464 }
465
466
480 public int searchCount(
481 long companyId, String name, String description,
482 LinkedHashMap<String, Object> params)
483 throws SystemException {
484
485 return userGroupFinder.countByC_N_D(
486 companyId, name, description, params, false);
487 }
488
489
499 public void setUserUserGroups(long userId, long[] userGroupIds)
500 throws PortalException, SystemException {
501
502 copyUserGroupLayouts(userGroupIds, userId);
503
504 userPersistence.setUserGroups(userId, userGroupIds);
505
506 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
507
508 indexer.reindex(userId);
509
510 PermissionCacheUtil.clearCache();
511 }
512
513
520 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
521 throws SystemException {
522
523 List<Team> teams = teamPersistence.findByGroupId(groupId);
524
525 for (Team team : teams) {
526 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
527 }
528
529 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
530 userGroupIds, groupId);
531
532 groupPersistence.removeUserGroups(groupId, userGroupIds);
533
534 PermissionCacheUtil.clearCache();
535 }
536
537
544 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
545 throws SystemException {
546
547 teamPersistence.removeUserGroups(teamId, userGroupIds);
548
549 PermissionCacheUtil.clearCache();
550 }
551
552
564 public UserGroup updateUserGroup(
565 long companyId, long userGroupId, String name, String description)
566 throws PortalException, SystemException {
567
568 validate(userGroupId, companyId, name);
569
570 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
571 userGroupId);
572
573 userGroup.setName(name);
574 userGroup.setDescription(description);
575
576 userGroupPersistence.update(userGroup, false);
577
578 return userGroup;
579 }
580
581 protected File[] exportLayouts(
582 long userGroupId, Map<String, String[]> parameterMap)
583 throws PortalException, SystemException {
584
585 File[] files = new File[2];
586
587 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
588 userGroupId);
589
590 Group group = userGroup.getGroup();
591
592 if (userGroup.hasPrivateLayouts()) {
593 files[0] = layoutLocalService.exportLayoutsAsFile(
594 group.getGroupId(), true, null, parameterMap, null, null);
595 }
596
597 if (userGroup.hasPublicLayouts()) {
598 files[1] = layoutLocalService.exportLayoutsAsFile(
599 group.getGroupId(), false, null, parameterMap, null, null);
600 }
601
602 return files;
603 }
604
605 protected Map<String, String[]> getLayoutTemplatesParameters() {
606 Map<String, String[]> parameterMap =
607 new LinkedHashMap<String, String[]>();
608
609 parameterMap.put(
610 PortletDataHandlerKeys.CATEGORIES,
611 new String[] {Boolean.TRUE.toString()});
612 parameterMap.put(
613 PortletDataHandlerKeys.DATA_STRATEGY,
614 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
615 parameterMap.put(
616 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
617 new String[] {Boolean.FALSE.toString()});
618 parameterMap.put(
619 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
620 new String[] {Boolean.FALSE.toString()});
621 parameterMap.put(
622 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
623 new String[] {Boolean.FALSE.toString()});
624 parameterMap.put(
625 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
626 new String[] {PortletDataHandlerKeys.
627 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
628 parameterMap.put(
629 PortletDataHandlerKeys.LOGO,
630 new String[] {Boolean.FALSE.toString()});
631 parameterMap.put(
632 PortletDataHandlerKeys.PERMISSIONS,
633 new String[] {Boolean.TRUE.toString()});
634 parameterMap.put(
635 PortletDataHandlerKeys.PORTLET_DATA,
636 new String[] {Boolean.TRUE.toString()});
637 parameterMap.put(
638 PortletDataHandlerKeys.PORTLET_DATA_ALL,
639 new String[] {Boolean.TRUE.toString()});
640 parameterMap.put(
641 PortletDataHandlerKeys.PORTLET_SETUP,
642 new String[] {Boolean.TRUE.toString()});
643 parameterMap.put(
644 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
645 new String[] {Boolean.TRUE.toString()});
646 parameterMap.put(
647 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
648 new String[] {PortletDataHandlerKeys.
649 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
650 parameterMap.put(
651 PortletDataHandlerKeys.THEME,
652 new String[] {Boolean.FALSE.toString()});
653 parameterMap.put(
654 PortletDataHandlerKeys.THEME_REFERENCE,
655 new String[] {Boolean.TRUE.toString()});
656 parameterMap.put(
657 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
658 new String[] {Boolean.FALSE.toString()});
659 parameterMap.put(
660 PortletDataHandlerKeys.USER_ID_STRATEGY,
661 new String[] {UserIdStrategy.CURRENT_USER_ID});
662 parameterMap.put(
663 PortletDataHandlerKeys.USER_PERMISSIONS,
664 new String[] {Boolean.FALSE.toString()});
665
666 return parameterMap;
667 }
668
669 protected void importLayouts(
670 long userId, Map<String, String[]> parameterMap,
671 File privateLayoutsFile, File publicLayoutsFile)
672 throws PortalException, SystemException {
673
674 User user = userPersistence.findByPrimaryKey(userId);
675
676 long groupId = user.getGroup().getGroupId();
677
678 if (privateLayoutsFile != null) {
679 layoutLocalService.importLayouts(
680 userId, groupId, true, parameterMap, privateLayoutsFile);
681 }
682
683 if (publicLayoutsFile != null) {
684 layoutLocalService.importLayouts(
685 userId, groupId, false, parameterMap, publicLayoutsFile);
686 }
687 }
688
689 protected void validate(long userGroupId, long companyId, String name)
690 throws PortalException, SystemException {
691
692 if (Validator.isNull(name) ||
693 (name.indexOf(CharPool.COMMA) != -1) ||
694 (name.indexOf(CharPool.STAR) != -1)) {
695
696 throw new UserGroupNameException();
697 }
698
699 if (Validator.isNumber(name) &&
700 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
701
702 throw new UserGroupNameException();
703 }
704
705 try {
706 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
707
708 if (userGroup.getUserGroupId() != userGroupId) {
709 throw new DuplicateUserGroupException();
710 }
711 }
712 catch (NoSuchUserGroupException nsuge) {
713 }
714 }
715
716 }