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.portal.staging.permission;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
020    import com.liferay.portal.kernel.staging.permission.StagingPermission;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    @DoPrivileged
031    public class StagingPermissionImpl implements StagingPermission {
032    
033            @Override
034            public Boolean hasPermission(
035                    PermissionChecker permissionChecker, Group group, String className,
036                    long classPK, String portletId, String actionId) {
037    
038                    try {
039                            return doHasPermission(
040                                    permissionChecker, group, className, classPK, portletId,
041                                    actionId);
042                    }
043                    catch (Exception e) {
044                            _log.error(e, e);
045                    }
046    
047                    return null;
048            }
049    
050            @Override
051            public Boolean hasPermission(
052                    PermissionChecker permissionChecker, long groupId, String className,
053                    long classPK, String portletId, String actionId) {
054    
055                    try {
056                            Group group = GroupLocalServiceUtil.getGroup(groupId);
057    
058                            return doHasPermission(
059                                    permissionChecker, group, className, classPK, portletId,
060                                    actionId);
061                    }
062                    catch (Exception e) {
063                            _log.error(e, e);
064                    }
065    
066                    return null;
067            }
068    
069            protected Boolean doHasPermission(
070                            PermissionChecker permissionChecker, Group group, String className,
071                            long classPK, String portletId, String actionId)
072                    throws Exception {
073    
074                    if (!actionId.equals(ActionKeys.ADD_TO_PAGE) &&
075                            !actionId.equals(ActionKeys.CONFIGURATION) &&
076                            !actionId.equals(ActionKeys.CUSTOMIZE) &&
077                            !actionId.equals(ActionKeys.DELETE) &&
078                            !actionId.equals(ActionKeys.VIEW) &&
079                            group.hasLocalOrRemoteStagingGroup() &&
080                            (Validator.isNull(portletId) || group.isStagedPortlet(portletId))) {
081    
082                            return false;
083                    }
084                    else {
085                            return null;
086                    }
087            }
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    StagingPermissionImpl.class);
091    
092    }