1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.messageboards.service.permission;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.security.permission.PermissionChecker;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.messageboards.model.MBDiscussion;
23  import com.liferay.portlet.messageboards.model.MBMessage;
24  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
25  import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
26  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
27  
28  /**
29   * <a href="MBDiscussionPermission.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Charles May
32   */
33  public class MBDiscussionPermission {
34  
35      public static void check(
36              PermissionChecker permissionChecker, long companyId, long groupId,
37              String className, long classPK, long messageId, long ownerId,
38              String actionId)
39          throws PortalException, SystemException {
40  
41          if (!contains(
42                  permissionChecker, companyId, groupId, className, classPK,
43                  messageId, ownerId, actionId)) {
44  
45              throw new PrincipalException();
46          }
47      }
48  
49      public static void check(
50              PermissionChecker permissionChecker, long companyId, long groupId,
51              String className, long classPK, long ownerId, String actionId)
52          throws PortalException, SystemException {
53  
54          if (!contains(
55                  permissionChecker, companyId, groupId, className, classPK,
56                  ownerId, actionId)) {
57  
58              throw new PrincipalException();
59          }
60      }
61  
62      public static boolean contains(
63              PermissionChecker permissionChecker, long companyId, long groupId,
64              String className, long classPK, long messageId, long ownerId,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (!contains(
69                  permissionChecker, companyId, groupId, className, classPK,
70                  ownerId, actionId)) {
71  
72              return false;
73          }
74  
75          MBMessage message = MBMessageLocalServiceUtil.getMessage(
76              messageId);
77  
78          MBDiscussion discussion =
79              MBDiscussionLocalServiceUtil.getThreadDiscussion(
80                  message.getThreadId());
81  
82          long classNameId = PortalUtil.getClassNameId(className);
83  
84          if ((discussion.getClassNameId() == classNameId) &&
85              (discussion.getClassPK() == classPK)) {
86  
87              return true;
88          }
89  
90          return false;
91      }
92  
93      public static boolean contains(
94              PermissionChecker permissionChecker, long companyId, long groupId,
95              String className, long classPK, long ownerId, String actionId)
96          throws SystemException {
97  
98          if (MBBanLocalServiceUtil.hasBan(
99                  groupId, permissionChecker.getUserId())) {
100 
101             return false;
102         }
103 
104         if (permissionChecker.hasOwnerPermission(
105                 companyId, className, classPK, ownerId, actionId)) {
106 
107             return true;
108         }
109 
110         return permissionChecker.hasPermission(
111             groupId, className, classPK, actionId);
112     }
113 
114 }