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