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