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 else {
076 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
077 categoryId);
078
079 return contains(permissionChecker, category, actionId);
080 }
081 }
082
083 public static boolean contains(
084 PermissionChecker permissionChecker, long categoryId,
085 String actionId)
086 throws PortalException, SystemException {
087
088 MBCategory category = MBCategoryLocalServiceUtil.getCategory(
089 categoryId);
090
091 return contains(permissionChecker, category, actionId);
092 }
093
094 public static boolean contains(
095 PermissionChecker permissionChecker, MBCategory category,
096 String actionId)
097 throws PortalException, SystemException {
098
099 if (actionId.equals(ActionKeys.ADD_CATEGORY)) {
100 actionId = ActionKeys.ADD_SUBCATEGORY;
101 }
102
103 if (MBBanLocalServiceUtil.hasBan(
104 category.getGroupId(), permissionChecker.getUserId())) {
105
106 return false;
107 }
108
109 if (actionId.equals(ActionKeys.VIEW) &&
110 PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
111
112 try {
113 long categoryId = category.getCategoryId();
114
115 while (categoryId !=
116 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {
117
118 category = MBCategoryLocalServiceUtil.getCategory(
119 categoryId);
120
121 if (!_hasPermission(
122 permissionChecker, category, actionId)) {
123
124 return false;
125 }
126
127 categoryId = category.getParentCategoryId();
128 }
129 }
130 catch (NoSuchCategoryException nsce) {
131 if (!category.isInTrash()) {
132 throw nsce;
133 }
134 }
135
136 return true;
137 }
138
139 return _hasPermission(permissionChecker, category, actionId);
140 }
141
142 private static boolean _hasPermission(
143 PermissionChecker permissionChecker, MBCategory category,
144 String actionId) {
145
146 if (permissionChecker.hasOwnerPermission(
147 category.getCompanyId(), MBCategory.class.getName(),
148 category.getCategoryId(), category.getUserId(), actionId) ||
149 permissionChecker.hasPermission(
150 category.getGroupId(), MBCategory.class.getName(),
151 category.getCategoryId(), actionId)) {
152
153 return true;
154 }
155
156 return false;
157 }
158
159 }