001    /**
002     * Copyright (c) 2000-2010 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.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.documentlibrary.model.DLFolder;
026    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
027    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class DLFolderPermission {
033    
034            public static void check(
035                            PermissionChecker permissionChecker, DLFolder folder,
036                            String actionId)
037                    throws PortalException, SystemException {
038    
039                    if (!contains(permissionChecker, folder, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public static void check(
045                            PermissionChecker permissionChecker, long groupId, long folderId,
046                            String actionId)
047                    throws PortalException, SystemException {
048    
049                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
050                            throw new PrincipalException();
051                    }
052            }
053    
054            public static boolean contains(
055                            PermissionChecker permissionChecker, DLFolder folder,
056                            String actionId)
057                    throws PortalException, SystemException {
058    
059                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
060                            actionId = ActionKeys.ADD_SUBFOLDER;
061                    }
062    
063                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
064                            permissionChecker, folder.getGroupId(), DLFolder.class.getName(),
065                            folder.getFolderId(), PortletKeys.DOCUMENT_LIBRARY, actionId);
066    
067                    if (hasPermission != null) {
068                            return hasPermission.booleanValue();
069                    }
070    
071                    long folderId = folder.getFolderId();
072    
073                    if (actionId.equals(ActionKeys.VIEW)) {
074                            while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
075                                    folder = DLFolderLocalServiceUtil.getFolder(folderId);
076    
077                                    folderId = folder.getParentFolderId();
078    
079                                    if (!permissionChecker.hasOwnerPermission(
080                                                    folder.getCompanyId(), DLFolder.class.getName(),
081                                                    folder.getFolderId(), folder.getUserId(), actionId) &&
082                                            !permissionChecker.hasPermission(
083                                                    folder.getGroupId(), DLFolder.class.getName(),
084                                                    folder.getFolderId(), actionId)) {
085    
086                                            return false;
087                                    }
088    
089                                    if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
090                                            break;
091                                    }
092                            }
093    
094                            return true;
095                    }
096                    else {
097                            while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
098                                    folder = DLFolderLocalServiceUtil.getFolder(folderId);
099    
100                                    folderId = folder.getParentFolderId();
101    
102                                    if (permissionChecker.hasOwnerPermission(
103                                                    folder.getCompanyId(), DLFolder.class.getName(),
104                                                    folder.getFolderId(), folder.getUserId(), actionId)) {
105    
106                                            return true;
107                                    }
108    
109                                    if (permissionChecker.hasPermission(
110                                                    folder.getGroupId(), DLFolder.class.getName(),
111                                                    folder.getFolderId(), actionId)) {
112    
113                                            return true;
114                                    }
115                            }
116    
117                            return false;
118                    }
119            }
120    
121            public static boolean contains(
122                            PermissionChecker permissionChecker, long groupId, long folderId,
123                            String actionId)
124                    throws PortalException, SystemException {
125    
126                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
127                            return DLPermission.contains(permissionChecker, groupId, actionId);
128                    }
129                    else {
130                            DLFolder folder = DLFolderLocalServiceUtil.getFolder(folderId);
131    
132                            return contains(permissionChecker, folder, actionId);
133                    }
134            }
135    
136    }