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.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portlet.messageboards.model.MBCategory;
24  import com.liferay.portlet.messageboards.model.MBCategoryConstants;
25  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
26  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
27  
28  /**
29   * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class MBCategoryPermission {
34  
35      public static void check(
36              PermissionChecker permissionChecker, long groupId, long categoryId,
37              String actionId)
38          throws PortalException, SystemException {
39  
40          if (!contains(permissionChecker, groupId, categoryId, actionId)) {
41              throw new PrincipalException();
42          }
43      }
44  
45      public static void check(
46              PermissionChecker permissionChecker, MBCategory category,
47              String actionId)
48          throws PortalException, SystemException {
49  
50          if (!contains(permissionChecker, category, actionId)) {
51              throw new PrincipalException();
52          }
53      }
54  
55      public static boolean contains(
56              PermissionChecker permissionChecker, long groupId, long categoryId,
57              String actionId)
58          throws PortalException, SystemException {
59  
60          if (categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
61              return MBPermission.contains(permissionChecker, groupId, actionId);
62          }
63          else {
64              MBCategory category = MBCategoryLocalServiceUtil.getCategory(
65                  categoryId);
66  
67              return contains(permissionChecker, category, actionId);
68          }
69      }
70  
71      public static boolean contains(
72              PermissionChecker permissionChecker, MBCategory category,
73              String actionId)
74          throws PortalException, SystemException {
75  
76          if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
77              actionId = ActionKeys.ADD_SUBCATEGORY;
78          }
79  
80          if (MBBanLocalServiceUtil.hasBan(
81                  category.getGroupId(), permissionChecker.getUserId())) {
82  
83              return false;
84          }
85  
86          long categoryId = category.getCategoryId();
87  
88          if (actionId.equals(ActionKeys.VIEW)) {
89              while (categoryId !=
90                      MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
91  
92                  category = MBCategoryLocalServiceUtil.getCategory(categoryId);
93  
94                  categoryId = category.getParentCategoryId();
95  
96                  if (!permissionChecker.hasOwnerPermission(
97                          category.getCompanyId(), MBCategory.class.getName(),
98                          category.getCategoryId(), category.getUserId(),
99                          actionId) &&
100                     !permissionChecker.hasPermission(
101                         category.getGroupId(), MBCategory.class.getName(),
102                         category.getCategoryId(), actionId)) {
103 
104                     return false;
105                 }
106 
107                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
108                     break;
109                 }
110             }
111 
112             return true;
113         }
114         else {
115             while (categoryId !=
116                     MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
117 
118                 if (permissionChecker.hasOwnerPermission(
119                         category.getCompanyId(), MBCategory.class.getName(),
120                         category.getCategoryId(), category.getUserId(),
121                         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                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
138 
139                 categoryId = category.getParentCategoryId();
140             }
141 
142             return false;
143         }
144     }
145 
146 }