1
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
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 }