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.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.model.Team;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.base.TeamServiceBaseImpl;
025    import com.liferay.portal.service.permission.GroupPermissionUtil;
026    import com.liferay.portal.service.permission.TeamPermissionUtil;
027    import com.liferay.portal.service.permission.UserPermissionUtil;
028    
029    import java.util.LinkedHashMap;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class TeamServiceImpl extends TeamServiceBaseImpl {
036    
037            /**
038             * @deprecated As of 7.0.0, replaced by {@link #addTeam(long, String,
039             *             String, ServiceContext)}
040             */
041            @Deprecated
042            @Override
043            public Team addTeam(long groupId, String name, String description)
044                    throws PortalException {
045    
046                    GroupPermissionUtil.check(
047                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
048    
049                    return teamLocalService.addTeam(
050                            getUserId(), groupId, name, description);
051            }
052    
053            @Override
054            public Team addTeam(
055                            long groupId, String name, String description,
056                            ServiceContext serviceContext)
057                    throws PortalException {
058    
059                    GroupPermissionUtil.check(
060                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
061    
062                    return teamLocalService.addTeam(
063                            getUserId(), groupId, name, description, serviceContext);
064            }
065    
066            @Override
067            public void deleteTeam(long teamId) throws PortalException {
068                    TeamPermissionUtil.check(
069                            getPermissionChecker(), teamId, ActionKeys.DELETE);
070    
071                    teamLocalService.deleteTeam(teamId);
072            }
073    
074            @Override
075            public List<Team> getGroupTeams(long groupId) throws PortalException {
076                    GroupPermissionUtil.check(
077                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
078    
079                    return teamLocalService.getGroupTeams(groupId);
080            }
081    
082            @Override
083            public Team getTeam(long teamId) throws PortalException {
084                    Team team = teamLocalService.getTeam(teamId);
085    
086                    TeamPermissionUtil.check(getPermissionChecker(), team, ActionKeys.VIEW);
087    
088                    return team;
089            }
090    
091            @Override
092            public Team getTeam(long groupId, String name) throws PortalException {
093                    Team team = teamLocalService.getTeam(groupId, name);
094    
095                    TeamPermissionUtil.check(getPermissionChecker(), team, ActionKeys.VIEW);
096    
097                    return team;
098            }
099    
100            @Override
101            public List<Team> getUserTeams(long userId) throws PortalException {
102                    UserPermissionUtil.check(
103                            getPermissionChecker(), userId, ActionKeys.UPDATE);
104    
105                    return teamLocalService.getUserTeams(userId);
106            }
107    
108            @Override
109            public List<Team> getUserTeams(long userId, long groupId)
110                    throws PortalException {
111    
112                    GroupPermissionUtil.check(
113                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
114    
115                    return teamLocalService.getUserTeams(userId, groupId);
116            }
117    
118            @Override
119            public boolean hasUserTeam(long userId, long teamId)
120                    throws PortalException {
121    
122                    PermissionChecker permissionChecker = getPermissionChecker();
123    
124                    Team team = teamPersistence.findByPrimaryKey(teamId);
125    
126                    if (!GroupPermissionUtil.contains(
127                                    permissionChecker, team.getGroupId(),
128                                    ActionKeys.MANAGE_TEAMS) &&
129                            !UserPermissionUtil.contains(
130                                    permissionChecker, userId, ActionKeys.UPDATE)) {
131    
132                            throw new PrincipalException.MustHavePermission(
133                                    permissionChecker, Team.class.getName(), teamId,
134                                    ActionKeys.MANAGE_TEAMS, ActionKeys.UPDATE);
135                    }
136    
137                    return userPersistence.containsTeam(userId, teamId);
138            }
139    
140            @Override
141            public List<Team> search(
142                    long groupId, String name, String description,
143                    LinkedHashMap<String, Object> params, int start, int end,
144                    OrderByComparator<Team> obc) {
145    
146                    return teamFinder.filterFindByG_N_D(
147                            groupId, name, description, params, start, end, obc);
148            }
149    
150            @Override
151            public int searchCount(
152                    long groupId, String name, String description,
153                    LinkedHashMap<String, Object> params) {
154    
155                    return teamFinder.filterCountByG_N_D(
156                            groupId, name, description, params);
157            }
158    
159            @Override
160            public Team updateTeam(long teamId, String name, String description)
161                    throws PortalException {
162    
163                    TeamPermissionUtil.check(
164                            getPermissionChecker(), teamId, ActionKeys.UPDATE);
165    
166                    return teamLocalService.updateTeam(teamId, name, description);
167            }
168    
169    }