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