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