1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.service.permission;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.security.permission.PermissionChecker;
21  import com.liferay.portlet.journal.model.JournalArticle;
22  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
23  
24  /**
25   * <a href="JournalArticlePermission.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   * @author Raymond Augé
29   */
30  public class JournalArticlePermission {
31  
32      public static void check(
33              PermissionChecker permissionChecker, JournalArticle article,
34              String actionId)
35          throws PortalException {
36  
37          if (!contains(permissionChecker, article, actionId)) {
38              throw new PrincipalException();
39          }
40      }
41  
42      public static void check(
43              PermissionChecker permissionChecker, long resourcePrimKey,
44              String actionId)
45          throws PortalException, SystemException {
46  
47          if (!contains(permissionChecker, resourcePrimKey, actionId)) {
48              throw new PrincipalException();
49          }
50      }
51  
52      public static void check(
53              PermissionChecker permissionChecker, long groupId, String articleId,
54              String actionId)
55          throws PortalException, SystemException {
56  
57          if (!contains(permissionChecker, groupId, articleId, actionId)) {
58              throw new PrincipalException();
59          }
60      }
61  
62      public static boolean contains(
63          PermissionChecker permissionChecker, JournalArticle article,
64          String actionId) {
65  
66          if (permissionChecker.hasOwnerPermission(
67                  article.getCompanyId(), JournalArticle.class.getName(),
68                  article.getResourcePrimKey(), article.getUserId(), actionId)) {
69  
70              return true;
71          }
72  
73          return permissionChecker.hasPermission(
74              article.getGroupId(), JournalArticle.class.getName(),
75              article.getResourcePrimKey(), actionId);
76      }
77  
78      public static boolean contains(
79              PermissionChecker permissionChecker, long resourcePrimKey,
80              String actionId)
81          throws PortalException, SystemException {
82  
83          JournalArticle article =
84              JournalArticleLocalServiceUtil.getLatestArticle(resourcePrimKey);
85  
86          return contains(permissionChecker, article, actionId);
87      }
88  
89      public static boolean contains(
90              PermissionChecker permissionChecker, long groupId, String articleId,
91              String actionId)
92          throws PortalException, SystemException {
93  
94          JournalArticle article = JournalArticleLocalServiceUtil.getArticle(
95              groupId, articleId);
96  
97          return contains(permissionChecker, article, actionId);
98      }
99  
100 }