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.dynamicdatalists.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portal.util.PortletKeys;
022    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
023    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
024    
025    /**
026     * @author Marcellus Tavares
027     */
028    public class DDLRecordSetPermission {
029    
030            public static void check(
031                            PermissionChecker permissionChecker, DDLRecordSet recordSet,
032                            String actionId)
033                    throws PortalException {
034    
035                    if (!contains(permissionChecker, recordSet, actionId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            public static void check(
041                            PermissionChecker permissionChecker, long recordSetId,
042                            String actionId)
043                    throws PortalException {
044    
045                    if (!contains(permissionChecker, recordSetId, actionId)) {
046                            throw new PrincipalException();
047                    }
048            }
049    
050            public static void check(
051                            PermissionChecker permissionChecker, long groupId,
052                            String recordSetKey, String actionId)
053                    throws PortalException {
054    
055                    if (!contains(permissionChecker, groupId, recordSetKey, actionId)) {
056                            throw new PrincipalException();
057                    }
058            }
059    
060            public static boolean contains(
061                    PermissionChecker permissionChecker, DDLRecordSet recordSet,
062                    String actionId) {
063    
064                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
065                            permissionChecker, recordSet.getGroupId(),
066                            DDLRecordSet.class.getName(), recordSet.getRecordSetId(),
067                            PortletKeys.DYNAMIC_DATA_LISTS, actionId);
068    
069                    if (hasPermission != null) {
070                            return hasPermission.booleanValue();
071                    }
072    
073                    if (permissionChecker.hasOwnerPermission(
074                                    recordSet.getCompanyId(), DDLRecordSet.class.getName(),
075                                    recordSet.getRecordSetId(), recordSet.getUserId(), actionId)) {
076    
077                            return true;
078                    }
079    
080                    return permissionChecker.hasPermission(
081                            recordSet.getGroupId(), DDLRecordSet.class.getName(),
082                            recordSet.getRecordSetId(), actionId);
083            }
084    
085            public static boolean contains(
086                            PermissionChecker permissionChecker, long recordSetId,
087                            String actionId)
088                    throws PortalException {
089    
090                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
091                            recordSetId);
092    
093                    return contains(permissionChecker, recordSet, actionId);
094            }
095    
096            public static boolean contains(
097                            PermissionChecker permissionChecker, long groupId,
098                            String recordSetKey, String actionId)
099                    throws PortalException {
100    
101                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
102                            groupId, recordSetKey);
103    
104                    return contains(permissionChecker, recordSet, actionId);
105            }
106    
107    }