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.portal.service.permission.PortletPermissionUtil;
28 import com.liferay.portal.util.PortletKeys;
29 import com.liferay.portlet.shopping.model.ShoppingCategory;
30 import com.liferay.portlet.shopping.model.impl.ShoppingCategoryImpl;
31 import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
32
33
39 public class ShoppingCategoryPermission {
40
41 public static void check(
42 PermissionChecker permissionChecker, long plid, long categoryId,
43 String actionId)
44 throws PortalException, SystemException {
45
46 if (!contains(permissionChecker, plid, categoryId, actionId)) {
47 throw new PrincipalException();
48 }
49 }
50
51 public static void check(
52 PermissionChecker permissionChecker, long categoryId,
53 String actionId)
54 throws PortalException, SystemException {
55
56 if (!contains(permissionChecker, categoryId, actionId)) {
57 throw new PrincipalException();
58 }
59 }
60
61 public static void check(
62 PermissionChecker permissionChecker, ShoppingCategory category,
63 String actionId)
64 throws PortalException, SystemException {
65
66 if (!contains(permissionChecker, category, actionId)) {
67 throw new PrincipalException();
68 }
69 }
70
71 public static boolean contains(
72 PermissionChecker permissionChecker, long plid, long categoryId,
73 String actionId)
74 throws PortalException, SystemException {
75
76 if (categoryId == ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
77 return PortletPermissionUtil.contains(
78 permissionChecker, plid, PortletKeys.SHOPPING, actionId);
79 }
80 else {
81 return contains(permissionChecker, categoryId, actionId);
82 }
83 }
84
85 public static boolean contains(
86 PermissionChecker permissionChecker, long categoryId,
87 String actionId)
88 throws PortalException, SystemException {
89
90 ShoppingCategory category =
91 ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
92
93 return contains(permissionChecker, category, actionId);
94 }
95
96 public static boolean contains(
97 PermissionChecker permissionChecker, ShoppingCategory category,
98 String actionId)
99 throws PortalException, SystemException {
100
101 if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
102 actionId = ActionKeys.ADD_SUBCATEGORY;
103 }
104
105 long categoryId = category.getCategoryId();
106
107 while (categoryId != ShoppingCategoryImpl.DEFAULT_PARENT_CATEGORY_ID) {
108 if (permissionChecker.hasOwnerPermission(
109 category.getCompanyId(), ShoppingCategory.class.getName(),
110 category.getCategoryId(), category.getUserId(), actionId)) {
111
112 return true;
113 }
114
115 if (permissionChecker.hasPermission(
116 category.getGroupId(), ShoppingCategory.class.getName(),
117 category.getCategoryId(), actionId)) {
118
119 return true;
120 }
121
122 if (actionId.equals(ActionKeys.VIEW)) {
123 break;
124 }
125
126 category = ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
127
128 categoryId = category.getParentCategoryId();
129 }
130
131 return false;
132 }
133
134 }