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.portlet.dynamicdatamapping.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
023    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
024    
025    /**
026     * @author Bruno Basto
027     */
028    public class DDMStructurePermission {
029    
030            public static void check(
031                            PermissionChecker permissionChecker, DDMStructure structure,
032                            String actionId)
033                    throws PortalException {
034    
035                    if (!contains(permissionChecker, structure, actionId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            public static void check(
041                            PermissionChecker permissionChecker, long groupId, long classNameId,
042                            String structureKey, String actionId)
043                    throws PortalException {
044    
045                    if (!contains(
046                                    permissionChecker, groupId, classNameId, structureKey,
047                                    actionId)) {
048    
049                            throw new PrincipalException();
050                    }
051            }
052    
053            public static void check(
054                            PermissionChecker permissionChecker, long structureId,
055                            String actionId)
056                    throws PortalException {
057    
058                    if (!contains(permissionChecker, structureId, actionId)) {
059                            throw new PrincipalException();
060                    }
061            }
062    
063            public static boolean contains(
064                    PermissionChecker permissionChecker, DDMStructure structure,
065                    String actionId) {
066    
067                    return contains(permissionChecker, structure, null, actionId);
068            }
069    
070            public static boolean contains(
071                    PermissionChecker permissionChecker, DDMStructure structure,
072                    String portletId, String actionId) {
073    
074                    if (Validator.isNotNull(portletId)) {
075                            Boolean hasPermission = StagingPermissionUtil.hasPermission(
076                                    permissionChecker, structure.getGroupId(),
077                                    DDMStructure.class.getName(), structure.getStructureId(),
078                                    portletId, actionId);
079    
080                            if (hasPermission != null) {
081                                    return hasPermission.booleanValue();
082                            }
083                    }
084    
085                    if (permissionChecker.hasOwnerPermission(
086                                    structure.getCompanyId(), DDMStructure.class.getName(),
087                                    structure.getStructureId(), structure.getUserId(), actionId)) {
088    
089                            return true;
090                    }
091    
092                    return permissionChecker.hasPermission(
093                            structure.getGroupId(), DDMStructure.class.getName(),
094                            structure.getStructureId(), actionId);
095            }
096    
097            public static boolean contains(
098                            PermissionChecker permissionChecker, long groupId, long classNameId,
099                            String structureKey, String actionId)
100                    throws PortalException {
101    
102                    DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
103                            groupId, classNameId, structureKey, true);
104    
105                    return contains(permissionChecker, structure, actionId);
106            }
107    
108            public static boolean contains(
109                            PermissionChecker permissionChecker, long structureId,
110                            String actionId)
111                    throws PortalException {
112    
113                    return contains(permissionChecker, structureId, null, actionId);
114            }
115    
116            public static boolean contains(
117                            PermissionChecker permissionChecker, long structureId,
118                            String portletId, String actionId)
119                    throws PortalException {
120    
121                    DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
122                            structureId);
123    
124                    return contains(permissionChecker, structure, portletId, actionId);
125            }
126    
127    }