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