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