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