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.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portlet.messageboards.model.MBCategory;
24  import com.liferay.portlet.messageboards.model.MBMessage;
25  import com.liferay.portlet.messageboards.model.MBThread;
26  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
27  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
28  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
29  import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
30  
31  /**
32   * <a href="MBMessagePermission.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class MBMessagePermission {
37  
38      public static void check(
39              PermissionChecker permissionChecker, long messageId,
40              String actionId)
41          throws PortalException, SystemException {
42  
43          if (!contains(permissionChecker, messageId, actionId)) {
44              throw new PrincipalException();
45          }
46      }
47  
48      public static void check(
49              PermissionChecker permissionChecker, MBMessage message,
50              String actionId)
51          throws PortalException, SystemException {
52  
53          if (!contains(permissionChecker, message, actionId)) {
54              throw new PrincipalException();
55          }
56      }
57  
58      public static boolean contains(
59              PermissionChecker permissionChecker, long messageId,
60              String actionId)
61          throws PortalException, SystemException {
62  
63          MBMessage message =  MBMessageLocalServiceUtil.getMessage(messageId);
64  
65          return contains(permissionChecker, message, actionId);
66      }
67  
68      public static boolean contains(
69              PermissionChecker permissionChecker, MBMessage message,
70              String actionId)
71          throws PortalException, SystemException {
72  
73          long groupId = message.getGroupId();
74  
75          if (MBBanLocalServiceUtil.hasBan(
76                  groupId, permissionChecker.getUserId())) {
77  
78              return false;
79          }
80  
81          MBCategory category = MBCategoryLocalServiceUtil.getCategory(
82              message.getCategoryId());
83  
84          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
85              if (!MBCategoryPermission.contains(
86                      permissionChecker, category, ActionKeys.VIEW)) {
87  
88                  return false;
89              }
90          }
91  
92          MBThread thread = MBThreadLocalServiceUtil.getThread(
93              message.getThreadId());
94  
95          if (permissionChecker.hasOwnerPermission(
96                  message.getCompanyId(), MBMessage.class.getName(),
97                  thread.getRootMessageId(), message.getUserId(), actionId)) {
98  
99              return true;
100         }
101 
102         return permissionChecker.hasPermission(
103             groupId, MBMessage.class.getName(), thread.getRootMessageId(),
104             actionId);
105     }
106 
107 }