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