001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.util.PortletKeys;
023    
024    /**
025     * @author Bruno Basto
026     */
027    public class DDMPermission {
028    
029            public static final String RESOURCE_NAME =
030                    "com.liferay.portlet.dynamicdatamapping";
031    
032            public static void check(
033                            PermissionChecker permissionChecker, long groupId, String name,
034                            String actionId)
035                    throws PortalException {
036    
037                    if (!contains(permissionChecker, groupId, name, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static boolean contains(
043                    PermissionChecker permissionChecker, long groupId, String name,
044                    String actionId) {
045    
046                    Boolean hasPermission = null;
047    
048                    if (actionId.equals(ActionKeys.ADD_PORTLET_DISPLAY_TEMPLATE)) {
049                            hasPermission = StagingPermissionUtil.hasPermission(
050                                    permissionChecker, groupId, RESOURCE_NAME, groupId,
051                                    PortletKeys.PORTLET_DISPLAY_TEMPLATES, actionId);
052                    }
053                    else {
054                            hasPermission = StagingPermissionUtil.hasPermission(
055                                    permissionChecker, groupId, RESOURCE_NAME, groupId, name,
056                                    actionId);
057                    }
058    
059                    if (hasPermission != null) {
060                            return hasPermission.booleanValue();
061                    }
062    
063                    return permissionChecker.hasPermission(
064                            groupId, name, groupId, actionId);
065            }
066    
067    }