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.portal.security.permission;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.ResourceConstants;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.service.ResourceLocalServiceUtil;
023    import com.liferay.portal.service.ResourcePermissionLocalServiceUtil;
024    import com.liferay.portlet.exportimport.staging.permission.StagingPermissionUtil;
025    
026    /**
027     * @author Preston Crary
028     */
029    public abstract class BaseResourcePermissionChecker
030            implements ResourcePermissionChecker {
031    
032            public static boolean contains(
033                    PermissionChecker permissionChecker, String name, long classPK,
034                    String actionId) {
035    
036                    Group group = GroupLocalServiceUtil.fetchGroup(classPK);
037    
038                    if ((group != null) && group.isStagingGroup()) {
039                            classPK = group.getLiveGroupId();
040                    }
041    
042                    try {
043                            int count =
044                                    ResourcePermissionLocalServiceUtil.getResourcePermissionsCount(
045                                            permissionChecker.getCompanyId(), name,
046                                            ResourceConstants.SCOPE_INDIVIDUAL,
047                                            String.valueOf(classPK));
048    
049                            if (count == 0) {
050                                    ResourceLocalServiceUtil.addResources(
051                                            permissionChecker.getCompanyId(), classPK, 0, name, classPK,
052                                            false, true, true);
053                            }
054                    }
055                    catch (Exception e) {
056                            if (_log.isWarnEnabled()) {
057                                    _log.warn(e, e);
058                            }
059                    }
060    
061                    return permissionChecker.hasPermission(
062                            classPK, name, classPK, actionId);
063            }
064    
065            public static boolean contains(
066                    PermissionChecker permissionChecker, String name, String portletId,
067                    long classPK, String actionId) {
068    
069                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
070                            permissionChecker, classPK, name, classPK, portletId, actionId);
071    
072                    if (hasPermission != null) {
073                            return hasPermission.booleanValue();
074                    }
075    
076                    return contains(permissionChecker, name, classPK, actionId);
077            }
078    
079            private static final Log _log = LogFactoryUtil.getLog(
080                    BaseResourcePermissionChecker.class);
081    
082    }