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            @Override
067            public Team getTeam(long teamId) throws PortalException, SystemException {
068                    TeamPermissionUtil.check(
069                            getPermissionChecker(), teamId, ActionKeys.VIEW);
070    
071                    return teamLocalService.getTeam(teamId);
072            }
073    
074            @Override
075            public Team getTeam(long groupId, String name)
076                    throws PortalException, SystemException {
077    
078                    Team team = teamLocalService.getTeam(groupId, name);
079    
080                    TeamPermissionUtil.check(getPermissionChecker(), team, ActionKeys.VIEW);
081    
082                    return team;
083            }
084    
085            @Override
086            public List<Team> getUserTeams(long userId)
087                    throws PortalException, SystemException {
088    
089                    UserPermissionUtil.check(
090                            getPermissionChecker(), userId, ActionKeys.UPDATE);
091    
092                    return teamLocalService.getUserTeams(userId);
093            }
094    
095            @Override
096            public List<Team> getUserTeams(long userId, long groupId)
097                    throws PortalException, SystemException {
098    
099                    GroupPermissionUtil.check(
100                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
101    
102                    return teamLocalService.getUserTeams(userId, groupId);
103            }
104    
105            @Override
106            public boolean hasUserTeam(long userId, long teamId)
107                    throws PortalException, SystemException {
108    
109                    PermissionChecker permissionChecker = getPermissionChecker();
110    
111                    Team team = teamPersistence.findByPrimaryKey(teamId);
112    
113                    if (!GroupPermissionUtil.contains(
114                                    permissionChecker, team.getGroupId(),
115                                    ActionKeys.MANAGE_TEAMS) &&
116                            !UserPermissionUtil.contains(
117                                    permissionChecker, userId, ActionKeys.UPDATE)) {
118    
119                            throw new PrincipalException();
120                    }
121    
122                    return userPersistence.containsTeam(userId, teamId);
123            }
124    
125            @Override
126            public Team updateTeam(long teamId, String name, String description)
127                    throws PortalException, SystemException {
128    
129                    TeamPermissionUtil.check(
130                            getPermissionChecker(), teamId, ActionKeys.UPDATE);
131    
132                    return teamLocalService.updateTeam(teamId, name, description);
133            }
134    
135    }