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 void deleteUserGroup(long userGroupId)
255 throws PortalException, SystemException {
256
257 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
258 userGroupId);
259
260 deleteUserGroup(userGroup);
261 }
262
263
271 @Override
272 public void 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
312
321 @Override
322 public UserGroup getUserGroup(long userGroupId)
323 throws PortalException, SystemException {
324
325 return userGroupPersistence.findByPrimaryKey(userGroupId);
326 }
327
328
337 public UserGroup getUserGroup(long companyId, String name)
338 throws PortalException, SystemException {
339
340 return userGroupPersistence.findByC_N(companyId, name);
341 }
342
343
350 public List<UserGroup> getUserGroups(long companyId)
351 throws SystemException {
352
353 return userGroupPersistence.findByCompanyId(companyId);
354 }
355
356
364 public List<UserGroup> getUserGroups(long[] userGroupIds)
365 throws PortalException, SystemException {
366
367 List<UserGroup> userGroups = new ArrayList<UserGroup>(
368 userGroupIds.length);
369
370 for (long userGroupId : userGroupIds) {
371 UserGroup userGroup = getUserGroup(userGroupId);
372
373 userGroups.add(userGroup);
374 }
375
376 return userGroups;
377 }
378
379
386 public List<UserGroup> getUserUserGroups(long userId)
387 throws SystemException {
388
389 return userPersistence.getUserGroups(userId);
390 }
391
392
401 public boolean hasGroupUserGroup(long groupId, long userGroupId)
402 throws SystemException {
403
404 return groupPersistence.containsUserGroup(groupId, userGroupId);
405 }
406
407
416 public boolean hasTeamUserGroup(long teamId, long userGroupId)
417 throws SystemException {
418
419 return teamPersistence.containsUserGroup(teamId, userGroupId);
420 }
421
422 public List<UserGroup> search(
423 long companyId, String keywords,
424 LinkedHashMap<String, Object> params, int start, int end,
425 OrderByComparator obc)
426 throws SystemException {
427
428 return userGroupFinder.findByKeywords(
429 companyId, keywords, params, start, end, obc);
430 }
431
432
462 public List<UserGroup> search(
463 long companyId, String name, String description,
464 LinkedHashMap<String, Object> params, int start, int end,
465 OrderByComparator obc)
466 throws SystemException {
467
468 return userGroupFinder.findByC_N_D(
469 companyId, name, description, params, false, start, end, obc);
470 }
471
472 public int searchCount(
473 long companyId, String keywords,
474 LinkedHashMap<String, Object> params)
475 throws SystemException {
476
477 return userGroupFinder.countByKeywords(companyId, keywords, params);
478 }
479
480
494 public int searchCount(
495 long companyId, String name, String description,
496 LinkedHashMap<String, Object> params)
497 throws SystemException {
498
499 return userGroupFinder.countByC_N_D(
500 companyId, name, description, params, false);
501 }
502
503
513 public void setUserUserGroups(long userId, long[] userGroupIds)
514 throws PortalException, SystemException {
515
516 copyUserGroupLayouts(userGroupIds, userId);
517
518 userPersistence.setUserGroups(userId, userGroupIds);
519
520 Indexer indexer = IndexerRegistryUtil.getIndexer(User.class);
521
522 indexer.reindex(userId);
523
524 PermissionCacheUtil.clearCache();
525 }
526
527
534 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
535 throws SystemException {
536
537 List<Team> teams = teamPersistence.findByGroupId(groupId);
538
539 for (Team team : teams) {
540 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
541 }
542
543 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
544 userGroupIds, groupId);
545
546 groupPersistence.removeUserGroups(groupId, userGroupIds);
547
548 PermissionCacheUtil.clearCache();
549 }
550
551
558 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
559 throws SystemException {
560
561 teamPersistence.removeUserGroups(teamId, userGroupIds);
562
563 PermissionCacheUtil.clearCache();
564 }
565
566
578 public UserGroup updateUserGroup(
579 long companyId, long userGroupId, String name, String description)
580 throws PortalException, SystemException {
581
582 validate(userGroupId, companyId, name);
583
584 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
585 userGroupId);
586
587 userGroup.setName(name);
588 userGroup.setDescription(description);
589
590 userGroupPersistence.update(userGroup, false);
591
592 return userGroup;
593 }
594
595 protected File[] exportLayouts(
596 long userGroupId, Map<String, String[]> parameterMap)
597 throws PortalException, SystemException {
598
599 File[] files = new File[2];
600
601 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
602 userGroupId);
603
604 Group group = userGroup.getGroup();
605
606 if (userGroup.hasPrivateLayouts()) {
607 files[0] = layoutLocalService.exportLayoutsAsFile(
608 group.getGroupId(), true, null, parameterMap, null, null);
609 }
610
611 if (userGroup.hasPublicLayouts()) {
612 files[1] = layoutLocalService.exportLayoutsAsFile(
613 group.getGroupId(), false, null, parameterMap, null, null);
614 }
615
616 return files;
617 }
618
619 protected Map<String, String[]> getLayoutTemplatesParameters() {
620 Map<String, String[]> parameterMap =
621 new LinkedHashMap<String, String[]>();
622
623 parameterMap.put(
624 PortletDataHandlerKeys.CATEGORIES,
625 new String[] {Boolean.TRUE.toString()});
626 parameterMap.put(
627 PortletDataHandlerKeys.DATA_STRATEGY,
628 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
629 parameterMap.put(
630 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
631 new String[] {Boolean.FALSE.toString()});
632 parameterMap.put(
633 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
634 new String[] {Boolean.FALSE.toString()});
635 parameterMap.put(
636 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
637 new String[] {Boolean.FALSE.toString()});
638 parameterMap.put(
639 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
640 new String[] {PortletDataHandlerKeys.
641 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
642 parameterMap.put(
643 PortletDataHandlerKeys.LOGO,
644 new String[] {Boolean.FALSE.toString()});
645 parameterMap.put(
646 PortletDataHandlerKeys.PERMISSIONS,
647 new String[] {Boolean.TRUE.toString()});
648 parameterMap.put(
649 PortletDataHandlerKeys.PORTLET_DATA,
650 new String[] {Boolean.TRUE.toString()});
651 parameterMap.put(
652 PortletDataHandlerKeys.PORTLET_DATA_ALL,
653 new String[] {Boolean.TRUE.toString()});
654 parameterMap.put(
655 PortletDataHandlerKeys.PORTLET_SETUP,
656 new String[] {Boolean.TRUE.toString()});
657 parameterMap.put(
658 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
659 new String[] {Boolean.TRUE.toString()});
660 parameterMap.put(
661 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
662 new String[] {PortletDataHandlerKeys.
663 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
664 parameterMap.put(
665 PortletDataHandlerKeys.THEME,
666 new String[] {Boolean.FALSE.toString()});
667 parameterMap.put(
668 PortletDataHandlerKeys.THEME_REFERENCE,
669 new String[] {Boolean.TRUE.toString()});
670 parameterMap.put(
671 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
672 new String[] {Boolean.FALSE.toString()});
673 parameterMap.put(
674 PortletDataHandlerKeys.USER_ID_STRATEGY,
675 new String[] {UserIdStrategy.CURRENT_USER_ID});
676 parameterMap.put(
677 PortletDataHandlerKeys.USER_PERMISSIONS,
678 new String[] {Boolean.FALSE.toString()});
679
680 return parameterMap;
681 }
682
683 protected void importLayouts(
684 long userId, Map<String, String[]> parameterMap,
685 File privateLayoutsFile, File publicLayoutsFile)
686 throws PortalException, SystemException {
687
688 User user = userPersistence.findByPrimaryKey(userId);
689
690 long groupId = user.getGroup().getGroupId();
691
692 if (privateLayoutsFile != null) {
693 layoutLocalService.importLayouts(
694 userId, groupId, true, parameterMap, privateLayoutsFile);
695 }
696
697 if (publicLayoutsFile != null) {
698 layoutLocalService.importLayouts(
699 userId, groupId, false, parameterMap, publicLayoutsFile);
700 }
701 }
702
703 protected void validate(long userGroupId, long companyId, String name)
704 throws PortalException, SystemException {
705
706 if ((Validator.isNull(name)) ||
707 (name.indexOf(CharPool.COMMA) != -1) ||
708 (name.indexOf(CharPool.STAR) != -1)) {
709
710 throw new UserGroupNameException();
711 }
712
713 if (Validator.isNumber(name) &&
714 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
715
716 throw new UserGroupNameException();
717 }
718
719 try {
720 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
721
722 if (userGroup.getUserGroupId() != userGroupId) {
723 throw new DuplicateUserGroupException();
724 }
725 }
726 catch (NoSuchUserGroupException nsuge) {
727 }
728 }
729
730 }