001
014
015 package com.liferay.portlet.shopping.service.permission;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.security.auth.PrincipalException;
020 import com.liferay.portal.security.permission.ActionKeys;
021 import com.liferay.portal.security.permission.PermissionChecker;
022 import com.liferay.portal.util.PropsValues;
023 import com.liferay.portlet.shopping.model.ShoppingCategory;
024 import com.liferay.portlet.shopping.model.ShoppingCategoryConstants;
025 import com.liferay.portlet.shopping.service.ShoppingCategoryLocalServiceUtil;
026
027
030 public class ShoppingCategoryPermission {
031
032 public static void check(
033 PermissionChecker permissionChecker, long groupId, long categoryId,
034 String actionId)
035 throws PortalException, SystemException {
036
037 if (!contains(permissionChecker, groupId, categoryId, actionId)) {
038 throw new PrincipalException();
039 }
040 }
041
042 public static void check(
043 PermissionChecker permissionChecker, ShoppingCategory category,
044 String actionId)
045 throws PortalException, SystemException {
046
047 if (!contains(permissionChecker, category, actionId)) {
048 throw new PrincipalException();
049 }
050 }
051
052 public static boolean contains(
053 PermissionChecker permissionChecker, long groupId, long categoryId,
054 String actionId)
055 throws PortalException, SystemException {
056
057 if (categoryId ==
058 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
059
060 return ShoppingPermission.contains(
061 permissionChecker, groupId, actionId);
062 }
063 else {
064 ShoppingCategory category =
065 ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
066
067 return contains(permissionChecker, category, actionId);
068 }
069 }
070
071 public static boolean contains(
072 PermissionChecker permissionChecker, ShoppingCategory category,
073 String actionId)
074 throws PortalException, SystemException {
075
076 if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
077 actionId = ActionKeys.ADD_SUBCATEGORY;
078 }
079
080 long categoryId = category.getCategoryId();
081
082 if (actionId.equals(ActionKeys.VIEW)) {
083 while (categoryId !=
084 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
085
086 category = ShoppingCategoryLocalServiceUtil.getCategory(
087 categoryId);
088
089 categoryId = category.getParentCategoryId();
090
091 if (!permissionChecker.hasOwnerPermission(
092 category.getCompanyId(),
093 ShoppingCategory.class.getName(),
094 category.getCategoryId(), category.getUserId(),
095 actionId) &&
096 !permissionChecker.hasPermission(
097 category.getGroupId(), ShoppingCategory.class.getName(),
098 category.getCategoryId(), actionId)) {
099
100 return false;
101 }
102
103 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
104 break;
105 }
106 }
107
108 return true;
109 }
110
111 while (categoryId !=
112 ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
113
114 category = ShoppingCategoryLocalServiceUtil.getCategory(categoryId);
115
116 categoryId = category.getParentCategoryId();
117
118 if (permissionChecker.hasOwnerPermission(
119 category.getCompanyId(), ShoppingCategory.class.getName(),
120 category.getCategoryId(), category.getUserId(), 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
137 return false;
138 }
139
140 }