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