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