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.portal.util.PropsValues;
23  import com.liferay.portlet.shopping.model.ShoppingCategory;
24  import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
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, ShoppingCategory category,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, category, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public static boolean contains(
55              PermissionChecker permissionChecker, long groupId, long categoryId,
56              String actionId)
57          throws PortalException, SystemException {
58  
59          if (categoryId ==
60                  ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
61  
62              return ShoppingPermission.contains(
63                  permissionChecker, groupId, actionId);
64          }
65          else {
66              ShoppingCategory category =
67                  ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
68  
69              return contains(permissionChecker, category, actionId);
70          }
71      }
72  
73      public static boolean contains(
74              PermissionChecker permissionChecker, ShoppingCategory category,
75              String actionId)
76          throws PortalException, SystemException {
77  
78          if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
79              actionId = ActionKeys.ADD_SUBCATEGORY;
80          }
81  
82          long categoryId = category.getCategoryId();
83  
84          if (actionId.equals(ActionKeys.VIEW)) {
85              while (categoryId !=
86                      ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
87  
88                  category = ShoppingCategoryLocalServiceUtil.getCategory(
89                      categoryId);
90  
91                  categoryId = category.getParentCategoryId();
92  
93                  if (!permissionChecker.hasOwnerPermission(
94                          category.getCompanyId(),
95                          ShoppingCategory.class.getName(),
96                          category.getCategoryId(), category.getUserId(),
97                          actionId) &&
98                      !permissionChecker.hasPermission(
99                          category.getGroupId(), ShoppingCategory.class.getName(),
100                         category.getCategoryId(), actionId)) {
101 
102                     return false;
103                 }
104 
105                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
106                     break;
107                 }
108             }
109 
110             return true;
111         }
112         else {
113             while (categoryId !=
114                     ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
115 
116                 if (permissionChecker.hasOwnerPermission(
117                         category.getCompanyId(),
118                         ShoppingCategory.class.getName(),
119                         category.getCategoryId(), category.getUserId(),
120                         actionId)) {
121 
122                     return true;
123                 }
124 
125                 if (permissionChecker.hasPermission(
126                         category.getGroupId(), ShoppingCategory.class.getName(),
127                         category.getCategoryId(), actionId)) {
128 
129                     return true;
130                 }
131 
132                 if (actionId.equals(ActionKeys.VIEW)) {
133                     break;
134                 }
135 
136                 category = ShoppingCategoryLocalServiceUtil.getCategory(
137                     categoryId);
138 
139                 categoryId = category.getParentCategoryId();
140             }
141 
142             return false;
143         }
144     }
145 
146 }