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.GroupConstants;
033 import com.liferay.portal.model.ResourceConstants;
034 import com.liferay.portal.model.Team;
035 import com.liferay.portal.model.User;
036 import com.liferay.portal.model.UserGroup;
037 import com.liferay.portal.model.UserGroupConstants;
038 import com.liferay.portal.security.ldap.LDAPUserGroupTransactionThreadLocal;
039 import com.liferay.portal.security.permission.PermissionCacheUtil;
040 import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
041 import com.liferay.portal.util.PropsValues;
042
043 import java.io.File;
044
045 import java.util.ArrayList;
046 import java.util.LinkedHashMap;
047 import java.util.List;
048 import java.util.Map;
049
050
055 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
056
057
064 public void addGroupUserGroups(long groupId, long[] userGroupIds)
065 throws SystemException {
066
067 groupPersistence.addUserGroups(groupId, userGroupIds);
068
069 PermissionCacheUtil.clearCache();
070 }
071
072
079 public void addTeamUserGroups(long teamId, long[] userGroupIds)
080 throws SystemException {
081
082 teamPersistence.addUserGroups(teamId, userGroupIds);
083
084 PermissionCacheUtil.clearCache();
085 }
086
087
105 public UserGroup addUserGroup(
106 long userId, long companyId, String name, String description)
107 throws PortalException, SystemException {
108
109
110
111 validate(0, companyId, name);
112
113 long userGroupId = counterLocalService.increment();
114
115 UserGroup userGroup = userGroupPersistence.create(userGroupId);
116
117 userGroup.setCompanyId(companyId);
118 userGroup.setParentUserGroupId(
119 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
120 userGroup.setName(name);
121 userGroup.setDescription(description);
122 userGroup.setAddedByLDAPImport(
123 LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
124
125 userGroupPersistence.update(userGroup);
126
127
128
129 groupLocalService.addGroup(
130 userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
131 UserGroup.class.getName(), userGroup.getUserGroupId(),
132 GroupConstants.DEFAULT_LIVE_GROUP_ID, String.valueOf(userGroupId),
133 null, 0, null, false, true, null);
134
135
136
137 resourceLocalService.addResources(
138 companyId, 0, userId, UserGroup.class.getName(),
139 userGroup.getUserGroupId(), false, false, false);
140
141 return userGroup;
142 }
143
144
155 public void clearUserUserGroups(long userId) throws SystemException {
156 userPersistence.clearUserGroups(userId);
157
158 PermissionCacheUtil.clearCache();
159 }
160
161
172 public void copyUserGroupLayouts(long userGroupId, long userIds[])
173 throws PortalException, SystemException {
174
175 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
176
177 File[] files = exportLayouts(userGroupId, parameterMap);
178
179 try {
180 for (long userId : userIds) {
181 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
182 importLayouts(userId, parameterMap, files[0], files[1]);
183 }
184 }
185 }
186 finally {
187 if (files[0] != null) {
188 files[0].delete();
189 }
190
191 if (files[1] != null) {
192 files[1].delete();
193 }
194 }
195 }
196
197
207 public void copyUserGroupLayouts(long userGroupIds[], long userId)
208 throws PortalException, SystemException {
209
210 for (long userGroupId : userGroupIds) {
211 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
212 copyUserGroupLayouts(userGroupId, userId);
213 }
214 }
215 }
216
217
227 public void copyUserGroupLayouts(long userGroupId, long userId)
228 throws PortalException, SystemException {
229
230 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
231
232 File[] files = exportLayouts(userGroupId, parameterMap);
233
234 try {
235 importLayouts(userId, parameterMap, files[0], files[1]);
236 }
237 finally {
238 if (files[0] != null) {
239 files[0].delete();
240 }
241
242 if (files[1] != null) {
243 files[1].delete();
244 }
245 }
246 }
247
248
257 @Override
258 public UserGroup deleteUserGroup(long userGroupId)
259 throws PortalException, SystemException {
260
261 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
262 userGroupId);
263
264 return deleteUserGroup(userGroup);
265 }
266
267
276 @Override
277 public UserGroup deleteUserGroup(UserGroup userGroup)
278 throws PortalException, SystemException {
279
280 int count = userLocalService.getUserGroupUsersCount(
281 userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
282
283 if (count > 0) {
284 throw new RequiredUserGroupException();
285 }
286
287
288
289 clearUserUserGroups(userGroup.getUserGroupId());
290
291
292
293 Group group = userGroup.getGroup();
294
295 groupLocalService.deleteGroup(group);
296
297
298
299 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
300 userGroup.getUserGroupId());
301
302
303
304 resourceLocalService.deleteResource(
305 userGroup.getCompanyId(), UserGroup.class.getName(),
306 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
307
308
309
310 userGroupPersistence.remove(userGroup);
311
312
313
314 PermissionCacheUtil.clearCache();
315
316 return userGroup;
317 }
318
319
328 public UserGroup getUserGroup(long companyId, String name)
329 throws PortalException, SystemException {
330
331 return userGroupPersistence.findByC_N(companyId, name);
332 }
333
334
341 public List<UserGroup> getUserGroups(long companyId)
342 throws SystemException {
343
344 return userGroupPersistence.findByCompanyId(companyId);
345 }
346
347
355 public List<UserGroup> getUserGroups(long[] userGroupIds)
356 throws PortalException, SystemException {
357
358 List<UserGroup> userGroups = new ArrayList<UserGroup>(
359 userGroupIds.length);
360
361 for (long userGroupId : userGroupIds) {
362 UserGroup userGroup = getUserGroup(userGroupId);
363
364 userGroups.add(userGroup);
365 }
366
367 return userGroups;
368 }
369
370
377 public List<UserGroup> getUserUserGroups(long userId)
378 throws SystemException {
379
380 return userPersistence.getUserGroups(userId);
381 }
382
383
392 public boolean hasGroupUserGroup(long groupId, long userGroupId)
393 throws SystemException {
394
395 return groupPersistence.containsUserGroup(groupId, userGroupId);
396 }
397
398
407 public boolean hasTeamUserGroup(long teamId, long userGroupId)
408 throws SystemException {
409
410 return teamPersistence.containsUserGroup(teamId, userGroupId);
411 }
412
413
441 public List<UserGroup> search(
442 long companyId, String keywords,
443 LinkedHashMap<String, Object> params, int start, int end,
444 OrderByComparator obc)
445 throws SystemException {
446
447 return userGroupFinder.findByKeywords(
448 companyId, keywords, params, start, end, obc);
449 }
450
451
481 public List<UserGroup> search(
482 long companyId, String name, String description,
483 LinkedHashMap<String, Object> params, int start, int end,
484 OrderByComparator obc)
485 throws SystemException {
486
487 return userGroupFinder.findByC_N_D(
488 companyId, name, description, params, false, start, end, obc);
489 }
490
491
504 public int searchCount(
505 long companyId, String keywords,
506 LinkedHashMap<String, Object> params)
507 throws SystemException {
508
509 return userGroupFinder.countByKeywords(companyId, keywords, params);
510 }
511
512
526 public int searchCount(
527 long companyId, String name, String description,
528 LinkedHashMap<String, Object> params)
529 throws SystemException {
530
531 return userGroupFinder.countByC_N_D(
532 companyId, name, description, params, false);
533 }
534
535
545 public void setUserUserGroups(long userId, long[] userGroupIds)
546 throws PortalException, SystemException {
547
548 copyUserGroupLayouts(userGroupIds, userId);
549
550 userPersistence.setUserGroups(userId, userGroupIds);
551
552 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
553
554 indexer.reindex(userId);
555
556 PermissionCacheUtil.clearCache();
557 }
558
559
566 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
567 throws SystemException {
568
569 List<Team> teams = teamPersistence.findByGroupId(groupId);
570
571 for (Team team : teams) {
572 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
573 }
574
575 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
576 userGroupIds, groupId);
577
578 groupPersistence.removeUserGroups(groupId, userGroupIds);
579
580 PermissionCacheUtil.clearCache();
581 }
582
583
590 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
591 throws SystemException {
592
593 teamPersistence.removeUserGroups(teamId, userGroupIds);
594
595 PermissionCacheUtil.clearCache();
596 }
597
598
610 public UserGroup updateUserGroup(
611 long companyId, long userGroupId, String name, String description)
612 throws PortalException, SystemException {
613
614 validate(userGroupId, companyId, name);
615
616 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
617 userGroupId);
618
619 userGroup.setName(name);
620 userGroup.setDescription(description);
621
622 userGroupPersistence.update(userGroup);
623
624 return userGroup;
625 }
626
627 protected File[] exportLayouts(
628 long userGroupId, Map<String, String[]> parameterMap)
629 throws PortalException, SystemException {
630
631 File[] files = new File[2];
632
633 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
634 userGroupId);
635
636 Group group = userGroup.getGroup();
637
638 if (userGroup.hasPrivateLayouts()) {
639 files[0] = layoutLocalService.exportLayoutsAsFile(
640 group.getGroupId(), true, null, parameterMap, null, null);
641 }
642
643 if (userGroup.hasPublicLayouts()) {
644 files[1] = layoutLocalService.exportLayoutsAsFile(
645 group.getGroupId(), false, null, parameterMap, null, null);
646 }
647
648 return files;
649 }
650
651 protected Map<String, String[]> getLayoutTemplatesParameters() {
652 Map<String, String[]> parameterMap =
653 new LinkedHashMap<String, String[]>();
654
655 parameterMap.put(
656 PortletDataHandlerKeys.CATEGORIES,
657 new String[] {Boolean.TRUE.toString()});
658 parameterMap.put(
659 PortletDataHandlerKeys.DATA_STRATEGY,
660 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
661 parameterMap.put(
662 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
663 new String[] {Boolean.FALSE.toString()});
664 parameterMap.put(
665 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
666 new String[] {Boolean.FALSE.toString()});
667 parameterMap.put(
668 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
669 new String[] {Boolean.FALSE.toString()});
670 parameterMap.put(
671 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
672 new String[] {PortletDataHandlerKeys.
673 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
674 parameterMap.put(
675 PortletDataHandlerKeys.LOGO,
676 new String[] {Boolean.FALSE.toString()});
677 parameterMap.put(
678 PortletDataHandlerKeys.PERMISSIONS,
679 new String[] {Boolean.TRUE.toString()});
680 parameterMap.put(
681 PortletDataHandlerKeys.PORTLET_DATA,
682 new String[] {Boolean.TRUE.toString()});
683 parameterMap.put(
684 PortletDataHandlerKeys.PORTLET_DATA_ALL,
685 new String[] {Boolean.TRUE.toString()});
686 parameterMap.put(
687 PortletDataHandlerKeys.PORTLET_SETUP,
688 new String[] {Boolean.TRUE.toString()});
689 parameterMap.put(
690 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
691 new String[] {Boolean.TRUE.toString()});
692 parameterMap.put(
693 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
694 new String[] {PortletDataHandlerKeys.
695 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
696 parameterMap.put(
697 PortletDataHandlerKeys.THEME,
698 new String[] {Boolean.FALSE.toString()});
699 parameterMap.put(
700 PortletDataHandlerKeys.THEME_REFERENCE,
701 new String[] {Boolean.TRUE.toString()});
702 parameterMap.put(
703 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
704 new String[] {Boolean.FALSE.toString()});
705 parameterMap.put(
706 PortletDataHandlerKeys.USER_ID_STRATEGY,
707 new String[] {UserIdStrategy.CURRENT_USER_ID});
708
709 return parameterMap;
710 }
711
712 protected void importLayouts(
713 long userId, Map<String, String[]> parameterMap,
714 File privateLayoutsFile, File publicLayoutsFile)
715 throws PortalException, SystemException {
716
717 User user = userPersistence.findByPrimaryKey(userId);
718
719 long groupId = user.getGroup().getGroupId();
720
721 if (privateLayoutsFile != null) {
722 layoutLocalService.importLayouts(
723 userId, groupId, true, parameterMap, privateLayoutsFile);
724 }
725
726 if (publicLayoutsFile != null) {
727 layoutLocalService.importLayouts(
728 userId, groupId, false, parameterMap, publicLayoutsFile);
729 }
730 }
731
732 protected void validate(long userGroupId, long companyId, String name)
733 throws PortalException, SystemException {
734
735 if (Validator.isNull(name) ||
736 (name.indexOf(CharPool.COMMA) != -1) ||
737 (name.indexOf(CharPool.STAR) != -1)) {
738
739 throw new UserGroupNameException();
740 }
741
742 if (Validator.isNumber(name) &&
743 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
744
745 throw new UserGroupNameException();
746 }
747
748 try {
749 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
750
751 if (userGroup.getUserGroupId() != userGroupId) {
752 throw new DuplicateUserGroupException();
753 }
754 }
755 catch (NoSuchUserGroupException nsuge) {
756 }
757 }
758
759 }