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