001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
072     * Provides the remote service for accessing, adding, deleting, and updating
073     * groups. Its methods include permission checks. Groups are mostly used in
074     * Liferay as a resource container for permissioning and content scoping
075     * purposes.
076     *
077     * @author Brian Wing Shun Chan
078     * @see    GroupLocalServiceImpl
079     */
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            /**
129             * Adds a group.
130             *
131             * @param      parentGroupId the primary key of the parent group
132             * @param      liveGroupId the primary key of the live group
133             * @param      name the entity's name
134             * @param      description the group's description (optionally
135             *             <code>null</code>)
136             * @param      type the group's type. For more information see {@link
137             *             GroupConstants}.
138             * @param      manualMembership whether manual membership is allowed for the
139             *             group
140             * @param      membershipRestriction the group's membership restriction. For
141             *             more information see {@link GroupConstants}.
142             * @param      friendlyURL the group's friendlyURL (optionally
143             *             <code>null</code>)
144             * @param      site whether the group is to be associated with a main site
145             * @param      active whether the group is active
146             * @param      serviceContext the service context to be applied (optionally
147             *             <code>null</code>). Can set the asset category IDs and asset
148             *             tag names for the group, and can set whether the group is for
149             *             staging
150             * @return     the group
151             * @throws     PortalException if a portal exception occurred
152             * @deprecated As of 7.0.0, replaced by {@link #addGroup(long, long, Map,
153             *             Map, int, boolean, int, String, boolean, boolean,
154             *             ServiceContext)}
155             */
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            /**
173             * Adds the groups to the role.
174             *
175             * @param  roleId the primary key of the role
176             * @param  groupIds the primary keys of the groups
177             * @throws PortalException if a portal exception occurred
178             */
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            /**
190             * Checks that the current user is permitted to use the group for Remote
191             * Staging.
192             *
193             * @param  groupId the primary key of the group
194             * @throws PortalException if a portal exception occurred
195             */
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            /**
210             * Deletes the group.
211             *
212             * <p>
213             * The group is unstaged and its assets and resources including layouts,
214             * membership requests, subscriptions, teams, blogs, bookmarks, calendar
215             * events, image gallery, journals, message boards, polls, shopping related
216             * entities, and wikis are also deleted.
217             * </p>
218             *
219             * @param  groupId the primary key of the group
220             * @throws PortalException if a portal exception occurred
221             */
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            /**
251             * Returns the company group.
252             *
253             * @param  companyId the primary key of the company
254             * @return the group associated with the company
255             * @throws PortalException if a portal exception occurred
256             */
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            /**
268             * Returns the group with the primary key.
269             *
270             * @param  groupId the primary key of the group
271             * @return the group with the primary key
272             * @throws PortalException if a portal exception occurred
273             */
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            /**
285             * Returns the group with the name.
286             *
287             * @param  companyId the primary key of the company
288             * @param  groupKey the group key
289             * @return the group with the group key
290             * @throws PortalException if a portal exception occurred
291             */
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            /**
305             * Returns the group's display URL.
306             *
307             * @param  groupId the primary key of the group
308             * @param  privateLayout whether the layout set is private to the group
309             * @param  secureConnection whether the generated URL uses a secure
310             *         connection
311             * @return the group's display URL
312             * @throws PortalException if a group with the primary key could not be
313             *         found or if a portal exception occurred
314             */
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            /**
344             * Returns all the groups that are direct children of the parent group.
345             *
346             * @param  companyId the primary key of the company
347             * @param  parentGroupId the primary key of the parent group
348             * @param  site whether the group is to be associated with a main site
349             * @return the matching groups, or <code>null</code> if no matches were
350             *         found
351             * @throws PortalException if a portal exception occurred
352             */
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            /**
363             * Returns a range of all the site groups for which the user has control
364             * panel access.
365             *
366             * @param  portlets the portlets to manage
367             * @param  max the upper bound of the range of groups to consider (not
368             *         inclusive)
369             * @return the range of site groups for which the user has Control Panel
370             *         access
371             * @throws PortalException if a portal exception occurred
372             */
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            /**
412             * Returns the groups associated with the organizations.
413             *
414             * @param  organizations the organizations
415             * @return the groups associated with the organizations
416             * @throws PortalException if a portal exception occurred
417             */
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            /**
429             * Returns the group directly associated with the user.
430             *
431             * @param  companyId the primary key of the company
432             * @param  userId the primary key of the user
433             * @return the group directly associated with the user
434             * @throws PortalException if a portal exception occurred
435             */
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            /**
449             * Returns the groups associated with the user groups.
450             *
451             * @param  userGroups the user groups
452             * @return the groups associated with the user groups
453             * @throws PortalException if a portal exception occurred
454             */
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            /**
465             * Returns the range of all groups associated with the user's organization
466             * groups, including the ancestors of the organization groups, unless portal
467             * property <code>organizations.membership.strict</code> is set to
468             * <code>true</code>.
469             *
470             * <p>
471             * Useful when paginating results. Returns a maximum of <code>end -
472             * start</code> instances. <code>start</code> and <code>end</code> are not
473             * primary keys, they are indexes in the result set. Thus, <code>0</code>
474             * refers to the first result in the set. Setting both <code>start</code>
475             * and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full
476             * result set.
477             * </p>
478             *
479             * @param  userId the primary key of the user
480             * @param  start the lower bound of the range of groups to consider
481             * @param  end the upper bound of the range of groups to consider (not
482             *         inclusive)
483             * @return the range of groups associated with the user's organizations
484             * @throws PortalException if a portal exception occurred
485             */
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            /**
503             * Returns the user's groups &quot;sites&quot; associated with the group
504             * entity class names, including the Control Panel group if the user is
505             * permitted to view the Control Panel.
506             *
507             * <ul>
508             * <li>
509             * Class name &quot;User&quot; includes the user's layout set
510             * group.
511             * </li>
512             * <li>
513             * Class name &quot;Organization&quot; includes the user's
514             * immediate organization groups and inherited organization groups.
515             * </li>
516             * <li>
517             * Class name &quot;Group&quot; includes the user's immediate
518             * organization groups and site groups.
519             * </li>
520             * <li>
521             * A <code>classNames</code>
522             * value of <code>null</code> includes the user's layout set group,
523             * organization groups, inherited organization groups, and site groups.
524             * </li>
525             * </ul>
526             *
527             * @param  userId the primary key of the user
528             * @param  classNames the group entity class names (optionally
529             *         <code>null</code>). For more information see {@link
530             *         #getUserSitesGroups(long, String[], int)}.
531             * @param  max the maximum number of groups to return
532             * @return the user's groups &quot;sites&quot;
533             * @throws PortalException if a portal exception occurred
534             */
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            /**
646             * Returns the guest or current user's groups &quot;sites&quot; associated
647             * with the group entity class names, including the Control Panel group if
648             * the user is permitted to view the Control Panel.
649             *
650             * <ul>
651             * <li>
652             * Class name &quot;User&quot; includes the user's layout set
653             * group.
654             * </li>
655             * <li>
656             * Class name &quot;Organization&quot; includes the user's
657             * immediate organization groups and inherited organization groups.
658             * </li>
659             * <li>
660             * Class name &quot;Group&quot; includes the user's immediate
661             * organization groups and site groups.
662             * </li>
663             * <li>
664             * A <code>classNames</code>
665             * value of <code>null</code> includes the user's layout set group,
666             * organization groups, inherited organization groups, and site groups.
667             * </li>
668             * </ul>
669             *
670             * @param  classNames the group entity class names (optionally
671             *         <code>null</code>). For more information see {@link
672             *         #getUserSitesGroups(long, String[], int)}.
673             * @param  max the maximum number of groups to return
674             * @return the user's groups &quot;sites&quot;
675             * @throws PortalException if a portal exception occurred
676             */
677            @Override
678            public List<Group> getUserSitesGroups(String[] classNames, int max)
679                    throws PortalException {
680    
681                    return getUserSitesGroups(getGuestOrUserId(), classNames, max);
682            }
683    
684            /**
685             * Returns the number of the guest or current user's groups
686             * &quot;sites&quot; associated with the group entity class names, including
687             * the Control Panel group if the user is permitted to view the Control
688             * Panel.
689             *
690             * @return the number of user's groups &quot;sites&quot;
691             * @throws PortalException if a portal exception occurred
692             */
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            /**
702             * Returns <code>true</code> if the user is associated with the group,
703             * including the user's inherited organizations and user groups. System and
704             * staged groups are not included.
705             *
706             * @param  userId the primary key of the user
707             * @param  groupId the primary key of the group
708             * @return <code>true</code> if the user is associated with the group;
709             *         <code>false</code> otherwise
710             * @throws PortalException if a portal exception occurred
711             */
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            /**
757             * Returns an ordered range of all the site groups and organization groups
758             * that match the name and description, optionally including the user's
759             * inherited organization groups and user groups. System and staged groups
760             * are not included.
761             *
762             * <p>
763             * Useful when paginating results. Returns a maximum of <code>end -
764             * start</code> instances. <code>start</code> and <code>end</code> are not
765             * primary keys, they are indexes in the result set. Thus, <code>0</code>
766             * refers to the first result in the set. Setting both <code>start</code>
767             * and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full
768             * result set.
769             * </p>
770             *
771             * @param  companyId the primary key of the company
772             * @param  name the group's name (optionally <code>null</code>)
773             * @param  description the group's description (optionally
774             *         <code>null</code>)
775             * @param  params the finder params (optionally <code>null</code>). To
776             *         include the user's inherited organizations and user groups in the
777             *         search, add entries having &quot;usersGroups&quot; and
778             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
779             *         information see {@link
780             *         com.liferay.portal.kernel.service.persistence.GroupFinder}.
781             * @param  start the lower bound of the range of groups to return
782             * @param  end the upper bound of the range of groups to return (not
783             *         inclusive)
784             * @return the matching groups ordered by name
785             * @throws PortalException if a portal exception occurred
786             */
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            /**
807             * Returns the number of groups and organization groups that match the name
808             * and description, optionally including the user's inherited organizations
809             * and user groups. System and staged groups are not included.
810             *
811             * @param  companyId the primary key of the company
812             * @param  name the group's name (optionally <code>null</code>)
813             * @param  description the group's description (optionally
814             *         <code>null</code>)
815             * @param  params the finder params (optionally <code>null</code>). To
816             *         include the user's inherited organizations and user groups in the
817             *         search, add entries having &quot;usersGroups&quot; and
818             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
819             *         information see {@link
820             *         com.liferay.portal.kernel.service.persistence.GroupFinder}.
821             * @return the number of matching groups
822             */
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            /**
839             * Sets the groups associated with the role, removing and adding
840             * associations as necessary.
841             *
842             * @param  roleId the primary key of the role
843             * @param  groupIds the primary keys of the groups
844             * @throws PortalException if a portal exception occurred
845             */
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            /**
857             * Removes the groups from the role.
858             *
859             * @param  roleId the primary key of the role
860             * @param  groupIds the primary keys of the groups
861             * @throws PortalException if a portal exception occurred
862             */
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            /**
874             * Updates the group's friendly URL.
875             *
876             * @param  groupId the primary key of the group
877             * @param  friendlyURL the group's new friendlyURL (optionally
878             *         <code>null</code>)
879             * @return the group
880             * @throws PortalException if a portal exception occurred
881             */
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            /**
953             * Updates the group.
954             *
955             * @param      groupId the primary key of the group
956             * @param      parentGroupId the primary key of the parent group
957             * @param      name the group's name
958             * @param      description the group's new description (optionally
959             *             <code>null</code>)
960             * @param      type the group's new type. For more information see {@link
961             *             GroupConstants}.
962             * @param      manualMembership whether manual membership is allowed for the
963             *             group
964             * @param      membershipRestriction the group's membership restriction. For
965             *             more information see {@link GroupConstants}.
966             * @param      friendlyURL the group's new friendlyURL (optionally
967             *             <code>null</code>)
968             * @param      inheritContent whether to inherit content from the parent
969             *             group
970             * @param      active whether the group is active
971             * @param      serviceContext the service context to be applied (optionally
972             *             <code>null</code>). Can set the asset category IDs and asset
973             *             tag names for the group.
974             * @return     the group
975             * @throws     PortalException if a portal exception occurred
976             * @deprecated As of 7.0.0, replaced by {@link #updateGroup(long, long, Map,
977             *             Map, int, boolean, int, String, boolean, boolean,
978             *             ServiceContext)}
979             */
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            /**
997             * Updates the group's type settings.
998             *
999             * @param  groupId the primary key of the group
1000             * @param  typeSettings the group's new type settings (optionally
1001             *         <code>null</code>)
1002             * @return the group
1003             * @throws PortalException if a portal exception occurred
1004             */
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    }