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