001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
026    
027    /**
028     * @author Marcellus Tavares
029     */
030    public class DDLRecordSetPermission {
031    
032            public static void check(
033                            PermissionChecker permissionChecker, DDLRecordSet recordSet,
034                            String actionId)
035                    throws PortalException {
036    
037                    if (!contains(permissionChecker, recordSet, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, long recordSetId,
044                            String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, recordSetId, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static void check(
053                            PermissionChecker permissionChecker, long groupId,
054                            String recordSetKey, String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (!contains(permissionChecker, groupId, recordSetKey, actionId)) {
058                            throw new PrincipalException();
059                    }
060            }
061    
062            public static boolean contains(
063                    PermissionChecker permissionChecker, DDLRecordSet recordSet,
064                    String actionId) {
065    
066                    if (!actionId.equals(ActionKeys.ADD_RECORD)) {
067                            Boolean hasPermission = StagingPermissionUtil.hasPermission(
068                                    permissionChecker, recordSet.getGroupId(),
069                                    DDLRecordSet.class.getName(), recordSet.getRecordSetId(),
070                                    PortletKeys.DYNAMIC_DATA_LISTS, actionId);
071    
072                            if (hasPermission != null) {
073                                    return hasPermission.booleanValue();
074                            }
075                    }
076    
077                    if (permissionChecker.hasOwnerPermission(
078                                    recordSet.getCompanyId(), DDLRecordSet.class.getName(),
079                                    recordSet.getRecordSetId(), recordSet.getUserId(), actionId)) {
080    
081                            return true;
082                    }
083    
084                    return permissionChecker.hasPermission(
085                            recordSet.getGroupId(), DDLRecordSet.class.getName(),
086                            recordSet.getRecordSetId(), actionId);
087            }
088    
089            public static boolean contains(
090                            PermissionChecker permissionChecker, long recordSetId,
091                            String actionId)
092                    throws PortalException, SystemException {
093    
094                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
095                            recordSetId);
096    
097                    return contains(permissionChecker, recordSet, actionId);
098            }
099    
100            public static boolean contains(
101                            PermissionChecker permissionChecker, long groupId,
102                            String recordSetKey, String actionId)
103                    throws PortalException, SystemException {
104    
105                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
106                            groupId, recordSetKey);
107    
108                    return contains(permissionChecker, recordSet, actionId);
109            }
110    
111    }