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.messageboards.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.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 }