001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
036                            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            public void deleteTeam(long teamId)
047                    throws PortalException, SystemException {
048    
049                    TeamPermissionUtil.check(
050                            getPermissionChecker(), teamId, ActionKeys.DELETE);
051    
052                    teamLocalService.deleteTeam(teamId);
053            }
054    
055            public List<Team> getGroupTeams(long groupId)
056                    throws PortalException, SystemException {
057    
058                    GroupPermissionUtil.check(
059                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
060    
061                    return teamLocalService.getGroupTeams(groupId);
062    
063            }
064    
065            public Team getTeam(long teamId)
066                    throws PortalException, SystemException {
067    
068                    TeamPermissionUtil.check(
069                            getPermissionChecker(), teamId, ActionKeys.VIEW);
070    
071                    return teamLocalService.getTeam(teamId);
072            }
073    
074            public Team getTeam(long groupId, String name)
075                    throws PortalException, SystemException {
076    
077                    Team team = teamLocalService.getTeam(groupId, name);
078    
079                    TeamPermissionUtil.check(
080                            getPermissionChecker(), team, ActionKeys.VIEW);
081    
082                    return team;
083            }
084    
085            public List<Team> getUserTeams(long userId)
086                    throws PortalException, SystemException {
087    
088                    UserPermissionUtil.check(
089                            getPermissionChecker(), userId, ActionKeys.UPDATE);
090    
091                    return teamLocalService.getUserTeams(userId);
092            }
093    
094            public List<Team> getUserTeams(long userId, long groupId)
095                    throws PortalException, SystemException {
096    
097                    GroupPermissionUtil.check(
098                            getPermissionChecker(), groupId, ActionKeys.MANAGE_TEAMS);
099    
100                    return teamLocalService.getUserTeams(userId, groupId);
101            }
102    
103            public boolean hasUserTeam(long userId, long teamId)
104                    throws PortalException, SystemException {
105    
106                    PermissionChecker permissionChecker = getPermissionChecker();
107    
108                    Team team = teamPersistence.findByPrimaryKey(teamId);
109    
110                    if (!GroupPermissionUtil.contains(
111                                    permissionChecker, team.getGroupId(),
112                                    ActionKeys.MANAGE_TEAMS) &&
113                            !UserPermissionUtil.contains(
114                                    permissionChecker, userId, ActionKeys.UPDATE)) {
115    
116                            throw new PrincipalException();
117                    }
118    
119                    return userPersistence.containsTeam(userId, teamId);
120            }
121    
122            public Team updateTeam(long teamId, String name, String description)
123                    throws PortalException, SystemException {
124    
125                    TeamPermissionUtil.check(
126                            getPermissionChecker(), teamId, ActionKeys.UPDATE);
127    
128                    return teamLocalService.updateTeam(teamId, name, description);
129            }
130    
131    }