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