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