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