1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.permission;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.security.permission.PermissionChecker;
26  import com.liferay.portlet.messageboards.model.MBCategory;
27  import com.liferay.portlet.messageboards.model.MBMessage;
28  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
29  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
30  
31  /**
32   * <a href="MBMessagePermission.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class MBMessagePermission {
38  
39      public static void check(
40              PermissionChecker permissionChecker, long messageId,
41              String actionId)
42          throws PortalException, SystemException {
43  
44          if (!contains(permissionChecker, messageId, actionId)) {
45              throw new PrincipalException();
46          }
47      }
48  
49      public static void check(
50              PermissionChecker permissionChecker, MBMessage message,
51              String actionId)
52          throws PortalException, SystemException {
53  
54          if (!contains(permissionChecker, message, actionId)) {
55              throw new PrincipalException();
56          }
57      }
58  
59      public static boolean contains(
60              PermissionChecker permissionChecker, long messageId,
61              String actionId)
62          throws PortalException, SystemException {
63  
64          MBMessage message =  MBMessageLocalServiceUtil.getMessage(messageId);
65  
66          return contains(permissionChecker, message, actionId);
67      }
68  
69      public static boolean contains(
70              PermissionChecker permissionChecker, MBMessage message,
71              String actionId)
72          throws SystemException {
73  
74          MBCategory category = message.getCategory();
75  
76          long groupId = 0;
77  
78          if (category != null) {
79              groupId = category.getGroupId();
80          }
81  
82          if (MBBanLocalServiceUtil.hasBan(
83                  groupId, permissionChecker.getUserId())) {
84  
85              return false;
86          }
87  
88          if (permissionChecker.hasOwnerPermission(
89                  message.getCompanyId(), MBMessage.class.getName(),
90                  message.getMessageId(), message.getUserId(), actionId)) {
91  
92              return true;
93          }
94  
95          return permissionChecker.hasPermission(
96              groupId, MBMessage.class.getName(), message.getMessageId(),
97              actionId);
98      }
99  
100 }