001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.DuplicateUserGroupException;
018 import com.liferay.portal.RequiredUserGroupException;
019 import com.liferay.portal.UserGroupNameException;
020 import com.liferay.portal.kernel.dao.orm.QueryUtil;
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.systemevent.SystemEvent;
032 import com.liferay.portal.kernel.util.CharPool;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.kernel.workflow.WorkflowConstants;
036 import com.liferay.portal.model.Group;
037 import com.liferay.portal.model.GroupConstants;
038 import com.liferay.portal.model.ResourceConstants;
039 import com.liferay.portal.model.SystemEventConstants;
040 import com.liferay.portal.model.Team;
041 import com.liferay.portal.model.User;
042 import com.liferay.portal.model.UserGroup;
043 import com.liferay.portal.model.UserGroupConstants;
044 import com.liferay.portal.security.ldap.LDAPUserGroupTransactionThreadLocal;
045 import com.liferay.portal.security.permission.PermissionCacheUtil;
046 import com.liferay.portal.service.ServiceContext;
047 import com.liferay.portal.service.base.UserGroupLocalServiceBaseImpl;
048 import com.liferay.portal.util.PropsValues;
049
050 import java.io.File;
051 import java.io.Serializable;
052
053 import java.util.ArrayList;
054 import java.util.Date;
055 import java.util.HashMap;
056 import java.util.LinkedHashMap;
057 import java.util.List;
058 import java.util.Map;
059
060
066 public class UserGroupLocalServiceImpl extends UserGroupLocalServiceBaseImpl {
067
068
075 @Override
076 public void addGroupUserGroups(long groupId, long[] userGroupIds)
077 throws SystemException {
078
079 groupPersistence.addUserGroups(groupId, userGroupIds);
080
081 PermissionCacheUtil.clearCache();
082 }
083
084
091 @Override
092 public void addTeamUserGroups(long teamId, long[] userGroupIds)
093 throws SystemException {
094
095 teamPersistence.addUserGroups(teamId, userGroupIds);
096
097 PermissionCacheUtil.clearCache();
098 }
099
100
120 @Override
121 public UserGroup addUserGroup(
122 long userId, long companyId, String name, String description)
123 throws PortalException, SystemException {
124
125 return addUserGroup(userId, companyId, name, description, null);
126 }
127
128
149 @Override
150 public UserGroup addUserGroup(
151 long userId, long companyId, String name, String description,
152 ServiceContext serviceContext)
153 throws PortalException, SystemException {
154
155
156
157 Date now = new Date();
158
159 validate(0, companyId, name);
160
161 User user = userPersistence.findByPrimaryKey(userId);
162
163 long userGroupId = counterLocalService.increment();
164
165 UserGroup userGroup = userGroupPersistence.create(userGroupId);
166
167 if (serviceContext != null) {
168 userGroup.setUuid(serviceContext.getUuid());
169 }
170
171 userGroup.setCompanyId(companyId);
172 userGroup.setUserId(user.getUserId());
173 userGroup.setUserName(user.getFullName());
174
175 if (serviceContext != null) {
176 userGroup.setCreateDate(serviceContext.getCreateDate(now));
177 userGroup.setModifiedDate(serviceContext.getModifiedDate(now));
178 }
179 else {
180 userGroup.setCreateDate(now);
181 userGroup.setModifiedDate(now);
182 }
183
184 userGroup.setParentUserGroupId(
185 UserGroupConstants.DEFAULT_PARENT_USER_GROUP_ID);
186 userGroup.setName(name);
187 userGroup.setDescription(description);
188 userGroup.setAddedByLDAPImport(
189 LDAPUserGroupTransactionThreadLocal.isOriginatesFromLDAP());
190 userGroup.setExpandoBridgeAttributes(serviceContext);
191
192 userGroupPersistence.update(userGroup);
193
194
195
196 groupLocalService.addGroup(
197 userId, GroupConstants.DEFAULT_PARENT_GROUP_ID,
198 UserGroup.class.getName(), userGroup.getUserGroupId(),
199 GroupConstants.DEFAULT_LIVE_GROUP_ID, String.valueOf(userGroupId),
200 null, 0, true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, null,
201 false, true, null);
202
203
204
205 resourceLocalService.addResources(
206 companyId, 0, userId, UserGroup.class.getName(),
207 userGroup.getUserGroupId(), false, false, false);
208
209
210
211 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
212 UserGroup.class);
213
214 indexer.reindex(userGroup);
215
216 return userGroup;
217 }
218
219
226 @Override
227 public void clearUserUserGroups(long userId) throws SystemException {
228 userPersistence.clearUserGroups(userId);
229
230 PermissionCacheUtil.clearCache(userId);
231 }
232
233
243 @Override
244 public void copyUserGroupLayouts(long userGroupId, long userId)
245 throws PortalException, SystemException {
246
247 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
248
249 File[] files = exportLayouts(userGroupId, parameterMap);
250
251 try {
252 importLayouts(userId, parameterMap, files[0], files[1]);
253 }
254 finally {
255 if (files[0] != null) {
256 files[0].delete();
257 }
258
259 if (files[1] != null) {
260 files[1].delete();
261 }
262 }
263 }
264
265
276 @Override
277 public void copyUserGroupLayouts(long userGroupId, long[] userIds)
278 throws PortalException, SystemException {
279
280 Map<String, String[]> parameterMap = getLayoutTemplatesParameters();
281
282 File[] files = exportLayouts(userGroupId, parameterMap);
283
284 try {
285 for (long userId : userIds) {
286 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
287 importLayouts(userId, parameterMap, files[0], files[1]);
288 }
289 }
290 }
291 finally {
292 if (files[0] != null) {
293 files[0].delete();
294 }
295
296 if (files[1] != null) {
297 files[1].delete();
298 }
299 }
300 }
301
302
312 @Override
313 public void copyUserGroupLayouts(long[] userGroupIds, long userId)
314 throws PortalException, SystemException {
315
316 for (long userGroupId : userGroupIds) {
317 if (!userGroupPersistence.containsUser(userGroupId, userId)) {
318 copyUserGroupLayouts(userGroupId, userId);
319 }
320 }
321 }
322
323
332 @Override
333 public UserGroup deleteUserGroup(long userGroupId)
334 throws PortalException, SystemException {
335
336 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
337 userGroupId);
338
339 return userGroupLocalService.deleteUserGroup(userGroup);
340 }
341
342
351 @Override
352 @SystemEvent(
353 action = SystemEventConstants.ACTION_SKIP,
354 type = SystemEventConstants.TYPE_DELETE)
355 public UserGroup deleteUserGroup(UserGroup userGroup)
356 throws PortalException, SystemException {
357
358 int count = userLocalService.getUserGroupUsersCount(
359 userGroup.getUserGroupId(), WorkflowConstants.STATUS_APPROVED);
360
361 if (count > 0) {
362 throw new RequiredUserGroupException();
363 }
364
365
366
367 expandoRowLocalService.deleteRows(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 @Override
398 public void deleteUserGroups(long companyId)
399 throws PortalException, SystemException {
400
401 List<UserGroup> userGroups = userGroupPersistence.findByCompanyId(
402 companyId);
403
404 for (UserGroup userGroup : userGroups) {
405 userGroupLocalService.deleteUserGroup(userGroup);
406 }
407 }
408
409 @Override
410 public UserGroup fetchUserGroup(long companyId, String name)
411 throws SystemException {
412
413 return userGroupPersistence.fetchByC_N(companyId, name);
414 }
415
416
425 @Override
426 public UserGroup getUserGroup(long companyId, String name)
427 throws PortalException, SystemException {
428
429 return userGroupPersistence.findByC_N(companyId, name);
430 }
431
432
439 @Override
440 public List<UserGroup> getUserGroups(long companyId)
441 throws SystemException {
442
443 return userGroupPersistence.findByCompanyId(companyId);
444 }
445
446
454 @Override
455 public List<UserGroup> getUserGroups(long[] userGroupIds)
456 throws PortalException, SystemException {
457
458 List<UserGroup> userGroups = new ArrayList<UserGroup>(
459 userGroupIds.length);
460
461 for (long userGroupId : userGroupIds) {
462 UserGroup userGroup = getUserGroup(userGroupId);
463
464 userGroups.add(userGroup);
465 }
466
467 return userGroups;
468 }
469
470
498 @Override
499 public List<UserGroup> search(
500 long companyId, String keywords,
501 LinkedHashMap<String, Object> params, int start, int end,
502 OrderByComparator obc)
503 throws SystemException {
504
505 return userGroupFinder.findByKeywords(
506 companyId, keywords, params, start, end, obc);
507 }
508
509
539 @Override
540 public Hits search(
541 long companyId, String keywords,
542 LinkedHashMap<String, Object> params, int start, int end, Sort sort)
543 throws SystemException {
544
545 String name = null;
546 String description = null;
547 boolean andOperator = false;
548
549 if (Validator.isNotNull(keywords)) {
550 name = keywords;
551 description = keywords;
552 }
553 else {
554 andOperator = true;
555 }
556
557 if (params != null) {
558 params.put("keywords", keywords);
559 }
560
561 return search(
562 companyId, name, description, params, andOperator, start, end,
563 sort);
564 }
565
566
599 @Override
600 public Hits search(
601 long companyId, String name, String description,
602 LinkedHashMap<String, Object> params, boolean andSearch, int start,
603 int end, Sort sort)
604 throws SystemException {
605
606 try {
607 SearchContext searchContext = buildSearchContext(
608 companyId, name, description, params, andSearch, start, end,
609 sort);
610
611 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
612 UserGroup.class);
613
614 return indexer.search(searchContext);
615 }
616 catch (Exception e) {
617 throw new SystemException(e);
618 }
619 }
620
621
634 @Override
635 public int searchCount(
636 long companyId, String keywords,
637 LinkedHashMap<String, Object> params)
638 throws SystemException {
639
640 if (!PropsValues.USER_GROUPS_INDEXER_ENABLED ||
641 !PropsValues.USER_GROUPS_SEARCH_WITH_INDEX ||
642 isUseCustomSQL(params)) {
643
644 return userGroupFinder.countByKeywords(companyId, keywords, params);
645 }
646
647 String name = null;
648 String description = null;
649 boolean andOperator = false;
650
651 if (Validator.isNotNull(keywords)) {
652 name = keywords;
653 description = keywords;
654 }
655 else {
656 andOperator = true;
657 }
658
659 if (params != null) {
660 params.put("keywords", keywords);
661 }
662
663 try {
664 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
665 UserGroup.class);
666
667 SearchContext searchContext = buildSearchContext(
668 companyId, name, description, params, andOperator,
669 QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
670
671 Hits hits = indexer.search(searchContext);
672
673 return hits.getLength();
674 }
675 catch (Exception e) {
676 throw new SystemException(e);
677 }
678 }
679
680
690 @Override
691 public void setUserUserGroups(long userId, long[] userGroupIds)
692 throws PortalException, SystemException {
693
694 if (PropsValues.USER_GROUPS_COPY_LAYOUTS_TO_USER_PERSONAL_SITE) {
695 copyUserGroupLayouts(userGroupIds, userId);
696 }
697
698 userPersistence.setUserGroups(userId, userGroupIds);
699
700 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);
701
702 indexer.reindex(userId);
703
704 PermissionCacheUtil.clearCache(userId);
705 }
706
707
714 @Override
715 public void unsetGroupUserGroups(long groupId, long[] userGroupIds)
716 throws SystemException {
717
718 List<Team> teams = teamPersistence.findByGroupId(groupId);
719
720 for (Team team : teams) {
721 teamPersistence.removeUserGroups(team.getTeamId(), userGroupIds);
722 }
723
724 userGroupGroupRoleLocalService.deleteUserGroupGroupRoles(
725 userGroupIds, groupId);
726
727 groupPersistence.removeUserGroups(groupId, userGroupIds);
728
729 PermissionCacheUtil.clearCache();
730 }
731
732
739 @Override
740 public void unsetTeamUserGroups(long teamId, long[] userGroupIds)
741 throws SystemException {
742
743 teamPersistence.removeUserGroups(teamId, userGroupIds);
744
745 PermissionCacheUtil.clearCache();
746 }
747
748
762 @Override
763 public UserGroup updateUserGroup(
764 long companyId, long userGroupId, String name, String description)
765 throws PortalException, SystemException {
766
767 return updateUserGroup(companyId, userGroupId, name, description, null);
768 }
769
770
785 @Override
786 public UserGroup updateUserGroup(
787 long companyId, long userGroupId, String name, String description,
788 ServiceContext serviceContext)
789 throws PortalException, SystemException {
790
791
792
793 validate(userGroupId, companyId, name);
794
795 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
796 userGroupId);
797
798 userGroup.setModifiedDate(new Date());
799 userGroup.setName(name);
800 userGroup.setDescription(description);
801 userGroup.setExpandoBridgeAttributes(serviceContext);
802
803 userGroupPersistence.update(userGroup);
804
805
806
807 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
808 UserGroup.class);
809
810 indexer.reindex(userGroup);
811
812 return userGroup;
813 }
814
815 protected SearchContext buildSearchContext(
816 long companyId, String name, String description,
817 LinkedHashMap<String, Object> params, boolean andSearch, int start,
818 int end, Sort sort) {
819
820 SearchContext searchContext = new SearchContext();
821
822 searchContext.setAndSearch(andSearch);
823
824 Map<String, Serializable> attributes =
825 new HashMap<String, Serializable>();
826
827 attributes.put("description", description);
828 attributes.put("name", name);
829
830 searchContext.setAttributes(attributes);
831
832 searchContext.setCompanyId(companyId);
833 searchContext.setEnd(end);
834
835 if (params != null) {
836 String keywords = (String)params.remove("keywords");
837
838 if (Validator.isNotNull(keywords)) {
839 searchContext.setKeywords(keywords);
840 }
841 }
842
843 QueryConfig queryConfig = new QueryConfig();
844
845 queryConfig.setHighlightEnabled(false);
846 queryConfig.setScoreEnabled(false);
847
848 searchContext.setQueryConfig(queryConfig);
849
850 if (sort != null) {
851 searchContext.setSorts(sort);
852 }
853
854 searchContext.setStart(start);
855
856 return searchContext;
857 }
858
859 protected File[] exportLayouts(
860 long userGroupId, Map<String, String[]> parameterMap)
861 throws PortalException, SystemException {
862
863 File[] files = new File[2];
864
865 UserGroup userGroup = userGroupPersistence.findByPrimaryKey(
866 userGroupId);
867
868 Group group = userGroup.getGroup();
869
870 if (userGroup.hasPrivateLayouts()) {
871 files[0] = layoutLocalService.exportLayoutsAsFile(
872 group.getGroupId(), true, null, parameterMap, null, null);
873 }
874
875 if (userGroup.hasPublicLayouts()) {
876 files[1] = layoutLocalService.exportLayoutsAsFile(
877 group.getGroupId(), false, null, parameterMap, null, null);
878 }
879
880 return files;
881 }
882
883 protected Map<String, String[]> getLayoutTemplatesParameters() {
884 Map<String, String[]> parameterMap =
885 new LinkedHashMap<String, String[]>();
886
887 parameterMap.put(
888 PortletDataHandlerKeys.CATEGORIES,
889 new String[] {Boolean.TRUE.toString()});
890 parameterMap.put(
891 PortletDataHandlerKeys.DATA_STRATEGY,
892 new String[] {PortletDataHandlerKeys.DATA_STRATEGY_MIRROR});
893 parameterMap.put(
894 PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
895 new String[] {Boolean.FALSE.toString()});
896 parameterMap.put(
897 PortletDataHandlerKeys.DELETE_PORTLET_DATA,
898 new String[] {Boolean.FALSE.toString()});
899 parameterMap.put(
900 PortletDataHandlerKeys.LAYOUT_SET_SETTINGS,
901 new String[] {Boolean.FALSE.toString()});
902 parameterMap.put(
903 PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
904 new String[] {PortletDataHandlerKeys.
905 LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE});
906 parameterMap.put(
907 PortletDataHandlerKeys.LOGO,
908 new String[] {Boolean.FALSE.toString()});
909 parameterMap.put(
910 PortletDataHandlerKeys.PERMISSIONS,
911 new String[] {Boolean.TRUE.toString()});
912 parameterMap.put(
913 PortletDataHandlerKeys.PORTLET_CONFIGURATION,
914 new String[] {Boolean.TRUE.toString()});
915 parameterMap.put(
916 PortletDataHandlerKeys.PORTLET_CONFIGURATION_ALL,
917 new String[] {Boolean.TRUE.toString()});
918 parameterMap.put(
919 PortletDataHandlerKeys.PORTLET_DATA,
920 new String[] {Boolean.TRUE.toString()});
921 parameterMap.put(
922 PortletDataHandlerKeys.PORTLET_DATA_ALL,
923 new String[] {Boolean.TRUE.toString()});
924 parameterMap.put(
925 PortletDataHandlerKeys.PORTLET_SETUP_ALL,
926 new String[] {Boolean.TRUE.toString()});
927 parameterMap.put(
928 PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
929 new String[] {PortletDataHandlerKeys.
930 PORTLETS_MERGE_MODE_ADD_TO_BOTTOM});
931 parameterMap.put(
932 PortletDataHandlerKeys.THEME_REFERENCE,
933 new String[] {Boolean.TRUE.toString()});
934 parameterMap.put(
935 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
936 new String[] {Boolean.FALSE.toString()});
937 parameterMap.put(
938 PortletDataHandlerKeys.USER_ID_STRATEGY,
939 new String[] {UserIdStrategy.CURRENT_USER_ID});
940
941 return parameterMap;
942 }
943
944 protected void importLayouts(
945 long userId, Map<String, String[]> parameterMap,
946 File privateLayoutsFile, File publicLayoutsFile)
947 throws PortalException, SystemException {
948
949 User user = userPersistence.findByPrimaryKey(userId);
950
951 long groupId = user.getGroupId();
952
953 if (privateLayoutsFile != null) {
954 layoutLocalService.importLayouts(
955 userId, groupId, true, parameterMap, privateLayoutsFile);
956 }
957
958 if (publicLayoutsFile != null) {
959 layoutLocalService.importLayouts(
960 userId, groupId, false, parameterMap, publicLayoutsFile);
961 }
962 }
963
964 protected boolean isUseCustomSQL(LinkedHashMap<String, Object> params) {
965 if ((params == null) || params.isEmpty()) {
966 return false;
967 }
968
969 return true;
970 }
971
972 protected void validate(long userGroupId, long companyId, String name)
973 throws PortalException, SystemException {
974
975 if (Validator.isNull(name) ||
976 (name.indexOf(CharPool.COMMA) != -1) ||
977 (name.indexOf(CharPool.STAR) != -1)) {
978
979 throw new UserGroupNameException();
980 }
981
982 if (Validator.isNumber(name) &&
983 !PropsValues.USER_GROUPS_NAME_ALLOW_NUMERIC) {
984
985 throw new UserGroupNameException();
986 }
987
988 UserGroup userGroup = fetchUserGroup(companyId, name);
989
990 if ((userGroup != null) &&
991 (userGroup.getUserGroupId() != userGroupId)) {
992
993 throw new DuplicateUserGroupException("{name=" + name + "}");
994 }
995 }
996
997 }