001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.UniqueList;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.GroupConstants;
026    import com.liferay.portal.model.Organization;
027    import com.liferay.portal.model.Portlet;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.UserGroup;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.permission.ActionKeys;
032    import com.liferay.portal.security.permission.PermissionChecker;
033    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portal.service.base.GroupServiceBaseImpl;
036    import com.liferay.portal.service.permission.GroupPermissionUtil;
037    import com.liferay.portal.service.permission.PortalPermissionUtil;
038    import com.liferay.portal.service.permission.PortletPermissionUtil;
039    import com.liferay.portal.service.permission.RolePermissionUtil;
040    import com.liferay.portal.service.permission.UserPermissionUtil;
041    import com.liferay.portal.util.PropsValues;
042    
043    import java.util.ArrayList;
044    import java.util.Collection;
045    import java.util.Collections;
046    import java.util.Iterator;
047    import java.util.LinkedHashMap;
048    import java.util.List;
049    
050    /**
051     * The group remote service is responsible for accessing, creating, modifying
052     * and deleting groups. For more information on group services and Group, see
053     * {@link com.liferay.portal.service.impl.GroupLocalServiceImpl}.
054     *
055     * @author Brian Wing Shun Chan
056     */
057    public class GroupServiceImpl extends GroupServiceBaseImpl {
058    
059            /**
060             * Adds a group.
061             *
062             * @param  liveGroupId the primary key of the live group
063             * @param  name the entity's name
064             * @param  description the group's description (optionally
065             *         <code>null</code>)
066             * @param  type the group's type. For more information see {@link
067             *         com.liferay.portal.model.GroupConstants}
068             * @param  friendlyURL the group's friendlyURL (optionally
069             *         <code>null</code>)
070             * @param  site whether the group is to be associated with a main site
071             * @param  active whether the group is active
072             * @param  serviceContext the service context to be applied (optionally
073             *         <code>null</code>). Can set the asset category IDs and asset tag
074             *         names for the group, and can set whether the group is for staging
075             * @return the group
076             * @throws PortalException if the user did not have permission to add the
077             *         group, if a creator could not be found, if the group's
078             *         information was invalid, if a layout could not be found, or if a
079             *         valid friendly URL could not be created for the group
080             * @throws SystemException if a system exception occurred
081             */
082            @Override
083            public Group addGroup(
084                            long liveGroupId, String name, String description, int type,
085                            String friendlyURL, boolean site, boolean active,
086                            ServiceContext serviceContext)
087                    throws PortalException, SystemException {
088    
089                    GroupPermissionUtil.check(
090                            getPermissionChecker(), liveGroupId, ActionKeys.UPDATE);
091    
092                    return groupLocalService.addGroup(
093                            getUserId(), null, 0, liveGroupId, name, description, type,
094                            friendlyURL, site, active, serviceContext);
095            }
096    
097            /**
098             * Adds the group using the group default live group ID.
099             *
100             * @param  name the entity's name
101             * @param  description the group's description (optionally
102             *         <code>null</code>)
103             * @param  type the group's type. For more information see {@link
104             *         com.liferay.portal.model.GroupConstants}
105             * @param  friendlyURL the group's friendlyURL
106             * @param  site whether the group is to be associated with a main site
107             * @param  active whether the group is active
108             * @param  serviceContext the service context to be applied (optionally
109             *         <code>null</code>). Can set asset category IDs and asset tag
110             *         names for the group, and can set whether the group is for staging
111             * @return the group
112             * @throws PortalException if the user did not have permission to add the
113             *         group, if a creator could not be found, if the group's
114             *         information was invalid, if a layout could not be found, or if a
115             *         valid friendly URL could not be created for the group
116             * @throws SystemException if a system exception occurred
117             */
118            @Override
119            public Group addGroup(
120                            String name, String description, int type, String friendlyURL,
121                            boolean site, boolean active, ServiceContext serviceContext)
122                    throws PortalException, SystemException {
123    
124                    PortalPermissionUtil.check(
125                            getPermissionChecker(), ActionKeys.ADD_COMMUNITY);
126    
127                    return groupLocalService.addGroup(
128                            getUserId(), null, 0, name, description, type, friendlyURL, site,
129                            active, serviceContext);
130            }
131    
132            /**
133             * Adds the groups to the role.
134             *
135             * @param  roleId the primary key of the role
136             * @param  groupIds the primary keys of the groups
137             * @throws PortalException if the user did not have permission to update the
138             *         role
139             * @throws SystemException if a system exception occurred
140             */
141            @Override
142            public void addRoleGroups(long roleId, long[] groupIds)
143                    throws PortalException, SystemException {
144    
145                    RolePermissionUtil.check(
146                            getPermissionChecker(), roleId, ActionKeys.UPDATE);
147    
148                    groupLocalService.addRoleGroups(roleId, groupIds);
149            }
150    
151            /**
152             * Deletes the group.
153             *
154             * <p>
155             * The group is unstaged and its assets and resources including layouts,
156             * membership requests, subscriptions, teams, blogs, bookmarks, calendar
157             * events, image gallery, journals, message boards, polls, shopping related
158             * entities, software catalog, and wikis are also deleted.
159             * </p>
160             *
161             * @param  groupId the primary key of the group
162             * @throws PortalException if the user did not have permission to delete the
163             *         group or its assets or resources, if a group with the primary key
164             *         could not be found, or if the group was a system group
165             * @throws SystemException if a system exception occurred
166             */
167            @Override
168            public void deleteGroup(long groupId)
169                    throws PortalException, SystemException {
170    
171                    GroupPermissionUtil.check(
172                            getPermissionChecker(), groupId, ActionKeys.DELETE);
173    
174                    groupLocalService.deleteGroup(groupId);
175            }
176    
177            /**
178             * Returns the group with the primary key.
179             *
180             * @param  groupId the primary key of the group
181             * @return the group with the primary key
182             * @throws PortalException if a group with the primary key could not be
183             *         found or if the current user did not have permission to view the
184             *         group
185             * @throws SystemException if a system exception occurred
186             */
187            @Override
188            public Group getGroup(long groupId)
189                    throws PortalException, SystemException {
190    
191                    GroupPermissionUtil.check(
192                            getPermissionChecker(), groupId, ActionKeys.VIEW);
193    
194                    return groupLocalService.getGroup(groupId);
195            }
196    
197            /**
198             * Returns the group with the name.
199             *
200             * @param  companyId the primary key of the company
201             * @param  name the group's name
202             * @return the group with the name
203             * @throws PortalException if a matching group could not be found or if the
204             *         current user did not have permission to view the group
205             * @throws SystemException if a system exception occurred
206             */
207            @Override
208            public Group getGroup(long companyId, String name)
209                    throws PortalException, SystemException {
210    
211                    Group group = groupLocalService.getGroup(companyId, name);
212    
213                    GroupPermissionUtil.check(
214                            getPermissionChecker(), group.getGroupId(), ActionKeys.VIEW);
215    
216                    return group;
217            }
218    
219            /**
220             * Returns a range of all the site groups for which the user has control
221             * panel access.
222             *
223             * @param  portlets the portlets to manage
224             * @param  max the upper bound of the range of groups to consider (not
225             *         inclusive)
226             * @return the range of site groups for which the user has control panel
227             *         access
228             * @throws PortalException if a portal exception occurred
229             * @throws SystemException if a system exception occurred
230             */
231            @Override
232            public List<Group> getManageableSites(Collection<Portlet> portlets, int max)
233                    throws PortalException, SystemException {
234    
235                    PermissionChecker permissionChecker = getPermissionChecker();
236    
237                    if (permissionChecker.isCompanyAdmin()) {
238                            LinkedHashMap<String, Object> params =
239                                    new LinkedHashMap<String, Object>();
240    
241                            params.put("site", Boolean.TRUE);
242    
243                            return groupLocalService.search(
244                                    permissionChecker.getCompanyId(), null, null, null, params, 0,
245                                    max);
246                    }
247    
248                    List<Group> groups = new UniqueList<Group>();
249    
250                    groups.addAll(
251                            userPersistence.getGroups(permissionChecker.getUserId(), 0, max));
252                    groups.addAll(
253                            getUserOrganizationsGroups(permissionChecker.getUserId(), 0, max));
254    
255                    List<UserGroup> userGroups = userPersistence.getUserGroups(
256                            permissionChecker.getUserId(), 0, max);
257    
258                    for (UserGroup userGroup : userGroups) {
259                            groups.addAll(
260                                    userGroupPersistence.getGroups(
261                                            userGroup.getUserGroupId(), 0, max));
262                    }
263    
264                    Iterator<Group> itr = groups.iterator();
265    
266                    while (itr.hasNext()) {
267                            Group group = itr.next();
268    
269                            if (!group.isSite() ||
270                                    !PortletPermissionUtil.contains(
271                                            permissionChecker, group.getGroupId(), 0L, portlets,
272                                            ActionKeys.ACCESS_IN_CONTROL_PANEL)) {
273    
274                                    itr.remove();
275                            }
276                    }
277    
278                    return groups;
279            }
280    
281            /**
282             * Returns the groups associated with the organizations.
283             *
284             * @param  organizations the organizations
285             * @return the groups associated with the organizations
286             * @throws PortalException if a portal exception occurred
287             * @throws SystemException if a system exception occurred
288             */
289            @Override
290            public List<Group> getOrganizationsGroups(List<Organization> organizations)
291                    throws PortalException, SystemException {
292    
293                    List<Group> groups = groupLocalService.getOrganizationsGroups(
294                            organizations);
295    
296                    return filterGroups(groups);
297            }
298    
299            /**
300             * Returns the group associated with the user.
301             *
302             * @param  companyId the primary key of the company
303             * @param  userId the primary key of the user
304             * @return the group associated with the user
305             * @throws PortalException if a matching group could not be found or if the
306             *         current user did not have permission to view the group
307             * @throws SystemException if a system exception occurred
308             */
309            @Override
310            public Group getUserGroup(long companyId, long userId)
311                    throws PortalException, SystemException {
312    
313                    Group group = groupLocalService.getUserGroup(companyId, userId);
314    
315                    GroupPermissionUtil.check(
316                            getPermissionChecker(), group.getGroupId(), ActionKeys.VIEW);
317    
318                    return group;
319            }
320    
321            /**
322             * Returns the groups associated with the user groups.
323             *
324             * @param  userGroups the user groups
325             * @return the groups associated with the user groups
326             * @throws PortalException if any one of the user group's group could not be
327             *         found
328             * @throws SystemException if a system exception occurred
329             */
330            @Override
331            public List<Group> getUserGroupsGroups(List<UserGroup> userGroups)
332                    throws PortalException, SystemException {
333    
334                    List<Group> groups = groupLocalService.getUserGroupsGroups(userGroups);
335    
336                    return filterGroups(groups);
337            }
338    
339            /**
340             * Returns the range of all groups associated with the user's organization
341             * groups, including the ancestors of the organization groups, unless portal
342             * property <code>organizations.membership.strict</code> is set to
343             * <code>true</code>.
344             *
345             * <p>
346             * Useful when paginating results. Returns a maximum of <code>end -
347             * start</code> instances. <code>start</code> and <code>end</code> are not
348             * primary keys, they are indexes in the result set. Thus, <code>0</code>
349             * refers to the first result in the set. Setting both <code>start</code>
350             * and <code>end</code> to {@link
351             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
352             * result set.
353             * </p>
354             *
355             * @param  userId the primary key of the user
356             * @param  start the lower bound of the range of groups to consider
357             * @param  end the upper bound of the range of groups to consider (not
358             *         inclusive)
359             * @return the range of groups associated with the user's organizations
360             * @throws PortalException if a user with the primary key could not be found
361             *         or if another portal exception occurred
362             * @throws SystemException if a system exception occurred
363             */
364            @Override
365            public List<Group> getUserOrganizationsGroups(
366                            long userId, int start, int end)
367                    throws PortalException, SystemException {
368    
369                    List<Group> groups = groupLocalService.getUserOrganizationsGroups(
370                            userId, start, end);
371    
372                    return filterGroups(groups);
373            }
374    
375            @Override
376            public List<Group> getUserPlaces(
377                            long userId, String[] classNames, boolean includeControlPanel,
378                            int max)
379                    throws PortalException, SystemException {
380    
381                    User user = userPersistence.fetchByPrimaryKey(userId);
382    
383                    if (user.isDefaultUser()) {
384                            return Collections.emptyList();
385                    }
386    
387                    List<Group> userPlaces = new UniqueList<Group>();
388    
389                    int start = QueryUtil.ALL_POS;
390                    int end = QueryUtil.ALL_POS;
391    
392                    if (max != QueryUtil.ALL_POS) {
393                            start = 0;
394                            end = max;
395                    }
396    
397                    if ((classNames == null) ||
398                            ArrayUtil.contains(classNames, Group.class.getName())) {
399    
400                            LinkedHashMap<String, Object> groupParams =
401                                    new LinkedHashMap<String, Object>();
402    
403                            groupParams.put("active", Boolean.TRUE);
404                            groupParams.put("usersGroups", new Long(userId));
405    
406                            userPlaces.addAll(
407                                    groupLocalService.search(
408                                            user.getCompanyId(), groupParams, start, end));
409                    }
410    
411                    if ((classNames == null) ||
412                            ArrayUtil.contains(classNames, Organization.class.getName())) {
413    
414                            LinkedHashMap<String, Object> organizationParams =
415                                    new LinkedHashMap<String, Object>();
416    
417                            organizationParams.put("usersOrgs", new Long(userId));
418    
419                            List<Organization> userOrgs = organizationLocalService.search(
420                                    user.getCompanyId(), organizationParams, start, end);
421    
422                            for (Organization organization : userOrgs) {
423                                    if (!organization.hasPrivateLayouts() &&
424                                            !organization.hasPublicLayouts()) {
425    
426                                            userPlaces.remove(organization.getGroup());
427                                    }
428                                    else {
429                                            userPlaces.add(0, organization.getGroup());
430                                    }
431    
432                                    if (!PropsValues.ORGANIZATIONS_MEMBERSHIP_STRICT) {
433                                            for (Organization ancestorOrganization :
434                                                            organization.getAncestors()) {
435    
436                                                    if (!organization.hasPrivateLayouts() &&
437                                                            !organization.hasPublicLayouts()) {
438    
439                                                            continue;
440                                                    }
441    
442                                                    userPlaces.add(0, ancestorOrganization.getGroup());
443                                            }
444                                    }
445                            }
446                    }
447    
448                    if ((classNames == null) ||
449                            ArrayUtil.contains(classNames, User.class.getName())) {
450    
451                            if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED ||
452                                    PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
453    
454                                    Group userGroup = user.getGroup();
455    
456                                    userPlaces.add(0, userGroup);
457                            }
458                    }
459    
460                    PermissionChecker permissionChecker = getPermissionChecker();
461    
462                    if (permissionChecker.getUserId() != userId) {
463                            try {
464                                    permissionChecker = PermissionCheckerFactoryUtil.create(user);
465                            }
466                            catch (Exception e) {
467                                    throw new PrincipalException(e);
468                            }
469                    }
470    
471                    if (includeControlPanel &&
472                            PortalPermissionUtil.contains(
473                                    permissionChecker, ActionKeys.VIEW_CONTROL_PANEL)) {
474    
475                            Group controlPanelGroup = groupLocalService.getGroup(
476                                    user.getCompanyId(), GroupConstants.CONTROL_PANEL);
477    
478                            userPlaces.add(0, controlPanelGroup);
479                    }
480    
481                    if ((max != QueryUtil.ALL_POS) && (userPlaces.size() > max)) {
482                            userPlaces = ListUtil.subList(userPlaces, start, end);
483                    }
484    
485                    return Collections.unmodifiableList(userPlaces);
486            }
487    
488            /**
489             * Returns the user's group &quot;places&quot; associated with the group
490             * entity class names, including the control panel group if the user is
491             * permitted to view the control panel.
492             *
493             * <p>
494             * <ul> <li> Class name &quot;User&quot; includes the user's layout set
495             * group. </li> <li> Class name &quot;Organization&quot; includes the user's
496             * immediate organization groups and inherited organization groups. </li>
497             * <li> Class name &quot;Group&quot; includes the user's immediate
498             * organization groups and site groups. </li> <li> A <code>classNames</code>
499             * value of <code>null</code> includes the user's layout set group,
500             * organization groups, inherited organization groups, and site groups.
501             * </li> </ul>
502             * </p>
503             *
504             * @param  userId the primary key of the user
505             * @param  classNames the group entity class names (optionally
506             *         <code>null</code>). For more information see {@link
507             *         #getUserPlaces(long, String[], int)}
508             * @param  max the maximum number of groups to return
509             * @return the user's group &quot;places&quot;
510             * @throws PortalException if a portal exception occurred
511             * @throws SystemException if a system exception occurred
512             */
513            @Override
514            public List<Group> getUserPlaces(long userId, String[] classNames, int max)
515                    throws PortalException, SystemException {
516    
517                    return getUserPlaces(userId, classNames, false, max);
518            }
519    
520            /**
521             * Returns the guest or current user's group &quot;places&quot; associated
522             * with the group entity class names, including the control panel group if
523             * the user is permitted to view the control panel.
524             *
525             * <p>
526             * <ul> <li> Class name &quot;User&quot; includes the user's layout set
527             * group. </li> <li> Class name &quot;Organization&quot; includes the user's
528             * immediate organization groups and inherited organization groups. </li>
529             * <li> Class name &quot;Group&quot; includes the user's immediate
530             * organization groups and site groups. </li> <li> A <code>classNames</code>
531             * value of <code>null</code> includes the user's layout set group,
532             * organization groups, inherited organization groups, and site groups.
533             * </li> </ul>
534             * </p>
535             *
536             * @param  classNames the group entity class names (optionally
537             *         <code>null</code>). For more information see {@link
538             *         #getUserPlaces(String[], int)}
539             * @param  max the maximum number of groups to return
540             * @return the user's group &quot;places&quot;
541             * @throws PortalException if a portal exception occurred
542             * @throws SystemException if a system exception occurred
543             */
544            @Override
545            public List<Group> getUserPlaces(String[] classNames, int max)
546                    throws PortalException, SystemException {
547    
548                    return getUserPlaces(getGuestOrUserId(), classNames, false, max);
549            }
550    
551            /**
552             * Returns the guest or current user's layout set group, organization
553             * groups, inherited organization groups, and site groups.
554             *
555             * @return the user's layout set group, organization groups, and inherited
556             *         organization groups, and site groups
557             * @throws PortalException if a portal exception occurred
558             * @throws SystemException if a system exception occurred
559             */
560            @Override
561            public List<Group> getUserSites() throws PortalException, SystemException {
562                    return getUserPlaces(null, QueryUtil.ALL_POS);
563            }
564    
565            /**
566             * Returns <code>true</code> if the user is associated with the group,
567             * including the user's inherited organizations and user groups. System and
568             * staged groups are not included.
569             *
570             * @param  userId the primary key of the user
571             * @param  groupId the primary key of the group
572             * @return <code>true</code> if the user is associated with the group;
573             *         <code>false</code> otherwise
574             * @throws PortalException if the current user did not have permission to
575             *         view the user or group members
576             * @throws SystemException if a system exception occurred
577             */
578            @Override
579            public boolean hasUserGroup(long userId, long groupId)
580                    throws PortalException, SystemException {
581    
582                    try {
583                            UserPermissionUtil.check(
584                                    getPermissionChecker(), userId, ActionKeys.VIEW);
585                    }
586                    catch (PrincipalException pe) {
587                            GroupPermissionUtil.check(
588                                    getPermissionChecker(), groupId, ActionKeys.VIEW_MEMBERS);
589                    }
590    
591                    return groupLocalService.hasUserGroup(userId, groupId);
592            }
593    
594            /**
595             * Returns a name ordered range of all the site groups and organization
596             * groups that match the name and description, optionally including the
597             * user's inherited organization groups and user groups. System and staged
598             * groups are not included.
599             *
600             * <p>
601             * Useful when paginating results. Returns a maximum of <code>end -
602             * start</code> instances. <code>start</code> and <code>end</code> are not
603             * primary keys, they are indexes in the result set. Thus, <code>0</code>
604             * refers to the first result in the set. Setting both <code>start</code>
605             * and <code>end</code> to {@link
606             * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full
607             * result set.
608             * </p>
609             *
610             * @param  companyId the primary key of the company
611             * @param  name the group's name (optionally <code>null</code>)
612             * @param  description the group's description (optionally
613             *         <code>null</code>)
614             * @param  params the finder params (optionally <code>null</code>). To
615             *         include the user's inherited organizations and user groups in the
616             *         search, add entries having &quot;usersGroups&quot; and
617             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
618             *         information see {@link
619             *         com.liferay.portal.service.persistence.GroupFinder}
620             * @param  start the lower bound of the range of groups to return
621             * @param  end the upper bound of the range of groups to return (not
622             *         inclusive)
623             * @return the matching groups ordered by name
624             * @throws PortalException if a portal exception occurred
625             * @throws SystemException if a system exception occurred
626             */
627            @Override
628            public List<Group> search(
629                            long companyId, String name, String description, String[] params,
630                            int start, int end)
631                    throws PortalException, SystemException {
632    
633                    if (params == null) {
634                            params = new String[0];
635                    }
636    
637                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
638                            params);
639    
640                    List<Group> groups = groupLocalService.search(
641                            companyId, name, description, paramsObj, start, end);
642    
643                    return filterGroups(groups);
644            }
645    
646            /**
647             * Returns the number of groups and organization groups that match the name
648             * and description, optionally including the user's inherited organizations
649             * and user groups. System and staged groups are not included.
650             *
651             * @param  companyId the primary key of the company
652             * @param  name the group's name (optionally <code>null</code>)
653             * @param  description the group's description (optionally
654             *         <code>null</code>)
655             * @param  params the finder params (optionally <code>null</code>). To
656             *         include the user's inherited organizations and user groups in the
657             *         search, add entries having &quot;usersGroups&quot; and
658             *         &quot;inherit&quot; as keys mapped to the the user's ID. For more
659             *         information see {@link
660             *         com.liferay.portal.service.persistence.GroupFinder}
661             * @return the number of matching groups
662             * @throws SystemException if a system exception occurred
663             */
664            @Override
665            public int searchCount(
666                            long companyId, String name, String description, String[] params)
667                    throws SystemException {
668    
669                    if (params == null) {
670                            params = new String[0];
671                    }
672    
673                    LinkedHashMap<String, Object> paramsObj = MapUtil.toLinkedHashMap(
674                            params);
675    
676                    return groupLocalService.searchCount(
677                            companyId, name, description, paramsObj);
678            }
679    
680            /**
681             * Sets the groups associated with the role, removing and adding
682             * associations as necessary.
683             *
684             * @param  roleId the primary key of the role
685             * @param  groupIds the primary keys of the groups
686             * @throws PortalException if the user did not have permission to update
687             *         update the role
688             * @throws SystemException if a system exception occurred
689             */
690            @Override
691            public void setRoleGroups(long roleId, long[] groupIds)
692                    throws PortalException, SystemException {
693    
694                    RolePermissionUtil.check(
695                            getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
696    
697                    groupLocalService.setRoleGroups(roleId, groupIds);
698            }
699    
700            /**
701             * Removes the groups from the role.
702             *
703             * @param  roleId the primary key of the role
704             * @param  groupIds the primary keys of the groups
705             * @throws PortalException if the user did not have permission to update the
706             *         role
707             * @throws SystemException if a system exception occurred
708             */
709            @Override
710            public void unsetRoleGroups(long roleId, long[] groupIds)
711                    throws PortalException, SystemException {
712    
713                    RolePermissionUtil.check(
714                            getPermissionChecker(), roleId, ActionKeys.ASSIGN_MEMBERS);
715    
716                    groupLocalService.unsetRoleGroups(roleId, groupIds);
717            }
718    
719            /**
720             * Updates the group's friendly URL.
721             *
722             * @param  groupId the primary key of the group
723             * @param  friendlyURL the group's new friendlyURL (optionally
724             *         <code>null</code>)
725             * @return the group
726             * @throws PortalException if the user did not have permission to update the
727             *         group, if a group with the primary key could not be found, or if
728             *         a valid friendly URL could not be created for the group
729             * @throws SystemException if a system exception occurred
730             */
731            @Override
732            public Group updateFriendlyURL(long groupId, String friendlyURL)
733                    throws PortalException, SystemException {
734    
735                    GroupPermissionUtil.check(
736                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
737    
738                    return groupLocalService.updateFriendlyURL(groupId, friendlyURL);
739            }
740    
741            /**
742             * Updates the group's type settings.
743             *
744             * @param  groupId the primary key of the group
745             * @param  typeSettings the group's new type settings (optionally
746             *         <code>null</code>)
747             * @return the group
748             * @throws PortalException if the user did not have permission to update the
749             *         group or if a group with the primary key could not be found
750             * @throws SystemException if a system exception occurred
751             */
752            @Override
753            public Group updateGroup(long groupId, String typeSettings)
754                    throws PortalException, SystemException {
755    
756                    GroupPermissionUtil.check(
757                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
758    
759                    return groupLocalService.updateGroup(groupId, typeSettings);
760            }
761    
762            /**
763             * Updates the group.
764             *
765             * @param  groupId the primary key of the group
766             * @param  name the group's new name
767             * @param  description the group's new description (optionally
768             *         <code>null</code>)
769             * @param  type the group's new type. For more information see {@link
770             *         com.liferay.portal.model.GroupConstants}
771             * @param  friendlyURL the group's new friendlyURL (optionally
772             *         <code>null</code>)
773             * @param  active whether the group is active
774             * @param  serviceContext the service context to be applied (optionally
775             *         <code>null</code>). Can set the asset category IDs and asset tag
776             *         names for the group.
777             * @return the group
778             * @throws PortalException if the user did not have permission to update the
779             *         group, if a group with the primary key could not be found, if the
780             *         friendly URL was invalid or could one not be created
781             * @throws SystemException if a system exception occurred
782             */
783            @Override
784            public Group updateGroup(
785                            long groupId, String name, String description, int type,
786                            String friendlyURL, boolean active, ServiceContext serviceContext)
787                    throws PortalException, SystemException {
788    
789                    GroupPermissionUtil.check(
790                            getPermissionChecker(), groupId, ActionKeys.UPDATE);
791    
792                    return groupLocalService.updateGroup(
793                            groupId, name, description, type, friendlyURL, active,
794                            serviceContext);
795            }
796    
797            protected List<Group> filterGroups(List<Group> groups)
798                    throws PortalException, SystemException {
799    
800                    List<Group> filteredGroups = new ArrayList<Group>();
801    
802                    for (Group group : groups) {
803                            if (GroupPermissionUtil.contains(
804                                            getPermissionChecker(), group.getGroupId(),
805                                            ActionKeys.VIEW)) {
806    
807                                    filteredGroups.add(group);
808                            }
809                    }
810    
811                    return filteredGroups;
812            }
813    
814    }