1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.permission;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portlet.messageboards.model.MBCategory;
31  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
32  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
33  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
34  
35  /**
36   * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class MBCategoryPermission {
42  
43      public static void check(
44              PermissionChecker permissionChecker, long groupId, long categoryId,
45              String actionId)
46          throws PortalException, SystemException {
47  
48          if (!contains(permissionChecker, groupId, categoryId, actionId)) {
49              throw new PrincipalException();
50          }
51      }
52  
53      public static void check(
54              PermissionChecker permissionChecker, long categoryId,
55              String actionId)
56          throws PortalException, SystemException {
57  
58          if (!contains(permissionChecker, categoryId, actionId)) {
59              throw new PrincipalException();
60          }
61      }
62  
63      public static void check(
64              PermissionChecker permissionChecker, MBCategory category,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (!contains(permissionChecker, category, actionId)) {
69              throw new PrincipalException();
70          }
71      }
72  
73      public static boolean contains(
74              PermissionChecker permissionChecker, long groupId, long categoryId,
75              String actionId)
76          throws PortalException, SystemException {
77  
78          if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
79              return MBPermission.contains(permissionChecker, groupId, actionId);
80          }
81          else {
82              return contains(permissionChecker, categoryId, actionId);
83          }
84      }
85  
86      public static boolean contains(
87              PermissionChecker permissionChecker, long categoryId,
88              String actionId)
89          throws PortalException, SystemException {
90  
91          MBCategory category =
92              MBCategoryLocalServiceUtil.getCategory(categoryId);
93  
94          return contains(permissionChecker, category, actionId);
95      }
96  
97      public static boolean contains(
98              PermissionChecker permissionChecker, MBCategory category,
99              String actionId)
100         throws PortalException, SystemException {
101 
102         if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
103             actionId = ActionKeys.ADD_SUBCATEGORY;
104         }
105 
106         if (MBBanLocalServiceUtil.hasBan(
107                 category.getGroupId(), permissionChecker.getUserId())) {
108 
109             return false;
110         }
111 
112         long categoryId = category.getCategoryId();
113 
114         while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
115             category = MBCategoryLocalServiceUtil.getCategory(categoryId);
116 
117             categoryId = category.getParentCategoryId();
118 
119             if (permissionChecker.hasOwnerPermission(
120                     category.getCompanyId(), MBCategory.class.getName(),
121                     category.getCategoryId(), category.getUserId(), actionId)) {
122 
123                 return true;
124             }
125 
126             if (permissionChecker.hasPermission(
127                     category.getGroupId(), MBCategory.class.getName(),
128                     category.getCategoryId(), actionId)) {
129 
130                 return true;
131             }
132 
133             if (actionId.equals(ActionKeys.VIEW)) {
134                 break;
135             }
136         }
137 
138         return false;
139     }
140 
141 }