1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.messageboards.service.permission;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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.service.permission.PortletPermissionUtil;
23  import com.liferay.portal.util.PortletKeys;
24  import com.liferay.portal.util.PropsValues;
25  import com.liferay.portlet.messageboards.model.MBCategory;
26  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
27  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
28  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
29  
30  /**
31   * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class MBCategoryPermission {
36  
37      public static void check(
38              PermissionChecker permissionChecker, long plid, long categoryId,
39              String actionId)
40          throws PortalException, SystemException {
41  
42          if (!contains(permissionChecker, plid, categoryId, actionId)) {
43              throw new PrincipalException();
44          }
45      }
46  
47      public static void check(
48              PermissionChecker permissionChecker, long categoryId,
49              String actionId)
50          throws PortalException, SystemException {
51  
52          if (!contains(permissionChecker, categoryId, actionId)) {
53              throw new PrincipalException();
54          }
55      }
56  
57      public static void check(
58              PermissionChecker permissionChecker, MBCategory category,
59              String actionId)
60          throws PortalException, SystemException {
61  
62          if (!contains(permissionChecker, category, actionId)) {
63              throw new PrincipalException();
64          }
65      }
66  
67      public static boolean contains(
68              PermissionChecker permissionChecker, long plid, long categoryId,
69              String actionId)
70          throws PortalException, SystemException {
71  
72          if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
73              return PortletPermissionUtil.contains(
74                  permissionChecker, plid, PortletKeys.MESSAGE_BOARDS, actionId);
75          }
76          else {
77              return contains(permissionChecker, categoryId, actionId);
78          }
79      }
80  
81      public static boolean contains(
82              PermissionChecker permissionChecker, long categoryId,
83              String actionId)
84          throws PortalException, SystemException {
85  
86          MBCategory category =
87              MBCategoryLocalServiceUtil.getCategory(categoryId);
88  
89          return contains(permissionChecker, category, actionId);
90      }
91  
92      public static boolean contains(
93              PermissionChecker permissionChecker, MBCategory category,
94              String actionId)
95          throws PortalException, SystemException {
96  
97          if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
98              actionId = ActionKeys.ADD_SUBCATEGORY;
99          }
100 
101         if (MBBanLocalServiceUtil.hasBan(
102                 category.getGroupId(), permissionChecker.getUserId())) {
103 
104             return false;
105         }
106 
107         long categoryId = category.getCategoryId();
108 
109         if (actionId.equals(ActionKeys.VIEW)) {
110             while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
111                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
112 
113                 categoryId = category.getParentCategoryId();
114 
115                 if (!permissionChecker.hasOwnerPermission(
116                         category.getCompanyId(), MBCategory.class.getName(),
117                         category.getCategoryId(), category.getUserId(),
118                         actionId) &&
119                     !permissionChecker.hasPermission(
120                         category.getGroupId(), MBCategory.class.getName(),
121                         category.getCategoryId(), actionId)) {
122 
123                     return false;
124                 }
125 
126                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
127                     break;
128                 }
129             }
130 
131             return true;
132         }
133         else {
134             while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
135                 if (permissionChecker.hasOwnerPermission(
136                         category.getCompanyId(), MBCategory.class.getName(),
137                         category.getCategoryId(), category.getUserId(),
138                         actionId)) {
139 
140                     return true;
141                 }
142 
143                 if (permissionChecker.hasPermission(
144                         category.getGroupId(), MBCategory.class.getName(),
145                         category.getCategoryId(), actionId)) {
146 
147                     return true;
148                 }
149 
150                 if (actionId.equals(ActionKeys.VIEW)) {
151                     break;
152                 }
153 
154                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
155 
156                 categoryId = category.getParentCategoryId();
157             }
158 
159             return false;
160         }
161     }
162 
163 }