001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.asset.kernel.model.AssetCategory;
018 import com.liferay.asset.kernel.model.AssetTag;
019 import com.liferay.expando.kernel.model.ExpandoBridge;
020 import com.liferay.exportimport.kernel.staging.StagingUtil;
021 import com.liferay.portal.kernel.dao.orm.QueryUtil;
022 import com.liferay.portal.kernel.exception.NoSuchGroupException;
023 import com.liferay.portal.kernel.exception.PortalException;
024 import com.liferay.portal.kernel.model.Company;
025 import com.liferay.portal.kernel.model.Group;
026 import com.liferay.portal.kernel.model.GroupConstants;
027 import com.liferay.portal.kernel.model.Layout;
028 import com.liferay.portal.kernel.model.Organization;
029 import com.liferay.portal.kernel.model.Portlet;
030 import com.liferay.portal.kernel.model.User;
031 import com.liferay.portal.kernel.model.UserGroup;
032 import com.liferay.portal.kernel.portlet.PortletProvider;
033 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
034 import com.liferay.portal.kernel.security.auth.PrincipalException;
035 import com.liferay.portal.kernel.security.membershippolicy.SiteMembershipPolicyUtil;
036 import com.liferay.portal.kernel.security.permission.ActionKeys;
037 import com.liferay.portal.kernel.security.permission.PermissionChecker;
038 import com.liferay.portal.kernel.security.permission.UserBag;
039 import com.liferay.portal.kernel.security.permission.UserBagFactoryUtil;
040 import com.liferay.portal.kernel.service.ServiceContext;
041 import com.liferay.portal.kernel.service.permission.GroupPermissionUtil;
042 import com.liferay.portal.kernel.service.permission.PortalPermissionUtil;
043 import com.liferay.portal.kernel.service.permission.PortletPermissionUtil;
044 import com.liferay.portal.kernel.service.permission.RolePermissionUtil;
045 import com.liferay.portal.kernel.service.permission.UserPermissionUtil;
046 import com.liferay.portal.kernel.util.ArrayUtil;
047 import com.liferay.portal.kernel.util.ListUtil;
048 import com.liferay.portal.kernel.util.LocaleUtil;
049 import com.liferay.portal.kernel.util.MapUtil;
050 import com.liferay.portal.kernel.util.OrderByComparator;
051 import com.liferay.portal.kernel.util.PortalUtil;
052 import com.liferay.portal.kernel.util.UnicodeProperties;
053 import com.liferay.portal.service.base.GroupServiceBaseImpl;
054 import com.liferay.portal.util.PropsValues;
055 import com.liferay.ratings.kernel.transformer.RatingsDataTransformerUtil;
056
057 import java.io.Serializable;
058
059 import java.util.ArrayList;
060 import java.util.Collection;
061 import java.util.Collections;
062 import java.util.HashMap;
063 import java.util.Iterator;
064 import java.util.LinkedHashMap;
065 import java.util.LinkedHashSet;
066 import java.util.List;
067 import java.util.Locale;
068 import java.util.Map;
069 import java.util.Set;
070
071
080 public class GroupServiceImpl extends GroupServiceBaseImpl {
081
082 @Override
083 public Group addGroup(
084 long parentGroupId, long liveGroupId, Map<Locale, String> nameMap,
085 Map<Locale, String> descriptionMap, int type,
086 boolean manualMembership, int membershipRestriction,
087 String friendlyURL, boolean site, boolean inheritContent,
088 boolean active, ServiceContext serviceContext)
089 throws PortalException {
090
091 if (parentGroupId == GroupConstants.DEFAULT_PARENT_GROUP_ID) {
092 PortalPermissionUtil.check(
093 getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
094 }
095 else {
096 GroupPermissionUtil.check(
097 getPermissionChecker(), parentGroupId,
098 ActionKeys.ADD_COMMUNITY);
099 }
100
101 Group group = groupLocalService.addGroup(
102 getUserId(), parentGroupId, null, 0, liveGroupId, nameMap,
103 descriptionMap, type, manualMembership, membershipRestriction,
104 friendlyURL, site, inheritContent, active, serviceContext);
105
106 if (site) {
107 SiteMembershipPolicyUtil.verifyPolicy(group);
108 }
109
110 return group;
111 }
112
113 @Override
114 public Group addGroup(
115 long parentGroupId, long liveGroupId, Map<Locale, String> nameMap,
116 Map<Locale, String> descriptionMap, int type,
117 boolean manualMembership, int membershipRestriction,
118 String friendlyURL, boolean site, boolean active,
119 ServiceContext serviceContext)
120 throws PortalException {
121
122 return addGroup(
123 parentGroupId, liveGroupId, nameMap, descriptionMap, type,
124 manualMembership, membershipRestriction, friendlyURL, site, false,
125 active, serviceContext);
126 }
127
128
156 @Deprecated
157 @Override
158 public Group addGroup(
159 long parentGroupId, long liveGroupId, String name,
160 String description, int type, boolean manualMembership,
161 int membershipRestriction, String friendlyURL, boolean site,
162 boolean active, ServiceContext serviceContext)
163 throws PortalException {
164
165 return addGroup(
166 parentGroupId, liveGroupId, getLocalizationMap(name),
167 getLocalizationMap(description), type, manualMembership,
168 membershipRestriction, friendlyURL, site, false, active,
169 serviceContext);
170 }
171
172
179 @Override
180 public void addRoleGroups(long roleId, long[] groupIds)
181 throws PortalException {
182
183 RolePermissionUtil.check(
184 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
185
186 groupLocalService.addRoleGroups(roleId, groupIds);
187 }
188
189
196 @Override
197 public void checkRemoteStagingGroup(long groupId) throws PortalException {
198 Group group = getGroup(groupId);
199
200 PermissionChecker permissionChecker = getPermissionChecker();
201
202 if (group.getCompanyId() != permissionChecker.getCompanyId()) {
203 throw new NoSuchGroupException(
204 "Group " + groupId + " does not belong in company " +
205 permissionChecker.getCompanyId());
206 }
207 }
208
209
222 @Override
223 public void deleteGroup(long groupId) throws PortalException {
224 GroupPermissionUtil.check(
225 getPermissionChecker(), groupId, ActionKeys.DELETE);
226
227 groupLocalService.deleteGroup(groupId);
228 }
229
230 @Override
231 public void disableStaging(long groupId) throws PortalException {
232 Group group = groupLocalService.getGroup(groupId);
233
234 GroupPermissionUtil.check(
235 getPermissionChecker(), group, ActionKeys.UPDATE);
236
237 groupLocalService.disableStaging(groupId);
238 }
239
240 @Override
241 public void enableStaging(long groupId) throws PortalException {
242 Group group = groupLocalService.getGroup(groupId);
243
244 GroupPermissionUtil.check(
245 getPermissionChecker(), group, ActionKeys.UPDATE);
246
247 groupLocalService.enableStaging(groupId);
248 }
249
250
257 @Override
258 public Group getCompanyGroup(long companyId) throws PortalException {
259 Group group = groupLocalService.getCompanyGroup(companyId);
260
261 GroupPermissionUtil.check(
262 getPermissionChecker(), group, ActionKeys.VIEW);
263
264 return group;
265 }
266
267
274 @Override
275 public Group getGroup(long groupId) throws PortalException {
276 Group group = groupLocalService.getGroup(groupId);
277
278 GroupPermissionUtil.check(
279 getPermissionChecker(), group, ActionKeys.VIEW);
280
281 return group;
282 }
283
284
292 @Override
293 public Group getGroup(long companyId, String groupKey)
294 throws PortalException {
295
296 Group group = groupLocalService.getGroup(companyId, groupKey);
297
298 GroupPermissionUtil.check(
299 getPermissionChecker(), group, ActionKeys.VIEW);
300
301 return group;
302 }
303
304
315 @Override
316 public String getGroupDisplayURL(
317 long groupId, boolean privateLayout, boolean secureConnection)
318 throws PortalException {
319
320 Group group = groupLocalService.getGroup(groupId);
321
322 GroupPermissionUtil.check(
323 getPermissionChecker(), group, ActionKeys.VIEW);
324
325 if (!privateLayout && (group.getPublicLayoutsPageCount() > 0)) {
326 return PortalUtil.getLayoutSetDisplayURL(
327 group.getPublicLayoutSet(), secureConnection);
328 }
329 else if (privateLayout && (group.getPrivateLayoutsPageCount() > 0)) {
330 return PortalUtil.getLayoutSetDisplayURL(
331 group.getPrivateLayoutSet(), secureConnection);
332 }
333
334 GroupPermissionUtil.check(
335 getPermissionChecker(), group, ActionKeys.UPDATE);
336
337 String portletId = PortletProviderUtil.getPortletId(
338 Layout.class.getName(), PortletProvider.Action.EDIT);
339
340 return PortalUtil.getControlPanelFullURL(groupId, portletId, null);
341 }
342
343
353 @Override
354 public List<Group> getGroups(
355 long companyId, long parentGroupId, boolean site)
356 throws PortalException {
357
358 return filterGroups(
359 groupLocalService.getGroups(companyId, parentGroupId, site));
360 }
361
362
373 @Override
374 public List<Group> getManageableSiteGroups(
375 Collection<Portlet> portlets, int max)
376 throws PortalException {
377
378 PermissionChecker permissionChecker = getPermissionChecker();
379
380 if (permissionChecker.isCompanyAdmin()) {
381 LinkedHashMap<String, Object> params = new LinkedHashMap<>();
382
383 params.put("site", Boolean.TRUE);
384
385 return ListUtil.unique(
386 groupLocalService.search(
387 permissionChecker.getCompanyId(), null, null, null, params,
388 true, 0, max));
389 }
390
391 Set<Group> groups = new LinkedHashSet<>();
392
393 List<Group> userSitesGroups = getUserSitesGroups(null, max);
394
395 Iterator<Group> itr = userSitesGroups.iterator();
396
397 while (itr.hasNext()) {
398 Group group = itr.next();
399
400 if (group.isSite() &&
401 PortletPermissionUtil.hasControlPanelAccessPermission(
402 permissionChecker, group.getGroupId(), portlets)) {
403
404 groups.add(group);
405 }
406 }
407
408 return new ArrayList<>(groups);
409 }
410
411
418 @Override
419 public List<Group> getOrganizationsGroups(List<Organization> organizations)
420 throws PortalException {
421
422 List<Group> groups = groupLocalService.getOrganizationsGroups(
423 organizations);
424
425 return filterGroups(groups);
426 }
427
428
436 @Override
437 public Group getUserGroup(long companyId, long userId)
438 throws PortalException {
439
440 Group group = groupLocalService.getUserGroup(companyId, userId);
441
442 GroupPermissionUtil.check(
443 getPermissionChecker(), group, ActionKeys.VIEW);
444
445 return group;
446 }
447
448
455 @Override
456 public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
457 throws PortalException {
458
459 List<Group> groups = groupLocalService.getUserGroupsGroups(userGroups);
460
461 return filterGroups(groups);
462 }
463
464
486 @Override
487 public List<Group> getUserOrganizationsGroups(
488 long userId, int start, int end)
489 throws PortalException {
490
491 List<Group> groups = groupLocalService.getUserOrganizationsGroups(
492 userId, start, end);
493
494 return filterGroups(groups);
495 }
496
497 @Override
498 public List<Group> getUserSitesGroups() throws PortalException {
499 return getUserSitesGroups(null, QueryUtil.ALL_POS);
500 }
501
502
535 @Override
536 public List<Group> getUserSitesGroups(
537 long userId, String[] classNames, int max)
538 throws PortalException {
539
540 User user = userPersistence.findByPrimaryKey(userId);
541
542 boolean checkPermissions = true;
543
544 if (userId == getUserId()) {
545 checkPermissions = false;
546 }
547
548 if (checkPermissions) {
549 UserPermissionUtil.check(
550 getPermissionChecker(), userId, ActionKeys.VIEW);
551 }
552
553 if (user.isDefaultUser()) {
554 return Collections.emptyList();
555 }
556
557 Set<Group> userSiteGroups = new LinkedHashSet<>();
558
559 if (classNames == null) {
560 classNames = new String[] {
561 Company.class.getName(), Group.class.getName(),
562 Organization.class.getName(), User.class.getName()
563 };
564 }
565
566 if (ArrayUtil.contains(classNames, User.class.getName())) {
567 if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
568 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
569
570 userSiteGroups.add(user.getGroup());
571
572 if (userSiteGroups.size() == max) {
573 if (checkPermissions) {
574 return filterGroups(new ArrayList<>(userSiteGroups));
575 }
576
577 return new ArrayList<>(userSiteGroups);
578 }
579 }
580 }
581
582 if (ArrayUtil.contains(classNames, Company.class.getName())) {
583 Group companyGroup = groupLocalService.getCompanyGroup(
584 user.getCompanyId());
585
586 if (GroupPermissionUtil.contains(
587 getPermissionChecker(), companyGroup,
588 ActionKeys.VIEW_SITE_ADMINISTRATION)) {
589
590 userSiteGroups.add(companyGroup);
591
592 if (userSiteGroups.size() == max) {
593 return new ArrayList<>(userSiteGroups);
594 }
595 }
596 }
597
598 if (ArrayUtil.contains(classNames, Group.class.getName()) ||
599 ArrayUtil.contains(classNames, Organization.class.getName())) {
600
601 UserBag userBag = UserBagFactoryUtil.create(userId);
602
603 if (ArrayUtil.contains(classNames, Group.class.getName())) {
604 for (Group group : userBag.getUserGroups()) {
605 if (group.isActive() && group.isSite()) {
606 if (userSiteGroups.add(group) &&
607 (userSiteGroups.size() == max)) {
608
609 if (checkPermissions) {
610 return filterGroups(
611 new ArrayList<>(userSiteGroups));
612 }
613
614 return new ArrayList<>(userSiteGroups);
615 }
616 }
617 }
618 }
619
620 if (ArrayUtil.contains(classNames, Organization.class.getName())) {
621 for (Group group : userBag.getUserOrgGroups()) {
622 if (group.isActive() && group.isSite()) {
623 if (userSiteGroups.add(group) &&
624 (userSiteGroups.size() == max)) {
625
626 if (checkPermissions) {
627 return filterGroups(
628 new ArrayList<>(userSiteGroups));
629 }
630
631 return new ArrayList<>(userSiteGroups);
632 }
633 }
634 }
635 }
636 }
637
638 if (checkPermissions) {
639 return filterGroups(new ArrayList<>(userSiteGroups));
640 }
641
642 return new ArrayList<>(userSiteGroups);
643 }
644
645
677 @Override
678 public List<Group> getUserSitesGroups(String[] classNames, int max)
679 throws PortalException {
680
681 return getUserSitesGroups(getGuestOrUserId(), classNames, max);
682 }
683
684
693 @Override
694 public int getUserSitesGroupsCount() throws PortalException {
695 List<Group> userSitesGroups = getUserSitesGroups(
696 getGuestOrUserId(), null, QueryUtil.ALL_POS);
697
698 return userSitesGroups.size();
699 }
700
701
712 @Override
713 public boolean hasUserGroup(long userId, long groupId)
714 throws PortalException {
715
716 try {
717 UserPermissionUtil.check(
718 getPermissionChecker(), userId, ActionKeys.VIEW);
719 }
720 catch (PrincipalException pe) {
721 GroupPermissionUtil.check(
722 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
723 }
724
725 return groupLocalService.hasUserGroup(userId, groupId);
726 }
727
728 @Override
729 public List<Group> search(
730 long companyId, long[] classNameIds, String keywords,
731 LinkedHashMap<String, Object> params, int start, int end,
732 OrderByComparator<Group> obc)
733 throws PortalException {
734
735 List<Group> groups = groupLocalService.search(
736 companyId, classNameIds, keywords, params, start, end, obc);
737
738 return filterGroups(groups);
739 }
740
741 @Override
742 public List<Group> search(
743 long companyId, long[] classNameIds, String name,
744 String description, LinkedHashMap<String, Object> params,
745 boolean andOperator, int start, int end,
746 OrderByComparator<Group> obc)
747 throws PortalException {
748
749 List<Group> groups = groupLocalService.search(
750 companyId, classNameIds, name, description, params, andOperator,
751 start, end, obc);
752
753 return filterGroups(groups);
754 }
755
756
787 @Override
788 public List<Group> search(
789 long companyId, String name, String description, String[] params,
790 int start, int end)
791 throws PortalException {
792
793 if (params == null) {
794 params = new String[0];
795 }
796
797 LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
798 params);
799
800 List<Group> groups = groupLocalService.search(
801 companyId, name, description, paramsObj, true, start, end);
802
803 return filterGroups(groups);
804 }
805
806
823 @Override
824 public int searchCount(
825 long companyId, String name, String description, String[] params) {
826
827 if (params == null) {
828 params = new String[0];
829 }
830
831 LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
832 params);
833
834 return groupLocalService.searchCount(
835 companyId, name, description, paramsObj, true);
836 }
837
838
846 @Override
847 public void setRoleGroups(long roleId, long[] groupIds)
848 throws PortalException {
849
850 RolePermissionUtil.check(
851 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
852
853 groupLocalService.setRoleGroups(roleId, groupIds);
854 }
855
856
863 @Override
864 public void unsetRoleGroups(long roleId, long[] groupIds)
865 throws PortalException {
866
867 RolePermissionUtil.check(
868 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
869
870 groupLocalService.unsetRoleGroups(roleId, groupIds);
871 }
872
873
882 @Override
883 public Group updateFriendlyURL(long groupId, String friendlyURL)
884 throws PortalException {
885
886 GroupPermissionUtil.check(
887 getPermissionChecker(), groupId, ActionKeys.UPDATE);
888
889 return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
890 }
891
892 @Override
893 public Group updateGroup(
894 long groupId, long parentGroupId, Map<Locale, String> nameMap,
895 Map<Locale, String> descriptionMap, int type,
896 boolean manualMembership, int membershipRestriction,
897 String friendlyURL, boolean inheritContent, boolean active,
898 ServiceContext serviceContext)
899 throws PortalException {
900
901 Group group = groupPersistence.findByPrimaryKey(groupId);
902
903 GroupPermissionUtil.check(
904 getPermissionChecker(), group, ActionKeys.UPDATE);
905
906 if (group.getParentGroupId() != parentGroupId) {
907 if (parentGroupId == GroupConstants.DEFAULT_PARENT_GROUP_ID) {
908 PortalPermissionUtil.check(
909 getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
910 }
911 else {
912 GroupPermissionUtil.check(
913 getPermissionChecker(), parentGroupId,
914 ActionKeys.ADD_COMMUNITY);
915 }
916 }
917
918 if (group.isSite()) {
919 Group oldGroup = group;
920
921 List<AssetCategory> oldAssetCategories =
922 assetCategoryLocalService.getCategories(
923 Group.class.getName(), groupId);
924
925 List<AssetTag> oldAssetTags = assetTagLocalService.getTags(
926 Group.class.getName(), groupId);
927
928 ExpandoBridge oldExpandoBridge = oldGroup.getExpandoBridge();
929
930 Map<String, Serializable> oldExpandoAttributes =
931 oldExpandoBridge.getAttributes();
932
933 group = groupLocalService.updateGroup(
934 groupId, parentGroupId, nameMap, descriptionMap, type,
935 manualMembership, membershipRestriction, friendlyURL,
936 inheritContent, active, serviceContext);
937
938 SiteMembershipPolicyUtil.verifyPolicy(
939 group, oldGroup, oldAssetCategories, oldAssetTags,
940 oldExpandoAttributes, null);
941
942 return group;
943 }
944 else {
945 return groupLocalService.updateGroup(
946 groupId, parentGroupId, nameMap, descriptionMap, type,
947 manualMembership, membershipRestriction, friendlyURL,
948 inheritContent, active, serviceContext);
949 }
950 }
951
952
980 @Deprecated
981 @Override
982 public Group updateGroup(
983 long groupId, long parentGroupId, String name, String description,
984 int type, boolean manualMembership, int membershipRestriction,
985 String friendlyURL, boolean inheritContent, boolean active,
986 ServiceContext serviceContext)
987 throws PortalException {
988
989 return updateGroup(
990 groupId, parentGroupId, getLocalizationMap(name),
991 getLocalizationMap(description), type, manualMembership,
992 membershipRestriction, friendlyURL, inheritContent, active,
993 serviceContext);
994 }
995
996
1005 @Override
1006 public Group updateGroup(long groupId, String typeSettings)
1007 throws PortalException {
1008
1009 Group group = groupPersistence.findByPrimaryKey(groupId);
1010
1011 GroupPermissionUtil.check(
1012 getPermissionChecker(), group, ActionKeys.UPDATE);
1013
1014 if (group.isSite()) {
1015 Group oldGroup = group;
1016
1017 UnicodeProperties oldTypeSettingsProperties =
1018 oldGroup.getTypeSettingsProperties();
1019
1020 group = groupLocalService.updateGroup(groupId, typeSettings);
1021
1022 RatingsDataTransformerUtil.transformGroupRatingsData(
1023 groupId, oldTypeSettingsProperties,
1024 group.getTypeSettingsProperties());
1025
1026 SiteMembershipPolicyUtil.verifyPolicy(
1027 group, oldGroup, null, null, null, oldTypeSettingsProperties);
1028
1029 return group;
1030 }
1031 else {
1032 return groupLocalService.updateGroup(groupId, typeSettings);
1033 }
1034 }
1035
1036 @Override
1037 public void updateStagedPortlets(
1038 long groupId, Map<String, String> stagedPortletIds)
1039 throws PortalException {
1040
1041 Group group = groupPersistence.findByPrimaryKey(groupId);
1042
1043 GroupPermissionUtil.check(
1044 getPermissionChecker(), group, ActionKeys.UPDATE);
1045
1046 UnicodeProperties typeSettingsProperties =
1047 group.getTypeSettingsProperties();
1048
1049 for (Map.Entry<String, String> entry : stagedPortletIds.entrySet()) {
1050 typeSettingsProperties.setProperty(
1051 StagingUtil.getStagedPortletId(entry.getKey()),
1052 entry.getValue());
1053 }
1054
1055 groupLocalService.updateGroup(group);
1056 }
1057
1058 protected List<Group> filterGroups(List<Group> groups)
1059 throws PortalException {
1060
1061 List<Group> filteredGroups = new ArrayList<>();
1062
1063 for (Group group : groups) {
1064 if (GroupPermissionUtil.contains(
1065 getPermissionChecker(), group, ActionKeys.VIEW)) {
1066
1067 filteredGroups.add(group);
1068 }
1069 }
1070
1071 return filteredGroups;
1072 }
1073
1074 protected Map<Locale, String> getLocalizationMap(String value) {
1075 Map<Locale, String> map = new HashMap<>();
1076
1077 map.put(LocaleUtil.getDefault(), value);
1078
1079 return map;
1080 }
1081
1082 }