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