001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.workflow.permission.WorkflowPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portlet.journal.model.JournalArticle;
023    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Raymond Augé
028     */
029    public class JournalArticlePermission {
030    
031            public static void check(
032                            PermissionChecker permissionChecker, JournalArticle article,
033                            String actionId)
034                    throws PortalException {
035    
036                    if (!contains(permissionChecker, article, actionId)) {
037                            throw new PrincipalException();
038                    }
039            }
040    
041            public static void check(
042                            PermissionChecker permissionChecker, long resourcePrimKey,
043                            String actionId)
044                    throws PortalException, SystemException {
045    
046                    if (!contains(permissionChecker, resourcePrimKey, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static void check(
052                            PermissionChecker permissionChecker, long groupId, String articleId,
053                            String actionId)
054                    throws PortalException, SystemException {
055    
056                    if (!contains(permissionChecker, groupId, articleId, actionId)) {
057                            throw new PrincipalException();
058                    }
059            }
060    
061            public static boolean contains(
062                    PermissionChecker permissionChecker, JournalArticle article,
063                    String actionId) {
064    
065                    if (article.isPending()) {
066                            Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
067                                    permissionChecker, article.getGroupId(),
068                                    JournalArticle.class.getName(), article.getResourcePrimKey(),
069                                    actionId);
070    
071                            if (hasPermission != null) {
072                                    return hasPermission.booleanValue();
073                            }
074                    }
075    
076                    if (permissionChecker.hasOwnerPermission(
077                                    article.getCompanyId(), JournalArticle.class.getName(),
078                                    article.getResourcePrimKey(), article.getUserId(), actionId)) {
079    
080                            return true;
081                    }
082    
083                    return permissionChecker.hasPermission(
084                            article.getGroupId(), JournalArticle.class.getName(),
085                            article.getResourcePrimKey(), actionId);
086            }
087    
088            public static boolean contains(
089                            PermissionChecker permissionChecker, long resourcePrimKey,
090                            String actionId)
091                    throws PortalException, SystemException {
092    
093                    JournalArticle article =
094                            JournalArticleLocalServiceUtil.getLatestArticle(resourcePrimKey);
095    
096                    return contains(permissionChecker, article, actionId);
097            }
098    
099            public static boolean contains(
100                            PermissionChecker permissionChecker, long groupId, String articleId,
101                            String actionId)
102                    throws PortalException, SystemException {
103    
104                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
105                            groupId, articleId);
106    
107                    return contains(permissionChecker, article, actionId);
108            }
109    
110    }