1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.portal.util.PropsValues;
31  import com.liferay.portlet.messageboards.model.MBCategory;
32  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
33  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
34  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
35  
36  /**
37   * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
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         if (actionId.equals(ActionKeys.VIEW)) {
115             while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
116                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
117 
118                 categoryId = category.getParentCategoryId();
119 
120                 if (!permissionChecker.hasOwnerPermission(
121                         category.getCompanyId(), MBCategory.class.getName(),
122                         category.getCategoryId(), category.getUserId(),
123                         actionId) &&
124                     !permissionChecker.hasPermission(
125                         category.getGroupId(), MBCategory.class.getName(),
126                         category.getCategoryId(), actionId)) {
127 
128                     return false;
129                 }
130 
131                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
132                     break;
133                 }
134             }
135 
136             return true;
137         }
138         else {
139             while (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
140                 if (permissionChecker.hasOwnerPermission(
141                         category.getCompanyId(), MBCategory.class.getName(),
142                         category.getCategoryId(), category.getUserId(),
143                         actionId)) {
144 
145                     return true;
146                 }
147 
148                 if (permissionChecker.hasPermission(
149                         category.getGroupId(), MBCategory.class.getName(),
150                         category.getCategoryId(), actionId)) {
151 
152                     return true;
153                 }
154 
155                 if (actionId.equals(ActionKeys.VIEW)) {
156                     break;
157                 }
158 
159                 category = MBCategoryLocalServiceUtil.getCategory(categoryId);
160 
161                 categoryId = category.getParentCategoryId();
162             }
163 
164             return false;
165         }
166     }
167 
168 }