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.mobiledevicerules.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.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.util.PortletKeys;
023    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroup;
024    import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupLocalServiceUtil;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class MDRRuleGroupPermissionImpl implements MDRRuleGroupPermission {
030    
031            @Override
032            public void check(
033                            PermissionChecker permissionChecker, long ruleGroupId,
034                            String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, ruleGroupId, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            @Override
043            public void check(
044                            PermissionChecker permissionChecker, MDRRuleGroup ruleGroup,
045                            String actionId)
046                    throws PortalException {
047    
048                    if (!contains(permissionChecker, ruleGroup, actionId)) {
049                            throw new PrincipalException();
050                    }
051            }
052    
053            @Override
054            public boolean contains(
055                            PermissionChecker permissionChecker, long ruleGroupId,
056                            String actionId)
057                    throws PortalException, SystemException {
058    
059                    MDRRuleGroup ruleGroup = MDRRuleGroupLocalServiceUtil.getMDRRuleGroup(
060                            ruleGroupId);
061    
062                    return contains(permissionChecker, ruleGroup, actionId);
063            }
064    
065            @Override
066            public boolean contains(
067                    PermissionChecker permissionChecker, MDRRuleGroup ruleGroup,
068                    String actionId) {
069    
070                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
071                            permissionChecker, ruleGroup.getGroupId(),
072                            MDRRuleGroup.class.getName(), ruleGroup.getRuleGroupId(),
073                            PortletKeys.MOBILE_DEVICE_SITE_ADMIN, actionId);
074    
075                    if (hasPermission != null) {
076                            return hasPermission.booleanValue();
077                    }
078    
079                    return permissionChecker.hasPermission(
080                            ruleGroup.getGroupId(), MDRRuleGroup.class.getName(),
081                            ruleGroup.getRuleGroupId(), actionId);
082            }
083    
084    }