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.asset.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.PermissionChecker;
21  import com.liferay.portlet.asset.model.AssetCategory;
22  import com.liferay.portlet.asset.model.AssetCategoryConstants;
23  import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
24  
25  /**
26   * <a href="AssetCategoryPermission.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Eduardo Lundgren
29   */
30  public class AssetCategoryPermission {
31  
32      public static void check(
33              PermissionChecker permissionChecker, AssetCategory category,
34              String actionId)
35          throws PortalException {
36  
37          if (!contains(permissionChecker, category, actionId)) {
38              throw new PrincipalException();
39          }
40      }
41  
42      public static void check(
43              PermissionChecker permissionChecker, long groupId, long categoryId,
44              String actionId)
45          throws PortalException, SystemException {
46  
47          if (!contains(permissionChecker, groupId, 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 boolean contains(
63          PermissionChecker permissionChecker, AssetCategory category,
64          String actionId) {
65  
66          if (permissionChecker.hasOwnerPermission(
67                  category.getCompanyId(), AssetCategory.class.getName(),
68                  category.getCategoryId(), category.getUserId(), actionId)) {
69  
70              return true;
71          }
72  
73          return permissionChecker.hasPermission(
74              category.getGroupId(), AssetCategory.class.getName(),
75              category.getCategoryId(), actionId);
76      }
77  
78      public static boolean contains(
79              PermissionChecker permissionChecker, long groupId, long categoryId,
80              String actionId)
81          throws PortalException, SystemException {
82  
83          if (categoryId == AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
84              return AssetPermission.contains(
85                  permissionChecker, groupId, actionId);
86          }
87          else {
88              return contains(permissionChecker, categoryId, actionId);
89          }
90      }
91  
92      public static boolean contains(
93              PermissionChecker permissionChecker, long categoryId,
94              String actionId)
95          throws PortalException, SystemException {
96  
97          AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
98              categoryId);
99  
100         return contains(permissionChecker, category, actionId);
101     }
102 
103 }