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.portal.util.PortalUtil;
27  import com.liferay.portlet.messageboards.model.MBDiscussion;
28  import com.liferay.portlet.messageboards.model.MBMessage;
29  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
30  import com.liferay.portlet.messageboards.service.MBDiscussionLocalServiceUtil;
31  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
32  
33  /**
34   * <a href="MBDiscussionPermission.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Charles May
37   *
38   */
39  public class MBDiscussionPermission {
40  
41      public static void check(
42              PermissionChecker permissionChecker, long companyId, long groupId,
43              String className, long classPK, long ownerId, String actionId)
44          throws PortalException, SystemException {
45  
46          if (!contains(
47                  permissionChecker, companyId, groupId, className, classPK,
48                  ownerId, actionId)) {
49  
50              throw new PrincipalException();
51          }
52      }
53  
54      public static void check(
55              PermissionChecker permissionChecker, long companyId, long groupId,
56              String className, long classPK, long messageId, long ownerId,
57              String actionId)
58          throws PortalException, SystemException {
59  
60          if (!contains(
61                  permissionChecker, companyId, groupId, className, classPK,
62                  messageId, ownerId, actionId)) {
63  
64              throw new PrincipalException();
65          }
66      }
67  
68      public static boolean contains(
69              PermissionChecker permissionChecker, long companyId, long groupId,
70              String className, long classPK, long ownerId, String actionId)
71          throws SystemException {
72  
73          if (MBBanLocalServiceUtil.hasBan(
74                  groupId, permissionChecker.getUserId())) {
75  
76              return false;
77          }
78  
79          if (permissionChecker.hasOwnerPermission(
80                  companyId, className, classPK, ownerId, actionId)) {
81  
82              return true;
83          }
84  
85          return permissionChecker.hasPermission(
86              groupId, className, classPK, actionId);
87      }
88  
89      public static boolean contains(
90              PermissionChecker permissionChecker, long companyId, long groupId,
91              String className, long classPK, long messageId, long ownerId,
92              String actionId)
93          throws PortalException, SystemException {
94  
95          if (!contains(
96                  permissionChecker, companyId, groupId, className, classPK,
97                  ownerId, actionId)) {
98  
99              return false;
100         }
101 
102         MBMessage message = MBMessageLocalServiceUtil.getMessage(
103             messageId);
104 
105         MBDiscussion discussion =
106             MBDiscussionLocalServiceUtil.getThreadDiscussion(
107                 message.getThreadId());
108 
109         long classNameId = PortalUtil.getClassNameId(className);
110 
111         if ((discussion.getClassNameId() == classNameId) &&
112             (discussion.getClassPK() == classPK)) {
113 
114             return true;
115         }
116 
117         return false;
118     }
119 
120 }