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