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