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.FileEntry;
020    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
021    import com.liferay.portal.kernel.workflow.permission.WorkflowPermissionUtil;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
029    import com.liferay.portlet.documentlibrary.model.DLFolder;
030    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
032    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Charles May
037     */
038    public class DLFileEntryPermission {
039    
040            public static void check(
041                            PermissionChecker permissionChecker, DLFileEntry dlFileEntry,
042                            String actionId)
043                    throws PortalException, SystemException {
044    
045                    if (!contains(permissionChecker, dlFileEntry, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static void check(
051                            PermissionChecker permissionChecker, FileEntry fileEntry,
052                            String actionId)
053                    throws PortalException, SystemException {
054    
055                    if (!fileEntry.containsPermission(permissionChecker, actionId)) {
056                            throw new PrincipalException();
057                    }
058            }
059    
060            public static void check(
061                            PermissionChecker permissionChecker, long fileEntryId,
062                            String actionId)
063                    throws PortalException, SystemException {
064    
065                    if (!contains(permissionChecker, fileEntryId, actionId)) {
066                            throw new PrincipalException();
067                    }
068            }
069    
070            public static boolean contains(
071                            PermissionChecker permissionChecker, DLFileEntry dlFileEntry,
072                            String actionId)
073                    throws PortalException, SystemException {
074    
075                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
076                            permissionChecker, dlFileEntry.getGroupId(),
077                            DLFileEntry.class.getName(), dlFileEntry.getFileEntryId(),
078                            PortletKeys.DOCUMENT_LIBRARY, actionId);
079    
080                    if (hasPermission != null) {
081                            return hasPermission.booleanValue();
082                    }
083    
084                    DLFileVersion latestDLFileVersion = dlFileEntry.getLatestFileVersion(
085                            true);
086    
087                    if (latestDLFileVersion.isPending()) {
088                            hasPermission = WorkflowPermissionUtil.hasPermission(
089                                    permissionChecker, dlFileEntry.getGroupId(),
090                                    DLFileEntry.class.getName(), dlFileEntry.getFileEntryId(),
091                                    actionId);
092    
093                            if (hasPermission != null) {
094                                    return hasPermission.booleanValue();
095                            }
096                    }
097    
098                    if (dlFileEntry.getFolderId() !=
099                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
100    
101                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(
102                                    dlFileEntry.getFolderId());
103    
104                            if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE &&
105                                    !DLFolderPermission.contains(
106                                            permissionChecker, dlFolder, ActionKeys.ACCESS) &&
107                                    !DLFolderPermission.contains(
108                                            permissionChecker, dlFolder, ActionKeys.VIEW)) {
109    
110                                    return false;
111                            }
112    
113                            if (!actionId.equals(ActionKeys.OVERRIDE_CHECKOUT) &&
114                                    DLFolderPermission.contains(
115                                            permissionChecker, dlFolder, actionId)) {
116    
117                                    return true;
118                            }
119                    }
120    
121                    if (permissionChecker.hasOwnerPermission(
122                                    dlFileEntry.getCompanyId(), DLFileEntry.class.getName(),
123                                    dlFileEntry.getFileEntryId(), dlFileEntry.getUserId(),
124                                    actionId)) {
125    
126                            return true;
127                    }
128    
129                    return permissionChecker.hasPermission(
130                            dlFileEntry.getGroupId(), DLFileEntry.class.getName(),
131                            dlFileEntry.getFileEntryId(), actionId);
132            }
133    
134            public static boolean contains(
135                            PermissionChecker permissionChecker, FileEntry fileEntry,
136                            String actionId)
137                    throws PortalException, SystemException {
138    
139                    return fileEntry.containsPermission(permissionChecker, actionId);
140            }
141    
142            public static boolean contains(
143                            PermissionChecker permissionChecker, long fileEntryId,
144                            String actionId)
145                    throws PortalException, SystemException {
146    
147                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
148    
149                    return fileEntry.containsPermission(permissionChecker, actionId);
150            }
151    
152    }