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.kernel.workflow.permission.WorkflowPermissionUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.journal.model.JournalArticle;
025    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Raymond Aug??
030     */
031    public class JournalArticlePermission {
032    
033            public static void check(
034                            PermissionChecker permissionChecker, JournalArticle article,
035                            String actionId)
036                    throws PortalException {
037    
038                    if (!contains(permissionChecker, article, actionId)) {
039                            throw new PrincipalException();
040                    }
041            }
042    
043            public static void check(
044                            PermissionChecker permissionChecker, long resourcePrimKey,
045                            String actionId)
046                    throws PortalException, SystemException {
047    
048                    if (!contains(permissionChecker, resourcePrimKey, actionId)) {
049                            throw new PrincipalException();
050                    }
051            }
052    
053            public static void check(
054                            PermissionChecker permissionChecker, long groupId, String articleId,
055                            double version, String actionId)
056                    throws PortalException, SystemException {
057    
058                    if (!contains(
059                                    permissionChecker, groupId, articleId, version, actionId)) {
060    
061                            throw new PrincipalException();
062                    }
063            }
064    
065            public static void check(
066                            PermissionChecker permissionChecker, long groupId, String articleId,
067                            int status, String actionId)
068                    throws PortalException, SystemException {
069    
070                    if (!contains(
071                                    permissionChecker, groupId, articleId, status, actionId)) {
072    
073                            throw new PrincipalException();
074                    }
075            }
076    
077            public static void check(
078                            PermissionChecker permissionChecker, long groupId, String articleId,
079                            String actionId)
080                    throws PortalException, SystemException {
081    
082                    if (!contains(permissionChecker, groupId, articleId, actionId)) {
083                            throw new PrincipalException();
084                    }
085            }
086    
087            public static boolean contains(
088                    PermissionChecker permissionChecker, JournalArticle article,
089                    String actionId) {
090    
091                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
092                            permissionChecker, article.getGroupId(),
093                            JournalArticle.class.getName(), article.getResourcePrimKey(),
094                            PortletKeys.JOURNAL, actionId);
095    
096                    if (hasPermission != null) {
097                            return hasPermission.booleanValue();
098                    }
099    
100                    if (article.isPending()) {
101                            hasPermission = WorkflowPermissionUtil.hasPermission(
102                                    permissionChecker, article.getGroupId(),
103                                    JournalArticle.class.getName(), article.getResourcePrimKey(),
104                                    actionId);
105    
106                            if (hasPermission != null) {
107                                    return hasPermission.booleanValue();
108                            }
109                    }
110    
111                    if (permissionChecker.hasOwnerPermission(
112                                    article.getCompanyId(), JournalArticle.class.getName(),
113                                    article.getResourcePrimKey(), article.getUserId(), actionId)) {
114    
115                            return true;
116                    }
117    
118                    return permissionChecker.hasPermission(
119                            article.getGroupId(), JournalArticle.class.getName(),
120                            article.getResourcePrimKey(), actionId);
121            }
122    
123            public static boolean contains(
124                            PermissionChecker permissionChecker, long resourcePrimKey,
125                            String actionId)
126                    throws PortalException, SystemException {
127    
128                    JournalArticle article =
129                            JournalArticleLocalServiceUtil.getLatestArticle(resourcePrimKey);
130    
131                    return contains(permissionChecker, article, actionId);
132            }
133    
134            public static boolean contains(
135                            PermissionChecker permissionChecker, long groupId, String articleId,
136                            double version, String actionId)
137                    throws PortalException, SystemException {
138    
139                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
140                            groupId, articleId, version);
141    
142                    return contains(permissionChecker, article, actionId);
143            }
144    
145            public static boolean contains(
146                            PermissionChecker permissionChecker, long groupId, String articleId,
147                            int status, String actionId)
148                    throws PortalException, SystemException {
149    
150                    JournalArticle article =
151                            JournalArticleLocalServiceUtil.getLatestArticle(
152                                    groupId, articleId, status);
153    
154                    return contains(permissionChecker, article, actionId);
155            }
156    
157            public static boolean contains(
158                            PermissionChecker permissionChecker, long groupId, String articleId,
159                            String actionId)
160                    throws PortalException, SystemException {
161    
162                    JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
163                            groupId, articleId);
164    
165                    return contains(permissionChecker, article, actionId);
166            }
167    
168    }