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.shopping.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.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portlet.shopping.model.ShoppingOrder;
23  import com.liferay.portlet.shopping.service.ShoppingOrderLocalServiceUtil;
24  
25  /**
26   * <a href="ShoppingOrderPermission.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class ShoppingOrderPermission {
31  
32      public static void check(
33              PermissionChecker permissionChecker, long groupId, long orderId,
34              String actionId)
35          throws PortalException, SystemException {
36  
37          if (!contains(permissionChecker, groupId, orderId, actionId)) {
38              throw new PrincipalException();
39          }
40      }
41  
42      public static void check(
43              PermissionChecker permissionChecker, long groupId,
44              ShoppingOrder order, String actionId)
45          throws PortalException {
46  
47          if (!contains(permissionChecker, groupId, order, actionId)) {
48              throw new PrincipalException();
49          }
50      }
51  
52      public static boolean contains(
53              PermissionChecker permissionChecker, long groupId, long orderId,
54              String actionId)
55          throws PortalException, SystemException {
56  
57          ShoppingOrder order =
58              ShoppingOrderLocalServiceUtil.getOrder(orderId);
59  
60          return contains(permissionChecker, groupId, order, actionId);
61      }
62  
63      public static boolean contains(
64          PermissionChecker permissionChecker, long groupId, ShoppingOrder order,
65          String actionId) {
66  
67          if (ShoppingPermission.contains(
68                  permissionChecker, groupId, ActionKeys.MANAGE_ORDERS)) {
69  
70              return true;
71          }
72          else {
73              if (permissionChecker.hasOwnerPermission(
74                      order.getCompanyId(), ShoppingOrder.class.getName(),
75                      order.getOrderId(), order.getUserId(), actionId)) {
76  
77                  return true;
78              }
79  
80              return permissionChecker.hasPermission(
81                  order.getGroupId(), ShoppingOrder.class.getName(),
82                  order.getOrderId(), actionId);
83          }
84      }
85  
86  }