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