001    /**
002     * Copyright (c) 2000-2012 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.dynamicdatamapping.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
022    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
023    
024    /**
025     * @author Bruno Basto
026     */
027    public class DDMStructurePermission {
028    
029            public static void check(
030                            PermissionChecker permissionChecker, DDMStructure structure,
031                            String actionId)
032                    throws PortalException {
033    
034                    if (!contains(permissionChecker, structure, actionId)) {
035                            throw new PrincipalException();
036                    }
037            }
038    
039            public static void check(
040                            PermissionChecker permissionChecker, long groupId, long classNameId,
041                            String structureKey, String actionId)
042                    throws PortalException, SystemException {
043    
044                    if (!contains(
045                                    permissionChecker, groupId, classNameId, structureKey,
046                                    actionId)) {
047    
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static void check(
053                            PermissionChecker permissionChecker, long structureId,
054                            String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (!contains(permissionChecker, structureId, actionId)) {
058                            throw new PrincipalException();
059                    }
060            }
061    
062            public static boolean contains(
063                    PermissionChecker permissionChecker, DDMStructure structure,
064                    String actionId) {
065    
066                    if (permissionChecker.hasOwnerPermission(
067                                    structure.getCompanyId(), DDMStructure.class.getName(),
068                                    structure.getStructureId(), structure.getUserId(), actionId)) {
069    
070                            return true;
071                    }
072    
073                    return permissionChecker.hasPermission(
074                            structure.getGroupId(), DDMStructure.class.getName(),
075                            structure.getStructureId(), actionId);
076            }
077    
078            public static boolean contains(
079                            PermissionChecker permissionChecker, long groupId, long classNameId,
080                            String structureKey, String actionId)
081                    throws PortalException, SystemException {
082    
083                    DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
084                            groupId, classNameId, structureKey);
085    
086                    return contains(permissionChecker, structure, actionId);
087            }
088    
089            public static boolean contains(
090                            PermissionChecker permissionChecker, long structureId,
091                            String actionId)
092                    throws PortalException, SystemException {
093    
094                    DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
095                            structureId);
096    
097                    return contains(permissionChecker, structure, actionId);
098            }
099    
100    }