1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.shopping.service.permission;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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.shopping.model.ShoppingCategory;
24  import com.liferay.portlet.shopping.model.ShoppingItem;
25  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
26  
27  /**
28   * <a href="ShoppingItemPermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class ShoppingItemPermission {
33  
34      public static void check(
35              PermissionChecker permissionChecker, long itemId, String actionId)
36          throws PortalException, SystemException {
37  
38          if (!contains(permissionChecker, itemId, actionId)) {
39              throw new PrincipalException();
40          }
41      }
42  
43      public static void check(
44              PermissionChecker permissionChecker, ShoppingItem item,
45              String actionId)
46          throws PortalException, SystemException {
47  
48          if (!contains(permissionChecker, item, actionId)) {
49              throw new PrincipalException();
50          }
51      }
52  
53      public static boolean contains(
54              PermissionChecker permissionChecker, long itemId, String actionId)
55          throws PortalException, SystemException {
56  
57          ShoppingItem item = ShoppingItemLocalServiceUtil.getItem(itemId);
58  
59          return contains(permissionChecker, item, actionId);
60      }
61  
62      public static boolean contains(
63              PermissionChecker permissionChecker, ShoppingItem item,
64              String actionId)
65          throws PortalException, SystemException {
66  
67          ShoppingCategory category = item.getCategory();
68  
69          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
70              if (!ShoppingCategoryPermission.contains(
71                      permissionChecker, category, ActionKeys.VIEW)) {
72  
73                  return false;
74              }
75          }
76  
77          if (permissionChecker.hasOwnerPermission(
78                  item.getCompanyId(), ShoppingItem.class.getName(),
79                  item.getItemId(), item.getUserId(), actionId)) {
80  
81              return true;
82          }
83  
84          return permissionChecker.hasPermission(
85              category.getGroupId(), ShoppingItem.class.getName(),
86              item.getItemId(), actionId);
87      }
88  
89  }