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