001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.util.ArrayUtil;
021 import com.liferay.portal.kernel.util.ListUtil;
022 import com.liferay.portal.kernel.util.LocaleUtil;
023 import com.liferay.portal.kernel.util.MapUtil;
024 import com.liferay.portal.kernel.util.OrderByComparator;
025 import com.liferay.portal.kernel.util.UnicodeProperties;
026 import com.liferay.portal.model.Company;
027 import com.liferay.portal.model.Group;
028 import com.liferay.portal.model.GroupConstants;
029 import com.liferay.portal.model.Organization;
030 import com.liferay.portal.model.Portlet;
031 import com.liferay.portal.model.User;
032 import com.liferay.portal.model.UserGroup;
033 import com.liferay.portal.security.auth.PrincipalException;
034 import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
035 import com.liferay.portal.security.permission.ActionKeys;
036 import com.liferay.portal.security.permission.PermissionChecker;
037 import com.liferay.portal.security.permission.UserBag;
038 import com.liferay.portal.security.permission.UserBagFactoryUtil;
039 import com.liferay.portal.service.ServiceContext;
040 import com.liferay.portal.service.base.GroupServiceBaseImpl;
041 import com.liferay.portal.service.permission.GroupPermissionUtil;
042 import com.liferay.portal.service.permission.PortalPermissionUtil;
043 import com.liferay.portal.service.permission.PortletPermissionUtil;
044 import com.liferay.portal.service.permission.RolePermissionUtil;
045 import com.liferay.portal.service.permission.UserPermissionUtil;
046 import com.liferay.portal.util.PropsValues;
047 import com.liferay.portlet.asset.model.AssetCategory;
048 import com.liferay.portlet.asset.model.AssetTag;
049 import com.liferay.portlet.expando.model.ExpandoBridge;
050 import com.liferay.portlet.exportimport.staging.StagingUtil;
051 import com.liferay.portlet.ratings.transformer.RatingsDataTransformerUtil;
052
053 import java.io.Serializable;
054
055 import java.util.ArrayList;
056 import java.util.Collection;
057 import java.util.Collections;
058 import java.util.HashMap;
059 import java.util.Iterator;
060 import java.util.LinkedHashMap;
061 import java.util.LinkedHashSet;
062 import java.util.List;
063 import java.util.Locale;
064 import java.util.Map;
065 import java.util.Set;
066
067
076 public class GroupServiceImpl extends GroupServiceBaseImpl {
077
078 @Override
079 public Group addGroup(
080 long parentGroupId, long liveGroupId, Map<Locale, String> nameMap,
081 Map<Locale, String> descriptionMap, int type,
082 boolean manualMembership, int membershipRestriction,
083 String friendlyURL, boolean site, boolean inheritContent,
084 boolean active, ServiceContext serviceContext)
085 throws PortalException {
086
087 if (parentGroupId == GroupConstants.DEFAULT_PARENT_GROUP_ID) {
088 PortalPermissionUtil.check(
089 getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
090 }
091 else {
092 GroupPermissionUtil.check(
093 getPermissionChecker(), parentGroupId,
094 ActionKeys.ADD_COMMUNITY);
095 }
096
097 Group group = groupLocalService.addGroup(
098 getUserId(), parentGroupId, null, 0, liveGroupId, nameMap,
099 descriptionMap, type, manualMembership, membershipRestriction,
100 friendlyURL, site, inheritContent, active, serviceContext);
101
102 if (site) {
103 SiteMembershipPolicyUtil.verifyPolicy(group);
104 }
105
106 return group;
107 }
108
109 @Override
110 public Group addGroup(
111 long parentGroupId, long liveGroupId, Map<Locale, String> nameMap,
112 Map<Locale, String> descriptionMap, int type,
113 boolean manualMembership, int membershipRestriction,
114 String friendlyURL, boolean site, boolean active,
115 ServiceContext serviceContext)
116 throws PortalException {
117
118 return addGroup(
119 parentGroupId, liveGroupId, nameMap, descriptionMap, type,
120 manualMembership, membershipRestriction, friendlyURL, site, false,
121 active, serviceContext);
122 }
123
124
151 @Deprecated
152 @Override
153 public Group addGroup(
154 long parentGroupId, long liveGroupId, String name,
155 String description, int type, boolean manualMembership,
156 int membershipRestriction, String friendlyURL, boolean site,
157 boolean active, ServiceContext serviceContext)
158 throws PortalException {
159
160 return addGroup(
161 parentGroupId, liveGroupId, getLocalizationMap(name),
162 getLocalizationMap(description), type, manualMembership,
163 membershipRestriction, friendlyURL, site, false, active,
164 serviceContext);
165 }
166
167
188 @Deprecated
189 @Override
190 public Group addGroup(
191 long parentGroupId, String name, String description, int type,
192 String friendlyURL, boolean site, boolean active,
193 ServiceContext serviceContext)
194 throws PortalException {
195
196 return addGroup(
197 parentGroupId, GroupConstants.DEFAULT_LIVE_GROUP_ID,
198 getLocalizationMap(name), getLocalizationMap(description), type,
199 true, GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION, friendlyURL,
200 site, active, serviceContext);
201 }
202
203
207 @Deprecated
208 @Override
209 public Group addGroup(
210 String name, String description, int type, String friendlyURL,
211 boolean site, boolean active, ServiceContext serviceContext)
212 throws PortalException {
213
214 return addGroup(
215 GroupConstants.DEFAULT_PARENT_GROUP_ID, name, description, type,
216 friendlyURL, site, active, serviceContext);
217 }
218
219
225 @Override
226 public void addRoleGroups(long roleId, long[] groupIds)
227 throws PortalException {
228
229 RolePermissionUtil.check(
230 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
231
232 groupLocalService.addRoleGroups(roleId, groupIds);
233 }
234
235
241 @Override
242 public void checkRemoteStagingGroup(long groupId) throws PortalException {
243 Group group = getGroup(groupId);
244
245 PermissionChecker permissionChecker = getPermissionChecker();
246
247 if (group.getCompanyId() != permissionChecker.getCompanyId()) {
248 throw new NoSuchGroupException(
249 "Group " + groupId + " does not belong in company " +
250 permissionChecker.getCompanyId());
251 }
252 }
253
254
266 @Override
267 public void deleteGroup(long groupId) throws PortalException {
268 GroupPermissionUtil.check(
269 getPermissionChecker(), groupId, ActionKeys.DELETE);
270
271 groupLocalService.deleteGroup(groupId);
272 }
273
274 @Override
275 public void disableStaging(long groupId) throws PortalException {
276 Group group = groupLocalService.getGroup(groupId);
277
278 GroupPermissionUtil.check(
279 getPermissionChecker(), group, ActionKeys.UPDATE);
280
281 groupLocalService.disableStaging(groupId);
282 }
283
284 @Override
285 public void enableStaging(long groupId) throws PortalException {
286 Group group = groupLocalService.getGroup(groupId);
287
288 GroupPermissionUtil.check(
289 getPermissionChecker(), group, ActionKeys.UPDATE);
290
291 groupLocalService.enableStaging(groupId);
292 }
293
294
300 @Override
301 public Group getCompanyGroup(long companyId) throws PortalException {
302 Group group = groupLocalService.getCompanyGroup(companyId);
303
304 GroupPermissionUtil.check(
305 getPermissionChecker(), group, ActionKeys.VIEW);
306
307 return group;
308 }
309
310
316 @Override
317 public Group getGroup(long groupId) throws PortalException {
318 Group group = groupLocalService.getGroup(groupId);
319
320 GroupPermissionUtil.check(
321 getPermissionChecker(), group, ActionKeys.VIEW);
322
323 return group;
324 }
325
326
333 @Override
334 public Group getGroup(long companyId, String groupKey)
335 throws PortalException {
336
337 Group group = groupLocalService.getGroup(companyId, groupKey);
338
339 GroupPermissionUtil.check(
340 getPermissionChecker(), group, ActionKeys.VIEW);
341
342 return group;
343 }
344
345
354 @Override
355 public List<Group> getGroups(
356 long companyId, long parentGroupId, boolean site)
357 throws PortalException {
358
359 return filterGroups(
360 groupLocalService.getGroups(companyId, parentGroupId, site));
361 }
362
363
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(permissionChecker.getCompanyId(), null,
387 null, null, params, true, 0, max));
388 }
389
390 Set<Group> groups = new LinkedHashSet<>();
391
392 List<Group> userSitesGroups = getUserSitesGroups(null, max);
393
394 Iterator<Group> itr = userSitesGroups.iterator();
395
396 while (itr.hasNext()) {
397 Group group = itr.next();
398
399 if (group.isSite() &&
400 PortletPermissionUtil.hasControlPanelAccessPermission(
401 permissionChecker, group.getGroupId(), portlets)) {
402
403 groups.add(group);
404 }
405 }
406
407 return new ArrayList<>(groups);
408 }
409
410
422 @Deprecated
423 @Override
424 public List<Group> getManageableSites(Collection<Portlet> portlets, int max)
425 throws PortalException {
426
427 return getManageableSiteGroups(portlets, max);
428 }
429
430
436 @Override
437 public List<Group> getOrganizationsGroups(List<Organization> organizations)
438 throws PortalException {
439
440 List<Group> groups = groupLocalService.getOrganizationsGroups(
441 organizations);
442
443 return filterGroups(groups);
444 }
445
446
453 @Override
454 public Group getUserGroup(long companyId, long userId)
455 throws PortalException {
456
457 Group group = groupLocalService.getUserGroup(companyId, userId);
458
459 GroupPermissionUtil.check(
460 getPermissionChecker(), group, ActionKeys.VIEW);
461
462 return group;
463 }
464
465
471 @Override
472 public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
473 throws PortalException {
474
475 List<Group> groups = groupLocalService.getUserGroupsGroups(userGroups);
476
477 return filterGroups(groups);
478 }
479
480
501 @Override
502 public List<Group> getUserOrganizationsGroups(
503 long userId, int start, int end)
504 throws PortalException {
505
506 List<Group> groups = groupLocalService.getUserOrganizationsGroups(
507 userId, start, end);
508
509 return filterGroups(groups);
510 }
511
512
516 @Deprecated
517 @Override
518 public List<Group> getUserPlaces(
519 long userId, String[] classNames, boolean includeControlPanel,
520 int max)
521 throws PortalException {
522
523 return getUserSitesGroups(userId, classNames, max);
524 }
525
526
560 @Deprecated
561 @Override
562 public List<Group> getUserPlaces(long userId, String[] classNames, int max)
563 throws PortalException {
564
565 return getUserSitesGroups(userId, classNames, max);
566 }
567
568
601 @Deprecated
602 @Override
603 public List<Group> getUserPlaces(String[] classNames, int max)
604 throws PortalException {
605
606 return getUserSitesGroups(classNames, max);
607 }
608
609
618 @Deprecated
619 @Override
620 public int getUserPlacesCount() throws PortalException {
621 return getUserSitesGroupsCount();
622 }
623
624
632 @Deprecated
633 @Override
634 public List<Group> getUserSites() throws PortalException {
635 return getUserSitesGroups();
636 }
637
638 @Override
639 public List<Group> getUserSitesGroups() throws PortalException {
640 return getUserSitesGroups(null, QueryUtil.ALL_POS);
641 }
642
643
675 @Override
676 public List<Group> getUserSitesGroups(
677 long userId, String[] classNames, int max)
678 throws PortalException {
679
680 User user = userPersistence.findByPrimaryKey(userId);
681
682 if (user.isDefaultUser()) {
683 return Collections.emptyList();
684 }
685
686 Set<Group> userSiteGroups = new LinkedHashSet<>();
687
688 if (classNames == null) {
689 classNames = new String[] {
690 Company.class.getName(), Group.class.getName(),
691 Organization.class.getName(), User.class.getName()
692 };
693 }
694
695 if (ArrayUtil.contains(classNames, User.class.getName())) {
696 if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
697 PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
698
699 userSiteGroups.add(user.getGroup());
700
701 if (userSiteGroups.size() == max) {
702 return new ArrayList<>(userSiteGroups);
703 }
704 }
705 }
706
707 if (ArrayUtil.contains(classNames, Company.class.getName())) {
708 userSiteGroups.add(
709 groupLocalService.getCompanyGroup(user.getCompanyId()));
710
711 if (userSiteGroups.size() == max) {
712 return new ArrayList<>(userSiteGroups);
713 }
714 }
715
716 if (ArrayUtil.contains(classNames, Group.class.getName()) ||
717 ArrayUtil.contains(classNames, Organization.class.getName())) {
718
719 UserBag userBag = UserBagFactoryUtil.create(userId);
720
721 if (ArrayUtil.contains(classNames, Group.class.getName())) {
722 for (Group group : userBag.getUserGroups()) {
723 if (group.isActive() && group.isSite()) {
724 if (userSiteGroups.add(group) &&
725 (userSiteGroups.size() == max)) {
726
727 return new ArrayList<>(userSiteGroups);
728 }
729 }
730 }
731 }
732
733 if (ArrayUtil.contains(classNames, Organization.class.getName())) {
734 for (Group group : userBag.getUserOrgGroups()) {
735 if (group.isActive() && group.isSite()) {
736 if (userSiteGroups.add(group) &&
737 (userSiteGroups.size() == max)) {
738
739 return new ArrayList<>(userSiteGroups);
740 }
741 }
742 }
743 }
744 }
745
746 return new ArrayList<>(userSiteGroups);
747 }
748
749
780 @Override
781 public List<Group> getUserSitesGroups(String[] classNames, int max)
782 throws PortalException {
783
784 return getUserSitesGroups(getGuestOrUserId(), classNames, max);
785 }
786
787
795 @Override
796 public int getUserSitesGroupsCount() throws PortalException {
797 List<Group> userSitesGroups = getUserSitesGroups(
798 getGuestOrUserId(), null, QueryUtil.ALL_POS);
799
800 return userSitesGroups.size();
801 }
802
803
813 @Override
814 public boolean hasUserGroup(long userId, long groupId)
815 throws PortalException {
816
817 try {
818 UserPermissionUtil.check(
819 getPermissionChecker(), userId, ActionKeys.VIEW);
820 }
821 catch (PrincipalException pe) {
822 GroupPermissionUtil.check(
823 getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
824 }
825
826 return groupLocalService.hasUserGroup(userId, groupId);
827 }
828
829 @Override
830 public List<Group> search(
831 long companyId, long[] classNameIds, String keywords,
832 LinkedHashMap<String, Object> params, int start, int end,
833 OrderByComparator<Group> obc)
834 throws PortalException {
835
836 List<Group> groups = groupLocalService.search(
837 companyId, classNameIds, keywords, params, start, end, obc);
838
839 return filterGroups(groups);
840 }
841
842 @Override
843 public List<Group> search(
844 long companyId, long[] classNameIds, String name,
845 String description, LinkedHashMap<String, Object> params,
846 boolean andOperator, int start, int end,
847 OrderByComparator<Group> obc)
848 throws PortalException {
849
850 List<Group> groups = groupLocalService.search(
851 companyId, classNameIds, name, description, params, andOperator,
852 start, end, obc);
853
854 return filterGroups(groups);
855 }
856
857
887 @Override
888 public List<Group> search(
889 long companyId, String name, String description, String[] params,
890 int start, int end)
891 throws PortalException {
892
893 if (params == null) {
894 params = new String[0];
895 }
896
897 LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
898 params);
899
900 List<Group> groups = groupLocalService.search(
901 companyId, name, description, paramsObj, true, start, end);
902
903 return filterGroups(groups);
904 }
905
906
923 @Override
924 public int searchCount(
925 long companyId, String name, String description, String[] params) {
926
927 if (params == null) {
928 params = new String[0];
929 }
930
931 LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
932 params);
933
934 return groupLocalService.searchCount(
935 companyId, name, description, paramsObj, true);
936 }
937
938
945 @Override
946 public void setRoleGroups(long roleId, long[] groupIds)
947 throws PortalException {
948
949 RolePermissionUtil.check(
950 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
951
952 groupLocalService.setRoleGroups(roleId, groupIds);
953 }
954
955
961 @Override
962 public void unsetRoleGroups(long roleId, long[] groupIds)
963 throws PortalException {
964
965 RolePermissionUtil.check(
966 getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
967
968 groupLocalService.unsetRoleGroups(roleId, groupIds);
969 }
970
971
979 @Override
980 public Group updateFriendlyURL(long groupId, String friendlyURL)
981 throws PortalException {
982
983 GroupPermissionUtil.check(
984 getPermissionChecker(), groupId, ActionKeys.UPDATE);
985
986 return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
987 }
988
989 @Override
990 public Group updateGroup(
991 long groupId, long parentGroupId, Map<Locale, String> nameMap,
992 Map<Locale, String> descriptionMap, int type,
993 boolean manualMembership, int membershipRestriction,
994 String friendlyURL, boolean inheritContent, boolean active,
995 ServiceContext serviceContext)
996 throws PortalException {
997
998 Group group = groupPersistence.findByPrimaryKey(groupId);
999
1000 GroupPermissionUtil.check(
1001 getPermissionChecker(), group, ActionKeys.UPDATE);
1002
1003 if (group.getParentGroupId() != parentGroupId) {
1004 if (parentGroupId == GroupConstants.DEFAULT_PARENT_GROUP_ID) {
1005 PortalPermissionUtil.check(
1006 getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
1007 }
1008 else {
1009 GroupPermissionUtil.check(
1010 getPermissionChecker(), parentGroupId,
1011 ActionKeys.ADD_COMMUNITY);
1012 }
1013 }
1014
1015 if (group.isSite()) {
1016 Group oldGroup = group;
1017
1018 List<AssetCategory> oldAssetCategories =
1019 assetCategoryLocalService.getCategories(
1020 Group.class.getName(), groupId);
1021
1022 List<AssetTag> oldAssetTags = assetTagLocalService.getTags(
1023 Group.class.getName(), groupId);
1024
1025 ExpandoBridge oldExpandoBridge = oldGroup.getExpandoBridge();
1026
1027 Map<String, Serializable> oldExpandoAttributes =
1028 oldExpandoBridge.getAttributes();
1029
1030 group = groupLocalService.updateGroup(
1031 groupId, parentGroupId, nameMap, descriptionMap, type,
1032 manualMembership, membershipRestriction, friendlyURL,
1033 inheritContent, active, serviceContext);
1034
1035 SiteMembershipPolicyUtil.verifyPolicy(
1036 group, oldGroup, oldAssetCategories, oldAssetTags,
1037 oldExpandoAttributes, null);
1038
1039 return group;
1040 }
1041 else {
1042 return groupLocalService.updateGroup(
1043 groupId, parentGroupId, nameMap, descriptionMap, type,
1044 manualMembership, membershipRestriction, friendlyURL,
1045 inheritContent, active, serviceContext);
1046 }
1047 }
1048
1049
1074 @Deprecated
1075 @Override
1076 public Group updateGroup(
1077 long groupId, long parentGroupId, String name, String description,
1078 int type, boolean manualMembership, int membershipRestriction,
1079 String friendlyURL, boolean inheritContent, boolean active,
1080 ServiceContext serviceContext)
1081 throws PortalException {
1082
1083 return updateGroup(
1084 groupId, parentGroupId, getLocalizationMap(name),
1085 getLocalizationMap(description), type, manualMembership,
1086 membershipRestriction, friendlyURL, inheritContent, active,
1087 serviceContext);
1088 }
1089
1090
1098 @Override
1099 public Group updateGroup(long groupId, String typeSettings)
1100 throws PortalException {
1101
1102 Group group = groupPersistence.findByPrimaryKey(groupId);
1103
1104 GroupPermissionUtil.check(
1105 getPermissionChecker(), group, ActionKeys.UPDATE);
1106
1107 if (group.isSite()) {
1108 Group oldGroup = group;
1109
1110 UnicodeProperties oldTypeSettingsProperties =
1111 oldGroup.getTypeSettingsProperties();
1112
1113 group = groupLocalService.updateGroup(groupId, typeSettings);
1114
1115 RatingsDataTransformerUtil.transformGroupRatingsData(
1116 groupId, oldTypeSettingsProperties,
1117 group.getTypeSettingsProperties());
1118
1119 SiteMembershipPolicyUtil.verifyPolicy(
1120 group, oldGroup, null, null, null, oldTypeSettingsProperties);
1121
1122 return group;
1123 }
1124 else {
1125 return groupLocalService.updateGroup(groupId, typeSettings);
1126 }
1127 }
1128
1129 @Override
1130 public void updateStagedPortlets(
1131 long groupId, Map<String, String> stagedPortletIds)
1132 throws PortalException {
1133
1134 Group group = groupPersistence.findByPrimaryKey(groupId);
1135
1136 GroupPermissionUtil.check(
1137 getPermissionChecker(), group, ActionKeys.UPDATE);
1138
1139 UnicodeProperties typeSettingsProperties =
1140 group.getTypeSettingsProperties();
1141
1142 for (String stagedPortletId : stagedPortletIds.keySet()) {
1143 typeSettingsProperties.setProperty(
1144 StagingUtil.getStagedPortletId(stagedPortletId),
1145 stagedPortletIds.get(stagedPortletId));
1146 }
1147
1148 groupLocalService.updateGroup(group);
1149 }
1150
1151 protected List<Group> filterGroups(List<Group> groups)
1152 throws PortalException {
1153
1154 List<Group> filteredGroups = new ArrayList<>();
1155
1156 for (Group group : groups) {
1157 if (GroupPermissionUtil.contains(
1158 getPermissionChecker(), group, ActionKeys.VIEW)) {
1159
1160 filteredGroups.add(group);
1161 }
1162 }
1163
1164 return filteredGroups;
1165 }
1166
1167 protected Map<Locale, String> getLocalizationMap(String value) {
1168 Map<Locale, String> map = new HashMap<>();
1169
1170 map.put(LocaleUtil.getDefault(), value);
1171
1172 return map;
1173 }
1174
1175 }