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.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.util.PortletKeys;
023    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
024    import com.liferay.portlet.documentlibrary.service.DLFileShortcutLocalServiceUtil;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class DLFileShortcutPermission {
030    
031            public static void check(
032                            PermissionChecker permissionChecker, DLFileShortcut dlFileShortcut,
033                            String actionId)
034                    throws PortalException {
035    
036                    if (!contains(permissionChecker, dlFileShortcut, actionId)) {
037                            throw new PrincipalException();
038                    }
039            }
040    
041            public static void check(
042                            PermissionChecker permissionChecker, long fileShortcutId,
043                            String actionId)
044                    throws PortalException, SystemException {
045    
046                    if (!contains(permissionChecker, fileShortcutId, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static boolean contains(
052                    PermissionChecker permissionChecker, DLFileShortcut dlFileShortcut,
053                    String actionId) {
054    
055                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
056                            permissionChecker, dlFileShortcut.getGroupId(),
057                            DLFileShortcut.class.getName(), dlFileShortcut.getFileShortcutId(),
058                            PortletKeys.DOCUMENT_LIBRARY, actionId);
059    
060                    if (hasPermission != null) {
061                            return hasPermission.booleanValue();
062                    }
063    
064                    if (permissionChecker.hasOwnerPermission(
065                                    dlFileShortcut.getCompanyId(), DLFileShortcut.class.getName(),
066                                    dlFileShortcut.getFileShortcutId(), dlFileShortcut.getUserId(),
067                                    actionId)) {
068    
069                            return true;
070                    }
071    
072                    return permissionChecker.hasPermission(
073                            dlFileShortcut.getGroupId(), DLFileShortcut.class.getName(),
074                            dlFileShortcut.getFileShortcutId(), actionId);
075            }
076    
077            public static boolean contains(
078                            PermissionChecker permissionChecker, long fileShortcutId,
079                            String actionId)
080                    throws PortalException, SystemException {
081    
082                    DLFileShortcut dlFileShortcut =
083                            DLFileShortcutLocalServiceUtil.getFileShortcut(fileShortcutId);
084    
085                    return contains(permissionChecker, dlFileShortcut, actionId);
086            }
087    
088    }