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