1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.permission;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portal.service.permission.PortletPermissionUtil;
28  import com.liferay.portal.util.PortletKeys;
29  import com.liferay.portlet.messageboards.model.MBCategory;
30  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
31  import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
32  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
33  
34  /**
35   * <a href="MBCategoryPermission.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class MBCategoryPermission {
41  
42      public static void check(
43              PermissionChecker permissionChecker, long plid, long categoryId,
44              String actionId)
45          throws PortalException, SystemException {
46  
47          if (!contains(permissionChecker, plid, categoryId, actionId)) {
48              throw new PrincipalException();
49          }
50      }
51  
52      public static void check(
53              PermissionChecker permissionChecker, long categoryId,
54              String actionId)
55          throws PortalException, SystemException {
56  
57          if (!contains(permissionChecker, categoryId, actionId)) {
58              throw new PrincipalException();
59          }
60      }
61  
62      public static void check(
63              PermissionChecker permissionChecker, MBCategory category,
64              String actionId)
65          throws PortalException, SystemException {
66  
67          if (!contains(permissionChecker, category, actionId)) {
68              throw new PrincipalException();
69          }
70      }
71  
72      public static boolean contains(
73              PermissionChecker permissionChecker, long plid, long categoryId,
74              String actionId)
75          throws PortalException, SystemException {
76  
77          if (categoryId == MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
78              return PortletPermissionUtil.contains(
79                  permissionChecker, plid, PortletKeys.MESSAGE_BOARDS, 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             if (permissionChecker.hasOwnerPermission(
116                     category.getCompanyId(), MBCategory.class.getName(),
117                     category.getCategoryId(), category.getUserId(), actionId)) {
118 
119                 return true;
120             }
121 
122             if (permissionChecker.hasPermission(
123                     category.getGroupId(), MBCategory.class.getName(),
124                     category.getCategoryId(), actionId)) {
125 
126                 return true;
127             }
128 
129             if (actionId.equals(ActionKeys.VIEW)) {
130                 break;
131             }
132 
133             category = MBCategoryLocalServiceUtil.getCategory(categoryId);
134 
135             categoryId = category.getParentCategoryId();
136         }
137 
138         return false;
139     }
140 
141 }