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.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    }