001    /**
002     * Copyright (c) 2000-2012 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            public Team addTeam(long groupId, String name, String description)
036                    throws PortalException, SystemException {
037    
038                    GroupPermissionUtil.check(
039                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
040    
041                    return teamLocalService.addTeam(
042                            getUserId(), groupId, name, description);
043            }
044    
045            public void deleteTeam(long teamId)
046                    throws PortalException, SystemException {
047    
048                    TeamPermissionUtil.check(
049                            getPermissionChecker(), teamId, ActionKeys.DELETE);
050    
051                    teamLocalService.deleteTeam(teamId);
052            }
053    
054            public List<Team> getGroupTeams(long groupId)
055                    throws PortalException, SystemException {
056    
057                    GroupPermissionUtil.check(
058                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
059    
060                    return teamLocalService.getGroupTeams(groupId);
061    
062            }
063    
064            public Team getTeam(long teamId)
065                    throws PortalException, SystemException {
066    
067                    TeamPermissionUtil.check(
068                            getPermissionChecker(), teamId, ActionKeys.VIEW);
069    
070                    return teamLocalService.getTeam(teamId);
071            }
072    
073            public Team getTeam(long groupId, String name)
074                    throws PortalException, SystemException {
075    
076                    Team team = teamLocalService.getTeam(groupId, name);
077    
078                    TeamPermissionUtil.check(getPermissionChecker(), team, ActionKeys.VIEW);
079    
080                    return team;
081            }
082    
083            public List<Team> getUserTeams(long userId)
084                    throws PortalException, SystemException {
085    
086                    UserPermissionUtil.check(
087                            getPermissionChecker(), userId, ActionKeys.UPDATE);
088    
089                    return teamLocalService.getUserTeams(userId);
090            }
091    
092            public List<Team> getUserTeams(long userId, long groupId)
093                    throws PortalException, SystemException {
094    
095                    GroupPermissionUtil.check(
096                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
097    
098                    return teamLocalService.getUserTeams(userId, groupId);
099            }
100    
101            public boolean hasUserTeam(long userId, long teamId)
102                    throws PortalException, SystemException {
103    
104                    PermissionChecker permissionChecker = getPermissionChecker();
105    
106                    Team team = teamPersistence.findByPrimaryKey(teamId);
107    
108                    if (!GroupPermissionUtil.contains(
109                                    permissionChecker, team.getGroupId(),
110                                    ActionKeys.MANAGE_TEAMS) &&
111                            !UserPermissionUtil.contains(
112                                    permissionChecker, userId, ActionKeys.UPDATE)) {
113    
114                            throw new PrincipalException();
115                    }
116    
117                    return userPersistence.containsTeam(userId, teamId);
118            }
119    
120            public Team updateTeam(long teamId, String name, String description)
121                    throws PortalException, SystemException {
122    
123                    TeamPermissionUtil.check(
124                            getPermissionChecker(), teamId, ActionKeys.UPDATE);
125    
126                    return teamLocalService.updateTeam(teamId, name, description);
127            }
128    
129    }