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.staging.permission.StagingPermissionUtil;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.util.PortletKeys;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.journal.NoSuchFolderException;
025    import com.liferay.portlet.journal.model.JournalFolder;
026    import com.liferay.portlet.journal.model.JournalFolderConstants;
027    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
028    
029    /**
030     * @author Juan Fern??ndez
031     * @author Zsolt Berentey
032     */
033    public class JournalFolderPermission {
034    
035            public static void check(
036                            PermissionChecker permissionChecker, JournalFolder folder,
037                            String actionId)
038                    throws PortalException {
039    
040                    if (!contains(permissionChecker, folder, actionId)) {
041                            throw new PrincipalException();
042                    }
043            }
044    
045            public static void check(
046                            PermissionChecker permissionChecker, long groupId, long folderId,
047                            String actionId)
048                    throws PortalException {
049    
050                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
051                            throw new PrincipalException();
052                    }
053            }
054    
055            public static boolean contains(
056                            PermissionChecker permissionChecker, JournalFolder folder,
057                            String actionId)
058                    throws PortalException {
059    
060                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
061                            actionId = ActionKeys.ADD_SUBFOLDER;
062                    }
063    
064                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
065                            permissionChecker, folder.getGroupId(),
066                            JournalFolder.class.getName(), folder.getFolderId(),
067                            PortletKeys.JOURNAL, actionId);
068    
069                    if (hasPermission != null) {
070                            return hasPermission.booleanValue();
071                    }
072    
073                    if (actionId.equals(ActionKeys.VIEW) &&
074                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
075    
076                            try {
077                                    long folderId = folder.getFolderId();
078    
079                                    while (folderId !=
080                                                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
081    
082                                            folder = JournalFolderLocalServiceUtil.getFolder(folderId);
083    
084                                            if (!_hasPermission(permissionChecker, folder, actionId)) {
085                                                    return false;
086                                            }
087    
088                                            folderId = folder.getParentFolderId();
089                                    }
090                            }
091                            catch (NoSuchFolderException nsfe) {
092                                    if (!folder.isInTrash()) {
093                                            throw nsfe;
094                                    }
095                            }
096    
097                            return JournalPermission.contains(
098                                    permissionChecker, folder.getGroupId(), actionId);
099                    }
100    
101                    return _hasPermission(permissionChecker, folder, actionId);
102            }
103    
104            public static boolean contains(
105                            PermissionChecker permissionChecker, long groupId, long folderId,
106                            String actionId)
107                    throws PortalException {
108    
109                    if (folderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
110                            return JournalPermission.contains(
111                                    permissionChecker, groupId, actionId);
112                    }
113                    else {
114                            JournalFolder folder =
115                                    JournalFolderLocalServiceUtil.getJournalFolder(folderId);
116    
117                            return contains(permissionChecker, folder, actionId);
118                    }
119            }
120    
121            private static boolean _hasPermission(
122                    PermissionChecker permissionChecker, JournalFolder folder,
123                    String actionId) {
124    
125                    if (permissionChecker.hasOwnerPermission(
126                                    folder.getCompanyId(), JournalFolder.class.getName(),
127                                    folder.getFolderId(), folder.getUserId(), actionId) ||
128                            permissionChecker.hasPermission(
129                                    folder.getGroupId(), JournalFolder.class.getName(),
130                                    folder.getFolderId(), actionId)) {
131    
132                            return true;
133                    }
134    
135                    return false;
136            }
137    
138    }