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.permission;
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.TeamLocalServiceUtil;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class TeamPermissionImpl implements TeamPermission {
029    
030            @Override
031            public void check(
032                            PermissionChecker permissionChecker, long teamId, String actionId)
033                    throws PortalException, SystemException {
034    
035                    if (!contains(permissionChecker, teamId, actionId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            @Override
041            public void check(
042                            PermissionChecker permissionChecker, Team team, String actionId)
043                    throws PortalException, SystemException {
044    
045                    if (!contains(permissionChecker, team, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            @Override
051            public boolean contains(
052                            PermissionChecker permissionChecker, long teamId, String actionId)
053                    throws PortalException, SystemException {
054    
055                    Team team = TeamLocalServiceUtil.getTeam(teamId);
056    
057                    return contains(permissionChecker, team, actionId);
058            }
059    
060            @Override
061            public boolean contains(
062                            PermissionChecker permissionChecker, Team team, String actionId)
063                    throws PortalException, SystemException {
064    
065                    if (GroupPermissionUtil.contains(
066                                    permissionChecker, team.getGroupId(),
067                                    ActionKeys.MANAGE_TEAMS)) {
068    
069                            return true;
070                    }
071    
072                    if (permissionChecker.hasOwnerPermission(
073                                    team.getCompanyId(), Team.class.getName(), team.getTeamId(),
074                                    team.getUserId(), actionId)) {
075    
076                            return true;
077                    }
078    
079                    return permissionChecker.hasPermission(
080                            team.getGroupId(), Team.class.getName(), team.getTeamId(),
081                            actionId);
082            }
083    
084    }