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.model.Team;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.service.base.TeamServiceBaseImpl;
023    import com.liferay.portal.service.permission.GroupPermissionUtil;
024    import com.liferay.portal.service.permission.TeamPermissionUtil;
025    import com.liferay.portal.service.permission.UserPermissionUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class TeamServiceImpl extends TeamServiceBaseImpl {
033    
034            @Override
035            public Team addTeam(long groupId, String name, String description)
036                    throws PortalException {
037    
038                    GroupPermissionUtil.check(
039                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
040    
041                    return teamLocalService.addTeam(
042                            getUserId(), groupId, name, description);
043            }
044    
045            @Override
046            public void deleteTeam(long teamId) throws PortalException {
047                    TeamPermissionUtil.check(
048                            getPermissionChecker(), teamId, ActionKeys.DELETE);
049    
050                    teamLocalService.deleteTeam(teamId);
051            }
052    
053            @Override
054            public List<Team> getGroupTeams(long groupId) throws PortalException {
055                    GroupPermissionUtil.check(
056                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
057    
058                    return teamLocalService.getGroupTeams(groupId);
059            }
060    
061            @Override
062            public Team getTeam(long teamId) throws PortalException {
063                    Team team = teamLocalService.getTeam(teamId);
064    
065                    TeamPermissionUtil.check(getPermissionChecker(), team, ActionKeys.VIEW);
066    
067                    return team;
068            }
069    
070            @Override
071            public Team getTeam(long groupId, String name) throws PortalException {
072                    Team team = teamLocalService.getTeam(groupId, name);
073    
074                    TeamPermissionUtil.check(getPermissionChecker(), team, ActionKeys.VIEW);
075    
076                    return team;
077            }
078    
079            @Override
080            public List<Team> getUserTeams(long userId) throws PortalException {
081                    UserPermissionUtil.check(
082                            getPermissionChecker(), userId, ActionKeys.UPDATE);
083    
084                    return teamLocalService.getUserTeams(userId);
085            }
086    
087            @Override
088            public List<Team> getUserTeams(long userId, long groupId)
089                    throws PortalException {
090    
091                    GroupPermissionUtil.check(
092                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
093    
094                    return teamLocalService.getUserTeams(userId, groupId);
095            }
096    
097            @Override
098            public boolean hasUserTeam(long userId, long teamId)
099                    throws PortalException {
100    
101                    PermissionChecker permissionChecker = getPermissionChecker();
102    
103                    Team team = teamPersistence.findByPrimaryKey(teamId);
104    
105                    if (!GroupPermissionUtil.contains(
106                                    permissionChecker, team.getGroupId(),
107                                    ActionKeys.MANAGE_TEAMS) &&
108                            !UserPermissionUtil.contains(
109                                    permissionChecker, userId, ActionKeys.UPDATE)) {
110    
111                            throw new PrincipalException();
112                    }
113    
114                    return userPersistence.containsTeam(userId, teamId);
115            }
116    
117            @Override
118            public Team updateTeam(long teamId, String name, String description)
119                    throws PortalException {
120    
121                    TeamPermissionUtil.check(
122                            getPermissionChecker(), teamId, ActionKeys.UPDATE);
123    
124                    return teamLocalService.updateTeam(teamId, name, description);
125            }
126    
127    }