1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.service.permission;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portlet.shopping.model.ShoppingCategory;
28  import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
29  import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
30  
31  /**
32   * <a href="ShoppingCategoryPermission.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class ShoppingCategoryPermission {
38  
39      public static void check(
40              PermissionChecker permissionChecker, long groupId, long categoryId,
41              String actionId)
42          throws PortalException, SystemException {
43  
44          if (!contains(permissionChecker, groupId, categoryId, actionId)) {
45              throw new PrincipalException();
46          }
47      }
48  
49      public static void check(
50              PermissionChecker permissionChecker, long categoryId,
51              String actionId)
52          throws PortalException, SystemException {
53  
54          if (!contains(permissionChecker, categoryId, actionId)) {
55              throw new PrincipalException();
56          }
57      }
58  
59      public static void check(
60              PermissionChecker permissionChecker, ShoppingCategory category,
61              String actionId)
62          throws PortalException, SystemException {
63  
64          if (!contains(permissionChecker, category, actionId)) {
65              throw new PrincipalException();
66          }
67      }
68  
69      public static boolean contains(
70              PermissionChecker permissionChecker, long groupId, long categoryId,
71              String actionId)
72          throws PortalException, SystemException {
73  
74          if (categoryId == ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
75              return ShoppingPermission.contains(
76                  permissionChecker, groupId, actionId);
77          }
78          else {
79              return contains(permissionChecker, categoryId, actionId);
80          }
81      }
82  
83      public static boolean contains(
84              PermissionChecker permissionChecker, long categoryId,
85              String actionId)
86          throws PortalException, SystemException {
87  
88          ShoppingCategory category =
89              ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
90  
91          return contains(permissionChecker, category, actionId);
92      }
93  
94      public static boolean contains(
95              PermissionChecker permissionChecker, ShoppingCategory category,
96              String actionId)
97          throws PortalException, SystemException {
98  
99          if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
100             actionId = ActionKeys.ADD_SUBCATEGORY;
101         }
102 
103         long categoryId = category.getCategoryId();
104 
105         while (categoryId != ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
106             if (permissionChecker.hasOwnerPermission(
107                     category.getCompanyId(), ShoppingCategory.class.getName(),
108                     category.getCategoryId(), category.getUserId(), actionId)) {
109 
110                 return true;
111             }
112 
113             if (permissionChecker.hasPermission(
114                     category.getGroupId(), ShoppingCategory.class.getName(),
115                     category.getCategoryId(), actionId)) {
116 
117                 return true;
118             }
119 
120             if (actionId.equals(ActionKeys.VIEW)) {
121                 break;
122             }
123 
124             category = ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
125 
126             categoryId = category.getParentCategoryId();
127         }
128 
129         return false;
130     }
131 
132 }