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.Hits;
026 import com.liferay.portal.kernel.search.Indexer;
027 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
028 import com.liferay.portal.kernel.search.QueryConfig;
029 import com.liferay.portal.kernel.search.SearchContext;
030 import com.liferay.portal.kernel.search.Sort;
031 import com.liferay.portal.kernel.util.CharPool;
032 import com.liferay.portal.kernel.util.OrderByComparator;
033 import com.liferay.portal.kernel.util.Validator;
034 import com.liferay.portal.kernel.workflow.WorkflowConstants;
035 import com.liferay.portal.model.Group;
036 import com.liferay.portal.model.GroupConstants;
037 import com.liferay.portal.model.ResourceConstants;
038 import com.liferay.portal.model.Team;
039 import com.liferay.portal.model.User;
040 import com.liferay.portal.model.UserGroup;
041 import com.liferay.portal.model.UserGroupConstants;
042 import com.liferay.portal.security.auth.MembershipPolicyUtil;
043 import com.liferay.portal.security.ldap.LDAPUserGroupTransactionThreadLocal;
044 import com.liferay.portal.security.permission.PermissionCacheUtil;
045 import com.liferay.portal.service.ServiceContext;
046 import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
047 import com.liferay.portal.util.PropsValues;
048
049 import java.io.File;
050 import java.io.Serializable;
051
052 import java.util.ArrayList;
053 import java.util.HashMap;
054 import java.util.LinkedHashMap;
055 import java.util.List;
056 import java.util.Map;
057 import java.util.Set;
058
059
064 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
065
066
073 public void addGroupUserGroups(long groupId, long[] userGroupIds)
074 throws SystemException {
075
076 groupPersistence.addUserGroups(groupId, userGroupIds);
077
078 PermissionCacheUtil.clearCache();
079 }
080
081
088 public void addTeamUserGroups(long teamId, long[] userGroupIds)
089 throws SystemException {
090
091 teamPersistence.addUserGroups(teamId, userGroupIds);
092
093 PermissionCacheUtil.clearCache();
094 }
095
096
116 public UserGroup addUserGroup(
117 long userId, long companyId, String name, String description)
118 throws PortalException, SystemException {
119
120 return addUserGroup(userId, companyId, name, description, null);
121 }
122
123
144 public UserGroup addUserGroup(
145 long userId, long companyId, String name, String description,
146 ServiceContext serviceContext)
147 throws PortalException, SystemException {
148
149
150
151 validate(0, companyId, name);
152
153 long userGroupId = counterLocalService.increment();
154
155 UserGroup userGroup = userGroupPersistence.create(userGroupId);
156
157 userGroup.setCompanyId(companyId);
158 userGroup.setParentUserGroupId(
159 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
160 userGroup.setName(name);
161 userGroup.setDescription(description);
162 userGroup.setAddedByLDAPImport(
163 LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
164 userGroup.setExpandoBridgeAttributes(serviceContext);
165
166 userGroupPersistence.update(userGroup);
167
168
169
170 groupLocalService.addGroup(
171 userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
172 UserGroup.class.getName(), userGroup.getUserGroupId(),
173 GroupConstants.DEFAULT_LIVE_GROUP_ID, String.valueOf(userGroupId),
174 null, 0, null, false, true, null);
175
176
177
178 resourceLocalService.addResources(
179 companyId, 0, userId, UserGroup.class.getName(),
180 userGroup.getUserGroupId(), false, false, false);
181
182
183
184 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
185 UserGroup.class);
186
187 indexer.reindex(userGroup);
188
189 return userGroup;
190 }
191
192 public void checkMembershipPolicy(User user)
193 throws PortalException, SystemException {
194
195 List<UserGroup> userGroups = getUserUserGroups(user.getUserId());
196
197 for (UserGroup userGroup : userGroups) {
198 if (!MembershipPolicyUtil.isMembershipAllowed(userGroup, user)) {
199 userLocalService.unsetUserGroupUsers(
200 userGroup.getUserGroupId(), new long[] {user.getUserId()});
201 }
202 }
203
204 Set<UserGroup> mandatoryUserGroups =
205 MembershipPolicyUtil.getMandatoryUserGroups(user);
206
207 for (UserGroup userGroup : mandatoryUserGroups) {
208 if (!userLocalService.hasUserGroupUser(
209 userGroup.getUserGroupId(), user.getUserId())) {
210
211 userLocalService.addUserGroupUsers(
212 userGroup.getUserGroupId(), new long[] {user.getUserId()});
213 }
214 }
215 }
216
217
228 public void clearUserUserGroups(long userId) throws SystemException {
229 userPersistence.clearUserGroups(userId);
230
231 PermissionCacheUtil.clearCache();
232 }
233
234
245 public void copyUserGroupLayouts(long userGroupId, long userIds[])
246 throws PortalException, SystemException {
247
248 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
249
250 File[] files = exportLayouts(userGroupId, parameterMap);
251
252 try {
253 for (long userId : userIds) {
254 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
255 importLayouts(userId, parameterMap, files[0], files[1]);
256 }
257 }
258 }
259 finally {
260 if (files[0] != null) {
261 files[0].delete();
262 }
263
264 if (files[1] != null) {
265 files[1].delete();
266 }
267 }
268 }
269
270
280 public void copyUserGroupLayouts(long userGroupIds[], long userId)
281 throws PortalException, SystemException {
282
283 for (long userGroupId : userGroupIds) {
284 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
285 copyUserGroupLayouts(userGroupId, userId);
286 }
287 }
288 }
289
290
300 public void copyUserGroupLayouts(long userGroupId, long userId)
301 throws PortalException, SystemException {
302
303 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
304
305 File[] files = exportLayouts(userGroupId, parameterMap);
306
307 try {
308 importLayouts(userId, parameterMap, files[0], files[1]);
309 }
310 finally {
311 if (files[0] != null) {
312 files[0].delete();
313 }
314
315 if (files[1] != null) {
316 files[1].delete();
317 }
318 }
319 }
320
321
330 @Override
331 public UserGroup deleteUserGroup(long userGroupId)
332 throws PortalException, SystemException {
333
334 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
335 userGroupId);
336
337 return deleteUserGroup(userGroup);
338 }
339
340
349 @Override
350 public UserGroup deleteUserGroup(UserGroup userGroup)
351 throws PortalException, SystemException {
352
353 int count = userLocalService.getUserGroupUsersCount(
354 userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
355
356 if (count > 0) {
357 throw new RequiredUserGroupException();
358 }
359
360
361
362 expandoValueLocalService.deleteValues(
363 UserGroup.class.getName(), userGroup.getUserGroupId());
364
365
366
367 clearUserUserGroups(userGroup.getUserGroupId());
368
369
370
371 Group group = userGroup.getGroup();
372
373 groupLocalService.deleteGroup(group);
374
375
376
377 userGroupGroupRoleLocalService.deleteUserGroupGroupRolesByUserGroupId(
378 userGroup.getUserGroupId());
379
380
381
382 resourceLocalService.deleteResource(
383 userGroup.getCompanyId(), UserGroup.class.getName(),
384 ResourceConstants.SCOPE_INDIVIDUAL, userGroup.getUserGroupId());
385
386
387
388 userGroupPersistence.remove(userGroup);
389
390
391
392 PermissionCacheUtil.clearCache();
393
394 return userGroup;
395 }
396
397
406 public UserGroup getUserGroup(long companyId, String name)
407 throws PortalException, SystemException {
408
409 return userGroupPersistence.findByC_N(companyId, name);
410 }
411
412
419 public List<UserGroup> getUserGroups(long companyId)
420 throws SystemException {
421
422 return userGroupPersistence.findByCompanyId(companyId);
423 }
424
425
433 public List<UserGroup> getUserGroups(long[] userGroupIds)
434 throws PortalException, SystemException {
435
436 List<UserGroup> userGroups = new ArrayList<UserGroup>(
437 userGroupIds.length);
438
439 for (long userGroupId : userGroupIds) {
440 UserGroup userGroup = getUserGroup(userGroupId);
441
442 userGroups.add(userGroup);
443 }
444
445 return userGroups;
446 }
447
448
455 public List<UserGroup> getUserUserGroups(long userId)
456 throws SystemException {
457
458 return userPersistence.getUserGroups(userId);
459 }
460
461
470 public boolean hasGroupUserGroup(long groupId, long userGroupId)
471 throws SystemException {
472
473 return groupPersistence.containsUserGroup(groupId, userGroupId);
474 }
475
476
485 public boolean hasTeamUserGroup(long teamId, long userGroupId)
486 throws SystemException {
487
488 return teamPersistence.containsUserGroup(teamId, userGroupId);
489 }
490
491
519 public List<UserGroup> search(
520 long companyId, String keywords,
521 LinkedHashMap<String, Object> params, int start, int end,
522 OrderByComparator obc)
523 throws SystemException {
524
525 return userGroupFinder.findByKeywords(
526 companyId, keywords, params, start, end, obc);
527 }
528
529
559 public Hits search(
560 long companyId, String keywords,
561 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
562 throws SystemException {
563
564 String name = null;
565 String description = null;
566 boolean andOperator = false;
567
568 if (Validator.isNotNull(keywords)) {
569 name = keywords;
570 description = keywords;
571 }
572 else {
573 andOperator = true;
574 }
575
576 if (params != null) {
577 params.put("keywords", keywords);
578 }
579
580 return search(
581 companyId, name, description, params, andOperator, start, end,
582 sort);
583 }
584
585
618 public Hits search(
619 long companyId, String name, String description,
620 LinkedHashMap<String, Object> params, boolean andSearch, int start,
621 int end, Sort sort)
622 throws SystemException {
623
624 try {
625 SearchContext searchContext = new SearchContext();
626
627 searchContext.setAndSearch(andSearch);
628
629 Map<String, Serializable> attributes =
630 new HashMap<String, Serializable>();
631
632 attributes.put("description", description);
633 attributes.put("name", name);
634
635 searchContext.setAttributes(attributes);
636
637 searchContext.setCompanyId(companyId);
638 searchContext.setEnd(end);
639
640 if (params != null) {
641 String keywords = (String)params.remove("keywords");
642
643 if (Validator.isNotNull(keywords)) {
644 searchContext.setKeywords(keywords);
645 }
646 }
647
648 QueryConfig queryConfig = new QueryConfig();
649
650 queryConfig.setHighlightEnabled(false);
651 queryConfig.setScoreEnabled(false);
652
653 searchContext.setQueryConfig(queryConfig);
654
655 if (sort != null) {
656 searchContext.setSorts(new Sort[] {sort});
657 }
658
659 searchContext.setStart(start);
660
661 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
662 UserGroup.class);
663
664 return indexer.search(searchContext);
665 }
666 catch (Exception e) {
667 throw new SystemException(e);
668 }
669 }
670
671
684 public int searchCount(
685 long companyId, String keywords,
686 LinkedHashMap<String, Object> params)
687 throws SystemException {
688
689 return userGroupFinder.countByKeywords(companyId, keywords, params);
690 }
691
692
702 public void setUserUserGroups(long userId, long[] userGroupIds)
703 throws PortalException, SystemException {
704
705 copyUserGroupLayouts(userGroupIds, userId);
706
707 userPersistence.setUserGroups(userId, userGroupIds);
708
709 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
710
711 indexer.reindex(userId);
712
713 PermissionCacheUtil.clearCache();
714 }
715
716
723 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
724 throws SystemException {
725
726 List<Team> teams = teamPersistence.findByGroupId(groupId);
727
728 for (Team team : teams) {
729 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
730 }
731
732 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
733 userGroupIds, groupId);
734
735 groupPersistence.removeUserGroups(groupId, userGroupIds);
736
737 PermissionCacheUtil.clearCache();
738 }
739
740
747 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
748 throws SystemException {
749
750 teamPersistence.removeUserGroups(teamId, userGroupIds);
751
752 PermissionCacheUtil.clearCache();
753 }
754
755
769 public UserGroup updateUserGroup(
770 long companyId, long userGroupId, String name, String description)
771 throws PortalException, SystemException {
772
773 return updateUserGroup(companyId, userGroupId, name, description, null);
774 }
775
776
791 public UserGroup updateUserGroup(
792 long companyId, long userGroupId, String name, String description,
793 ServiceContext serviceContext)
794 throws PortalException, SystemException {
795
796
797
798 validate(userGroupId, companyId, name);
799
800 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
801 userGroupId);
802
803 userGroup.setName(name);
804 userGroup.setDescription(description);
805 userGroup.setExpandoBridgeAttributes(serviceContext);
806
807 userGroupPersistence.update(userGroup);
808
809
810
811 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
812 UserGroup.class);
813
814 indexer.reindex(userGroup);
815
816 return userGroup;
817 }
818
819 protected File[] exportLayouts(
820 long userGroupId, Map<String, String[]> parameterMap)
821 throws PortalException, SystemException {
822
823 File[] files = new File[2];
824
825 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
826 userGroupId);
827
828 Group group = userGroup.getGroup();
829
830 if (userGroup.hasPrivateLayouts()) {
831 files[0] = layoutLocalService.exportLayoutsAsFile(
832 group.getGroupId(), true, null, parameterMap, null, null);
833 }
834
835 if (userGroup.hasPublicLayouts()) {
836 files[1] = layoutLocalService.exportLayoutsAsFile(
837 group.getGroupId(), false, null, parameterMap, null, null);
838 }
839
840 return files;
841 }
842
843 protected Map<String, String[]> getLayoutTemplatesParameters() {
844 Map<String, String[]> parameterMap =
845 new LinkedHashMap<String, String[]>();
846
847 parameterMap.put(
848 PortletDataHandlerKeys.CATEGORIES,
849 new String[] {Boolean.TRUE.toString()});
850 parameterMap.put(
851 PortletDataHandlerKeys.DATA_STRATEGY,
852 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
853 parameterMap.put(
854 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
855 new String[] {Boolean.FALSE.toString()});
856 parameterMap.put(
857 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
858 new String[] {Boolean.FALSE.toString()});
859 parameterMap.put(
860 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
861 new String[] {Boolean.FALSE.toString()});
862 parameterMap.put(
863 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
864 new String[] {PortletDataHandlerKeys.
865 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
866 parameterMap.put(
867 PortletDataHandlerKeys.LOGO,
868 new String[] {Boolean.FALSE.toString()});
869 parameterMap.put(
870 PortletDataHandlerKeys.PERMISSIONS,
871 new String[] {Boolean.TRUE.toString()});
872 parameterMap.put(
873 PortletDataHandlerKeys.PORTLET_DATA,
874 new String[] {Boolean.TRUE.toString()});
875 parameterMap.put(
876 PortletDataHandlerKeys.PORTLET_DATA_ALL,
877 new String[] {Boolean.TRUE.toString()});
878 parameterMap.put(
879 PortletDataHandlerKeys.PORTLET_SETUP,
880 new String[] {Boolean.TRUE.toString()});
881 parameterMap.put(
882 PortletDataHandlerKeys.PORTLET_USER_PREFERENCES,
883 new String[] {Boolean.TRUE.toString()});
884 parameterMap.put(
885 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
886 new String[] {PortletDataHandlerKeys.
887 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
888 parameterMap.put(
889 PortletDataHandlerKeys.THEME,
890 new String[] {Boolean.FALSE.toString()});
891 parameterMap.put(
892 PortletDataHandlerKeys.THEME_REFERENCE,
893 new String[] {Boolean.TRUE.toString()});
894 parameterMap.put(
895 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
896 new String[] {Boolean.FALSE.toString()});
897 parameterMap.put(
898 PortletDataHandlerKeys.USER_ID_STRATEGY,
899 new String[] {UserIdStrategy.CURRENT_USER_ID});
900
901 return parameterMap;
902 }
903
904 protected void importLayouts(
905 long userId, Map<String, String[]> parameterMap,
906 File privateLayoutsFile, File publicLayoutsFile)
907 throws PortalException, SystemException {
908
909 User user = userPersistence.findByPrimaryKey(userId);
910
911 long groupId = user.getGroup().getGroupId();
912
913 if (privateLayoutsFile != null) {
914 layoutLocalService.importLayouts(
915 userId, groupId, true, parameterMap, privateLayoutsFile);
916 }
917
918 if (publicLayoutsFile != null) {
919 layoutLocalService.importLayouts(
920 userId, groupId, false, parameterMap, publicLayoutsFile);
921 }
922 }
923
924 protected void validate(long userGroupId, long companyId, String name)
925 throws PortalException, SystemException {
926
927 if (Validator.isNull(name) ||
928 (name.indexOf(CharPool.COMMA) != -1) ||
929 (name.indexOf(CharPool.STAR) != -1)) {
930
931 throw new UserGroupNameException();
932 }
933
934 if (Validator.isNumber(name) &&
935 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
936
937 throw new UserGroupNameException();
938 }
939
940 try {
941 UserGroup userGroup = userGroupFinder.findByC_N(companyId, name);
942
943 if (userGroup.getUserGroupId() != userGroupId) {
944 throw new DuplicateUserGroupException();
945 }
946 }
947 catch (NoSuchUserGroupException nsuge) {
948 }
949 }
950
951 }