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