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, String portletId,
034                    long classPK, String actionId) {
035    
036                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
037                            permissionChecker, classPK, name, classPK, portletId, actionId);
038    
039                    if (hasPermission != null) {
040                            return hasPermission.booleanValue();
041                    }
042    
043                    Group group = GroupLocalServiceUtil.fetchGroup(classPK);
044    
045                    if ((group != null) && group.isStagingGroup()) {
046                            classPK = group.getLiveGroupId();
047                    }
048    
049                    try {
050                            int count =
051                                    ResourcePermissionLocalServiceUtil.getResourcePermissionsCount(
052                                            permissionChecker.getCompanyId(), name,
053                                            ResourceConstants.SCOPE_INDIVIDUAL,
054                                            String.valueOf(classPK));
055    
056                            if (count == 0) {
057                                    ResourceLocalServiceUtil.addResources(
058                                            permissionChecker.getCompanyId(), classPK, 0, name, classPK,
059                                            false, true, true);
060                            }
061                    }
062                    catch (Exception e) {
063                            if (_log.isWarnEnabled()) {
064                                    _log.warn(e, e);
065                            }
066                    }
067    
068                    return permissionChecker.hasPermission(
069                            classPK, name, classPK, actionId);
070            }
071    
072            private static final Log _log = LogFactoryUtil.getLog(
073                    BaseResourcePermissionChecker.class);
074    
075    }