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.journal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
019    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.security.permission.ResourcePermissionChecker;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.journal.model.JournalFolder;
027    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
028    
029    /**
030     * @author Jorge Ferrer
031     */
032    @OSGiBeanProperties(
033            property = {
034                    "model.class.name=com.liferay.portlet.journal.model.JournalFolder"
035            }
036    )
037    public class JournalPermission implements ResourcePermissionChecker {
038    
039            public static final String RESOURCE_NAME = "com.liferay.portlet.journal";
040    
041            public static void check(
042                            PermissionChecker permissionChecker, long groupId, String actionId)
043                    throws PortalException {
044    
045                    if (!contains(permissionChecker, groupId, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static boolean contains(
051                            PermissionChecker permissionChecker, long classPK, String actionId)
052                    throws PortalException {
053    
054                    Group group = GroupLocalServiceUtil.fetchGroup(classPK);
055    
056                    if (group == null) {
057                            JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(
058                                    classPK);
059    
060                            return JournalFolderPermission.contains(
061                                    permissionChecker, folder, actionId);
062                    }
063    
064                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
065                            permissionChecker, group.getGroupId(), RESOURCE_NAME,
066                            group.getGroupId(), PortletKeys.JOURNAL, actionId);
067    
068                    if (hasPermission != null) {
069                            return hasPermission.booleanValue();
070                    }
071    
072                    return permissionChecker.hasPermission(
073                            classPK, RESOURCE_NAME, group.getGroupId(), actionId);
074            }
075    
076            @Override
077            public Boolean checkResource(
078                            PermissionChecker permissionChecker, long classPK, String actionId)
079                    throws PortalException {
080    
081                    return contains(permissionChecker, classPK, actionId);
082            }
083    
084    }