001
014
015 package com.liferay.portlet.messageboards.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.messageboards.NoSuchCategoryException;
024 import com.liferay.portlet.messageboards.model.MBCategory;
025 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
026 import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
027 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
028
029
033 public class MBCategoryPermission {
034
035 public static void check(
036 PermissionChecker permissionChecker, long groupId, long categoryId,
037 String actionId)
038 throws PortalException, SystemException {
039
040 if (!contains(permissionChecker, groupId, categoryId, actionId)) {
041 throw new PrincipalException();
042 }
043 }
044
045 public static void check(
046 PermissionChecker permissionChecker, long categoryId,
047 String actionId)
048 throws PortalException, SystemException {
049
050 if (!contains(permissionChecker, categoryId, actionId)) {
051 throw new PrincipalException();
052 }
053 }
054
055 public static void check(
056 PermissionChecker permissionChecker, MBCategory category,
057 String actionId)
058 throws PortalException, SystemException {
059
060 if (!contains(permissionChecker, category, actionId)) {
061 throw new PrincipalException();
062 }
063 }
064
065 public static boolean contains(
066 PermissionChecker permissionChecker, long groupId, long categoryId,
067 String actionId)
068 throws PortalException, SystemException {
069
070 if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
071 (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
072
073 return MBPermission.contains(permissionChecker, groupId, actionId);
074 }
075
076 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
077 categoryId);
078
079 return contains(permissionChecker, category, actionId);
080 }
081
082 public static boolean contains(
083 PermissionChecker permissionChecker, long categoryId,
084 String actionId)
085 throws PortalException, SystemException {
086
087 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
088 categoryId);
089
090 return contains(permissionChecker, category, actionId);
091 }
092
093 public static boolean contains(
094 PermissionChecker permissionChecker, MBCategory category,
095 String actionId)
096 throws PortalException, SystemException {
097
098 if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
099 actionId = ActionKeys.ADD_SUBCATEGORY;
100 }
101
102 if (MBBanLocalServiceUtil.hasBan(
103 category.getGroupId(), permissionChecker.getUserId())) {
104
105 return false;
106 }
107
108 if (actionId.equals(ActionKeys.VIEW) &&
109 PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
110
111 try {
112 long categoryId = category.getCategoryId();
113
114 while (categoryId !=
115 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
116
117 category = MBCategoryLocalServiceUtil.getCategory(
118 categoryId);
119
120 if (!_hasPermission(
121 permissionChecker, category, actionId)) {
122
123 return false;
124 }
125
126 categoryId = category.getParentCategoryId();
127 }
128 }
129 catch (NoSuchCategoryException nsce) {
130 if (!category.isInTrash()) {
131 throw nsce;
132 }
133 }
134
135 return true;
136 }
137
138 return _hasPermission(permissionChecker, category, actionId);
139 }
140
141 private static boolean _hasPermission(
142 PermissionChecker permissionChecker, MBCategory category,
143 String actionId) {
144
145 if (permissionChecker.hasOwnerPermission(
146 category.getCompanyId(), MBCategory.class.getName(),
147 category.getCategoryId(), category.getUserId(), actionId) ||
148 permissionChecker.hasPermission(
149 category.getGroupId(), MBCategory.class.getName(),
150 category.getCategoryId(), actionId)) {
151
152 return true;
153 }
154
155 return false;
156 }
157
158 }