001    /**
002     * Copyright (c) 2000-2012 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.documentlibrary.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.Folder;
020    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
029    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class DLFolderPermission {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, DLFolder dlFolder,
039                            String actionId)
040                    throws PortalException, SystemException {
041    
042                    if (!contains(permissionChecker, dlFolder, actionId)) {
043                            throw new PrincipalException();
044                    }
045            }
046    
047            public static void check(
048                            PermissionChecker permissionChecker, Folder folder, String actionId)
049                    throws PortalException, SystemException {
050    
051                    if (!folder.containsPermission(permissionChecker, actionId)) {
052                            throw new PrincipalException();
053                    }
054            }
055    
056            public static void check(
057                            PermissionChecker permissionChecker, long groupId, long folderId,
058                            String actionId)
059                    throws PortalException, SystemException {
060    
061                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
062                            throw new PrincipalException();
063                    }
064            }
065    
066            public static boolean contains(
067                            PermissionChecker permissionChecker, DLFolder dlFolder,
068                            String actionId)
069                    throws PortalException, SystemException {
070    
071                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
072                            actionId = ActionKeys.ADD_SUBFOLDER;
073                    }
074    
075                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
076                            permissionChecker, dlFolder.getGroupId(), DLFolder.class.getName(),
077                            dlFolder.getFolderId(), PortletKeys.DOCUMENT_LIBRARY, actionId);
078    
079                    if (hasPermission != null) {
080                            return hasPermission.booleanValue();
081                    }
082    
083                    long folderId = dlFolder.getFolderId();
084    
085                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
086                            long originalFolderId = folderId;
087    
088                            try {
089                                    while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
090                                            dlFolder = DLFolderLocalServiceUtil.getFolder(folderId);
091    
092                                            if (!permissionChecker.hasOwnerPermission(
093                                                            dlFolder.getCompanyId(), DLFolder.class.getName(),
094                                                            folderId, dlFolder.getUserId(), ActionKeys.VIEW) &&
095                                                    !permissionChecker.hasPermission(
096                                                            dlFolder.getGroupId(), DLFolder.class.getName(),
097                                                            folderId, ActionKeys.VIEW)) {
098    
099                                                    return false;
100                                            }
101    
102                                            folderId = dlFolder.getParentFolderId();
103                                    }
104                            }
105                            catch (NoSuchFolderException nsfe) {
106                                    if (!dlFolder.isInTrash()) {
107                                            throw nsfe;
108                                    }
109                            }
110    
111                            if (actionId.equals(ActionKeys.VIEW)) {
112                                    return true;
113                            }
114    
115                            folderId = originalFolderId;
116                    }
117    
118                    try {
119                            while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
120                                    dlFolder = DLFolderLocalServiceUtil.getFolder(folderId);
121    
122                                    if (permissionChecker.hasOwnerPermission(
123                                                    dlFolder.getCompanyId(), DLFolder.class.getName(),
124                                                    folderId, dlFolder.getUserId(), actionId) ||
125                                            permissionChecker.hasPermission(
126                                                    dlFolder.getGroupId(), DLFolder.class.getName(),
127                                                    folderId, actionId)) {
128    
129                                            return true;
130                                    }
131    
132                                    folderId = dlFolder.getParentFolderId();
133                            }
134                    }
135                    catch (NoSuchFolderException nsfe) {
136                            if (!dlFolder.isInTrash()) {
137                                    throw nsfe;
138                            }
139                    }
140    
141                    return false;
142            }
143    
144            public static boolean contains(
145                            PermissionChecker permissionChecker, Folder folder, String actionId)
146                    throws PortalException, SystemException {
147    
148                    return folder.containsPermission(permissionChecker, actionId);
149            }
150    
151            public static boolean contains(
152                            PermissionChecker permissionChecker, long groupId, long folderId,
153                            String actionId)
154                    throws PortalException, SystemException {
155    
156                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
157    
158                            // Prevent the propagation of checks for actions that are not
159                            // supported at the application resource level. See LPS-24245.
160    
161                            if (actionId.equals(ActionKeys.ACCESS) ||
162                                    actionId.equals(ActionKeys.ADD_SUBFOLDER) ||
163                                    actionId.equals(ActionKeys.DELETE)) {
164    
165                                    return false;
166                            }
167    
168                            return DLPermission.contains(permissionChecker, groupId, actionId);
169                    }
170                    else {
171                            Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
172    
173                            return folder.containsPermission(permissionChecker, actionId);
174                    }
175            }
176    
177    }