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.portlet.PortletProvider;
019    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
020    import com.liferay.portal.kernel.repository.model.FileShortcut;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
024    import com.liferay.portlet.documentlibrary.model.DLFileShortcutConstants;
025    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
026    import com.liferay.portlet.exportimport.staging.permission.StagingPermissionUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class DLFileShortcutPermission {
032    
033            public static void check(
034                            PermissionChecker permissionChecker, DLFileShortcut dlFileShortcut,
035                            String actionId)
036                    throws PortalException {
037    
038                    if (!contains(permissionChecker, dlFileShortcut, actionId)) {
039                            throw new PrincipalException.MustHavePermission(
040                                    permissionChecker, DLFileShortcut.class.getName(),
041                                    dlFileShortcut.getFileShortcutId(), actionId);
042                    }
043            }
044    
045            public static void check(
046                            PermissionChecker permissionChecker, FileShortcut fileShortcut,
047                            String actionId)
048                    throws PortalException {
049    
050                    if (!contains(permissionChecker, fileShortcut, actionId)) {
051                            throw new PrincipalException.MustHavePermission(
052                                    permissionChecker, FileShortcut.class.getName(),
053                                    fileShortcut.getFileShortcutId(), actionId);
054                    }
055            }
056    
057            public static void check(
058                            PermissionChecker permissionChecker, long fileShortcutId,
059                            String actionId)
060                    throws PortalException {
061    
062                    if (!contains(permissionChecker, fileShortcutId, actionId)) {
063                            throw new PrincipalException.MustHavePermission(
064                                    permissionChecker, DLFileShortcut.class.getName(),
065                                    fileShortcutId, actionId);
066                    }
067            }
068    
069            public static boolean contains(
070                    PermissionChecker permissionChecker, DLFileShortcut dlFileShortcut,
071                    String actionId) {
072    
073                    String portletId = PortletProviderUtil.getPortletId(
074                            FileShortcut.class.getName(), PortletProvider.Action.EDIT);
075    
076                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
077                            permissionChecker, dlFileShortcut.getGroupId(),
078                            DLFileShortcutConstants.getClassName(),
079                            dlFileShortcut.getFileShortcutId(), portletId, actionId);
080    
081                    if (hasPermission != null) {
082                            return hasPermission.booleanValue();
083                    }
084    
085                    if (permissionChecker.hasOwnerPermission(
086                                    dlFileShortcut.getCompanyId(),
087                                    DLFileShortcutConstants.getClassName(),
088                                    dlFileShortcut.getFileShortcutId(), dlFileShortcut.getUserId(),
089                                    actionId)) {
090    
091                            return true;
092                    }
093    
094                    return permissionChecker.hasPermission(
095                            dlFileShortcut.getGroupId(), DLFileShortcutConstants.getClassName(),
096                            dlFileShortcut.getFileShortcutId(), actionId);
097            }
098    
099            public static boolean contains(
100                            PermissionChecker permissionChecker, FileShortcut fileShortcut,
101                            String actionId)
102                    throws PortalException {
103    
104                    return fileShortcut.containsPermission(permissionChecker, actionId);
105            }
106    
107            public static boolean contains(
108                            PermissionChecker permissionChecker, long fileShortcutId,
109                            String actionId)
110                    throws PortalException {
111    
112                    DLFileShortcut dlFileShortcut =
113                            DLFileShortcutLocalServiceUtil.getFileShortcut(fileShortcutId);
114    
115                    return contains(permissionChecker, dlFileShortcut, actionId);
116            }
117    
118    }