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