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.dynamicdatamapping.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.kernel.util.Validator;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
023    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
024    
025    /**
026     * @author Eduardo Lundgren
027     * @author Levente Hud??k
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 groupId,
043                            DDMTemplate template, String portletId, String actionId)
044                    throws PortalException {
045    
046                    if (!contains(
047                                    permissionChecker, groupId, template, portletId, actionId)) {
048    
049                            throw new PrincipalException();
050                    }
051            }
052    
053            public static void check(
054                            PermissionChecker permissionChecker, long groupId, long templateId,
055                            String portletId, String actionId)
056                    throws PortalException {
057    
058                    if (!contains(
059                                    permissionChecker, groupId, templateId, portletId, actionId)) {
060    
061                            throw new PrincipalException();
062                    }
063            }
064    
065            public static void check(
066                            PermissionChecker permissionChecker, long templateId,
067                            String actionId)
068                    throws PortalException {
069    
070                    if (!contains(permissionChecker, templateId, actionId)) {
071                            throw new PrincipalException();
072                    }
073            }
074    
075            public static boolean contains(
076                    PermissionChecker permissionChecker, DDMTemplate template,
077                    String actionId) {
078    
079                    if (permissionChecker.hasOwnerPermission(
080                                    template.getCompanyId(), DDMTemplate.class.getName(),
081                                    template.getTemplateId(), template.getUserId(), actionId)) {
082    
083                            return true;
084                    }
085    
086                    return permissionChecker.hasPermission(
087                            template.getGroupId(), DDMTemplate.class.getName(),
088                            template.getTemplateId(), actionId);
089            }
090    
091            /**
092             * @deprecated As of 7.0.0, replaced by {@link #contains(PermissionChecker,
093             *             DDMTemplate, String)}
094             */
095            @Deprecated
096            public static boolean contains(
097                    PermissionChecker permissionChecker, DDMTemplate template,
098                    String portletId, String actionId) {
099    
100                    return contains(permissionChecker, template, actionId);
101            }
102    
103            public static boolean contains(
104                    PermissionChecker permissionChecker, long groupId, DDMTemplate template,
105                    String portletId, String actionId) {
106    
107                    if (Validator.isNotNull(portletId)) {
108                            Boolean hasPermission = StagingPermissionUtil.hasPermission(
109                                    permissionChecker, groupId, DDMTemplate.class.getName(),
110                                    template.getTemplateId(), portletId, actionId);
111    
112                            if (hasPermission != null) {
113                                    return hasPermission.booleanValue();
114                            }
115                    }
116    
117                    return contains(permissionChecker, template, actionId);
118            }
119    
120            public static boolean contains(
121                            PermissionChecker permissionChecker, long groupId, long templateId,
122                            String portletId, String actionId)
123                    throws PortalException {
124    
125                    DDMTemplate template = DDMTemplateLocalServiceUtil.getTemplate(
126                            templateId);
127    
128                    return contains(
129                            permissionChecker, groupId, template, portletId, actionId);
130            }
131    
132            public static boolean contains(
133                            PermissionChecker permissionChecker, long templateId,
134                            String actionId)
135                    throws PortalException {
136    
137                    DDMTemplate template = DDMTemplateLocalServiceUtil.getTemplate(
138                            templateId);
139    
140                    return contains(permissionChecker, template, actionId);
141            }
142    
143            /**
144             * @deprecated As of 7.0.0, replaced by {@link #contains(PermissionChecker,
145             *             long, String)}
146             */
147            @Deprecated
148            public static boolean contains(
149                            PermissionChecker permissionChecker, long templateId,
150                            String portletId, String actionId)
151                    throws PortalException {
152    
153                    return contains(permissionChecker, templateId, actionId);
154            }
155    
156    }