001    /**
002     * Copyright (c) 2000-present 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.repository.model.FileEntry;
019    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
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.BaseModelPermissionChecker;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.security.permission.ResourcePermissionChecker;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
032    import com.liferay.portlet.documentlibrary.model.DLFolder;
033    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
034    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
035    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Charles May
040     */
041    @OSGiBeanProperties(
042            property = {
043                    "model.class.name=com.liferay.portlet.documentlibrary.model.DLFileEntry"
044            }
045    )
046    public class DLFileEntryPermission
047            implements BaseModelPermissionChecker, ResourcePermissionChecker {
048    
049            public static void check(
050                            PermissionChecker permissionChecker, DLFileEntry dlFileEntry,
051                            String actionId)
052                    throws PortalException {
053    
054                    if (!contains(permissionChecker, dlFileEntry, actionId)) {
055                            throw new PrincipalException();
056                    }
057            }
058    
059            public static void check(
060                            PermissionChecker permissionChecker, FileEntry fileEntry,
061                            String actionId)
062                    throws PortalException {
063    
064                    if (!fileEntry.containsPermission(permissionChecker, actionId)) {
065                            throw new PrincipalException();
066                    }
067            }
068    
069            public static void check(
070                            PermissionChecker permissionChecker, long fileEntryId,
071                            String actionId)
072                    throws PortalException {
073    
074                    if (!contains(permissionChecker, fileEntryId, actionId)) {
075                            throw new PrincipalException();
076                    }
077            }
078    
079            public static boolean contains(
080                            PermissionChecker permissionChecker, DLFileEntry dlFileEntry,
081                            String actionId)
082                    throws PortalException {
083    
084                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
085                            permissionChecker, dlFileEntry.getGroupId(),
086                            DLFileEntry.class.getName(), dlFileEntry.getFileEntryId(),
087                            PortletKeys.DOCUMENT_LIBRARY, actionId);
088    
089                    if (hasPermission != null) {
090                            return hasPermission.booleanValue();
091                    }
092    
093                    DLFileVersion latestDLFileVersion = dlFileEntry.getLatestFileVersion(
094                            true);
095    
096                    if (latestDLFileVersion.isPending()) {
097                            hasPermission = WorkflowPermissionUtil.hasPermission(
098                                    permissionChecker, dlFileEntry.getGroupId(),
099                                    DLFileEntry.class.getName(), dlFileEntry.getFileEntryId(),
100                                    actionId);
101    
102                            if (hasPermission != null) {
103                                    return hasPermission.booleanValue();
104                            }
105                    }
106    
107                    if (actionId.equals(ActionKeys.VIEW) &&
108                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
109    
110                            long dlFolderId = dlFileEntry.getFolderId();
111    
112                            if (dlFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
113                                    if (!DLPermission.contains(
114                                                    permissionChecker, dlFileEntry.getGroupId(),
115                                                    actionId)) {
116    
117                                            return false;
118                                    }
119                            }
120                            else {
121                                    try {
122                                            DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(
123                                                    dlFolderId);
124    
125                                            if (!DLFolderPermission.contains(
126                                                            permissionChecker, dlFolder, ActionKeys.ACCESS) &&
127                                                    !DLFolderPermission.contains(
128                                                            permissionChecker, dlFolder, ActionKeys.VIEW)) {
129    
130                                                    return false;
131                                            }
132                                    }
133                                    catch (NoSuchFolderException nsfe) {
134                                            if (!dlFileEntry.isInTrash()) {
135                                                    throw nsfe;
136                                            }
137                                    }
138                            }
139                    }
140    
141                    if (permissionChecker.hasOwnerPermission(
142                                    dlFileEntry.getCompanyId(), DLFileEntry.class.getName(),
143                                    dlFileEntry.getFileEntryId(), dlFileEntry.getUserId(),
144                                    actionId)) {
145    
146                            return true;
147                    }
148    
149                    return permissionChecker.hasPermission(
150                            dlFileEntry.getGroupId(), DLFileEntry.class.getName(),
151                            dlFileEntry.getFileEntryId(), actionId);
152            }
153    
154            public static boolean contains(
155                            PermissionChecker permissionChecker, FileEntry fileEntry,
156                            String actionId)
157                    throws PortalException {
158    
159                    return fileEntry.containsPermission(permissionChecker, actionId);
160            }
161    
162            public static boolean contains(
163                            PermissionChecker permissionChecker, long fileEntryId,
164                            String actionId)
165                    throws PortalException {
166    
167                    FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId);
168    
169                    return fileEntry.containsPermission(permissionChecker, actionId);
170            }
171    
172            @Override
173            public void checkBaseModel(
174                            PermissionChecker permissionChecker, long groupId, long primaryKey,
175                            String actionId)
176                    throws PortalException {
177    
178                    check(permissionChecker, primaryKey, actionId);
179            }
180    
181            @Override
182            public Boolean checkResource(
183                            PermissionChecker permissionChecker, long classPK, String actionId)
184                    throws PortalException {
185    
186                    return contains(permissionChecker, classPK, actionId);
187            }
188    
189    }