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