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 (actionId.equals(ActionKeys.VIEW)) {
086                            while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
087                                    try {
088                                            dlFolder = DLFolderLocalServiceUtil.getFolder(folderId);
089    
090                                            folderId = dlFolder.getParentFolderId();
091    
092                                            if (!permissionChecker.hasOwnerPermission(
093                                                            dlFolder.getCompanyId(), DLFolder.class.getName(),
094                                                            dlFolder.getFolderId(), dlFolder.getUserId(),
095                                                            actionId) &&
096                                                    !permissionChecker.hasPermission(
097                                                            dlFolder.getGroupId(), DLFolder.class.getName(),
098                                                            dlFolder.getFolderId(), actionId)) {
099    
100                                                    return false;
101                                            }
102    
103                                            if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
104                                                    break;
105                                            }
106                                    }
107                                    catch (NoSuchFolderException nsfe) {
108                                            if (dlFolder.isInTrash()) {
109                                                    break;
110                                            }
111    
112                                            throw nsfe;
113                                    }
114    
115                            }
116    
117                            return true;
118                    }
119                    else {
120                            while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
121                                    dlFolder = DLFolderLocalServiceUtil.getFolder(folderId);
122    
123                                    folderId = dlFolder.getParentFolderId();
124    
125                                    if (permissionChecker.hasOwnerPermission(
126                                                    dlFolder.getCompanyId(), DLFolder.class.getName(),
127                                                    dlFolder.getFolderId(), dlFolder.getUserId(),
128                                                    actionId)) {
129    
130                                            return true;
131                                    }
132    
133                                    if (permissionChecker.hasPermission(
134                                                    dlFolder.getGroupId(), DLFolder.class.getName(),
135                                                    dlFolder.getFolderId(), actionId)) {
136    
137                                            return true;
138                                    }
139                            }
140    
141                            return false;
142                    }
143            }
144    
145            public static boolean contains(
146                            PermissionChecker permissionChecker, Folder folder, String actionId)
147                    throws PortalException, SystemException {
148    
149                    return folder.containsPermission(permissionChecker, actionId);
150            }
151    
152            public static boolean contains(
153                            PermissionChecker permissionChecker, long groupId, long folderId,
154                            String actionId)
155                    throws PortalException, SystemException {
156    
157                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
158    
159                            // Prevent the propagation of checks for actions that are not
160                            // supported at the application resource level. See LPS-24245.
161    
162                            if (actionId.equals(ActionKeys.ACCESS) ||
163                                    actionId.equals(ActionKeys.ADD_SUBFOLDER) ||
164                                    actionId.equals(ActionKeys.DELETE)) {
165    
166                                    return false;
167                            }
168    
169                            return DLPermission.contains(permissionChecker, groupId, actionId);
170                    }
171                    else {
172                            Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
173    
174                            return folder.containsPermission(permissionChecker, actionId);
175                    }
176            }
177    
178    }