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    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Raymond Aug??
028     */
029    public class JournalTemplatePermission {
030    
031            public static void check(
032                            PermissionChecker permissionChecker, JournalTemplate 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                            String templateId, String actionId)
044                    throws PortalException, SystemException {
045    
046                    if (!contains(permissionChecker, groupId, templateId, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static boolean contains(
052                    PermissionChecker permissionChecker, JournalTemplate template,
053                    String actionId) {
054    
055                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
056                            permissionChecker, template.getGroupId(),
057                            JournalTemplate.class.getName(), template.getId(),
058                            PortletKeys.JOURNAL, actionId);
059    
060                    if (hasPermission != null) {
061                            return hasPermission.booleanValue();
062                    }
063    
064                    if (permissionChecker.hasOwnerPermission(
065                                    template.getCompanyId(), JournalTemplate.class.getName(),
066                                    template.getId(), template.getUserId(), actionId)) {
067    
068                            return true;
069                    }
070    
071                    return permissionChecker.hasPermission(
072                            template.getGroupId(), JournalTemplate.class.getName(),
073                            template.getId(), actionId);
074            }
075    
076            public static boolean contains(
077                            PermissionChecker permissionChecker, long groupId,
078                            String templateId, String actionId)
079                    throws PortalException, SystemException {
080    
081                    @SuppressWarnings("deprecation")
082                    JournalTemplate template =
083                            com.liferay.portlet.journal.service.
084                                    JournalTemplateLocalServiceUtil.getTemplate(
085                                            groupId, templateId, true);
086    
087                    return contains(permissionChecker, template, actionId);
088            }
089    
090    }