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.impl.ShoppingCategoryImpl;
25  import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
26  
27  /**
28   * <a href="ShoppingCategoryPermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class ShoppingCategoryPermission {
33  
34      public static void check(
35              PermissionChecker permissionChecker, long groupId, long categoryId,
36              String actionId)
37          throws PortalException, SystemException {
38  
39          if (!contains(permissionChecker, groupId, categoryId, actionId)) {
40              throw new PrincipalException();
41          }
42      }
43  
44      public static void check(
45              PermissionChecker permissionChecker, long categoryId,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, categoryId, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public static void check(
55              PermissionChecker permissionChecker, ShoppingCategory category,
56              String actionId)
57          throws PortalException, SystemException {
58  
59          if (!contains(permissionChecker, category, actionId)) {
60              throw new PrincipalException();
61          }
62      }
63  
64      public static boolean contains(
65              PermissionChecker permissionChecker, long groupId, long categoryId,
66              String actionId)
67          throws PortalException, SystemException {
68  
69          if (categoryId == ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
70              return ShoppingPermission.contains(
71                  permissionChecker, groupId, actionId);
72          }
73          else {
74              return contains(permissionChecker, categoryId, actionId);
75          }
76      }
77  
78      public static boolean contains(
79              PermissionChecker permissionChecker, long categoryId,
80              String actionId)
81          throws PortalException, SystemException {
82  
83          ShoppingCategory category =
84              ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
85  
86          return contains(permissionChecker, category, actionId);
87      }
88  
89      public static boolean contains(
90              PermissionChecker permissionChecker, ShoppingCategory category,
91              String actionId)
92          throws PortalException, SystemException {
93  
94          if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
95              actionId = ActionKeys.ADD_SUBCATEGORY;
96          }
97  
98          long categoryId = category.getCategoryId();
99  
100         if (actionId.equals(ActionKeys.VIEW)) {
101             while (categoryId !=
102                         ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
103 
104                 category = ShoppingCategoryLocalServiceUtil.getCategory(
105                     categoryId);
106 
107                 categoryId = category.getParentCategoryId();
108 
109                 if (!permissionChecker.hasOwnerPermission(
110                         category.getCompanyId(),
111                         ShoppingCategory.class.getName(),
112                         category.getCategoryId(), category.getUserId(),
113                         actionId) &&
114                     !permissionChecker.hasPermission(
115                         category.getGroupId(), ShoppingCategory.class.getName(),
116                         category.getCategoryId(), actionId)) {
117 
118                     return false;
119                 }
120 
121                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
122                     break;
123                 }
124             }
125 
126             return true;
127         }
128         else {
129             while (categoryId !=
130                         ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
131 
132                 if (permissionChecker.hasOwnerPermission(
133                         category.getCompanyId(),
134                         ShoppingCategory.class.getName(),
135                         category.getCategoryId(), category.getUserId(),
136                         actionId)) {
137 
138                     return true;
139                 }
140 
141                 if (permissionChecker.hasPermission(
142                         category.getGroupId(), ShoppingCategory.class.getName(),
143                         category.getCategoryId(), actionId)) {
144 
145                     return true;
146                 }
147 
148                 if (actionId.equals(ActionKeys.VIEW)) {
149                     break;
150                 }
151 
152                 category = ShoppingCategoryLocalServiceUtil.getCategory(
153                     categoryId);
154 
155                 categoryId = category.getParentCategoryId();
156             }
157 
158             return false;
159         }
160     }
161 
162 }