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.kernel.workflow.permission.WorkflowPermissionUtil;
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.DLFileEntry;
027    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
028    import com.liferay.portlet.documentlibrary.model.DLFolder;
029    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
030    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
031    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
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 fileEntry,
042                            String actionId)
043                    throws PortalException, SystemException {
044    
045                    if (!contains(permissionChecker, fileEntry, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static void check(
051                            PermissionChecker permissionChecker, long groupId, long folderId,
052                            String name, String actionId)
053                    throws PortalException, SystemException {
054    
055                    if (!contains(permissionChecker, groupId, folderId, name, 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 fileEntry,
072                            String actionId)
073                    throws PortalException, SystemException {
074    
075                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
076                            permissionChecker, fileEntry.getGroupId(),
077                            DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
078                            PortletKeys.DOCUMENT_LIBRARY, actionId);
079    
080                    if (hasPermission != null) {
081                            return hasPermission.booleanValue();
082                    }
083    
084                    DLFileVersion latestFileVersion =
085                            DLFileVersionLocalServiceUtil.getLatestFileVersion(
086                                    fileEntry.getGroupId(), fileEntry.getFolderId(),
087                                    fileEntry.getName());
088    
089                    if (latestFileVersion.isPending()) {
090                            hasPermission = WorkflowPermissionUtil.hasPermission(
091                                    permissionChecker, fileEntry.getGroupId(),
092                                    DLFileEntry.class.getName(), fileEntry.getFileEntryId(),
093                                    actionId);
094    
095                            if (hasPermission != null) {
096                                    return hasPermission.booleanValue();
097                            }
098                    }
099    
100                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
101                            if (fileEntry.getFolderId() !=
102                                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
103    
104                                    DLFolder folder = DLFolderLocalServiceUtil.getFolder(
105                                            fileEntry.getFolderId());
106    
107                                    if (!DLFolderPermission.contains(
108                                                    permissionChecker, folder, ActionKeys.ACCESS) &&
109                                            !DLFolderPermission.contains(
110                                                    permissionChecker, folder, ActionKeys.VIEW)) {
111    
112                                            return false;
113                                    }
114                            }
115                    }
116    
117                    if (permissionChecker.hasOwnerPermission(
118                                    fileEntry.getCompanyId(), DLFileEntry.class.getName(),
119                                    fileEntry.getFileEntryId(), fileEntry.getUserId(), actionId)) {
120    
121                            return true;
122                    }
123    
124                    return permissionChecker.hasPermission(
125                            fileEntry.getGroupId(), DLFileEntry.class.getName(),
126                            fileEntry.getFileEntryId(), actionId);
127            }
128    
129            public static boolean contains(
130                            PermissionChecker permissionChecker, long groupId, long folderId,
131                            String name, String actionId)
132                    throws PortalException, SystemException {
133    
134                    DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
135                            groupId, folderId, name);
136    
137                    return contains(permissionChecker, fileEntry, actionId);
138            }
139    
140            public static boolean contains(
141                            PermissionChecker permissionChecker, long fileEntryId,
142                            String actionId)
143                    throws PortalException, SystemException {
144    
145                    DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
146                            fileEntryId);
147    
148                    return contains(permissionChecker, fileEntry, actionId);
149            }
150    
151    }