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.workflow.permission.WorkflowPermissionUtil;
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.PropsValues;
024 import com.liferay.portlet.messageboards.NoSuchCategoryException;
025 import com.liferay.portlet.messageboards.model.MBCategory;
026 import com.liferay.portlet.messageboards.model.MBCategoryConstants;
027 import com.liferay.portlet.messageboards.model.MBMessage;
028 import com.liferay.portlet.messageboards.service.MBBanLocalServiceUtil;
029 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
030 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
031
032
035 public class MBMessagePermission {
036
037 public static void check(
038 PermissionChecker permissionChecker, long messageId,
039 String actionId)
040 throws PortalException, SystemException {
041
042 if (!contains(permissionChecker, messageId, actionId)) {
043 throw new PrincipalException();
044 }
045 }
046
047 public static void check(
048 PermissionChecker permissionChecker, MBMessage message,
049 String actionId)
050 throws PortalException, SystemException {
051
052 if (!contains(permissionChecker, message, actionId)) {
053 throw new PrincipalException();
054 }
055 }
056
057 public static boolean contains(
058 PermissionChecker permissionChecker, long messageId,
059 String actionId)
060 throws PortalException, SystemException {
061
062 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
063
064 return contains(permissionChecker, message, actionId);
065 }
066
067 public static boolean contains(
068 PermissionChecker permissionChecker, MBMessage message,
069 String actionId)
070 throws PortalException, SystemException {
071
072 long groupId = message.getGroupId();
073
074 if (message.isPending()) {
075 Boolean hasPermission = WorkflowPermissionUtil.hasPermission(
076 permissionChecker, message.getGroupId(),
077 message.getWorkflowClassName(), message.getMessageId(),
078 actionId);
079
080 if (hasPermission != null) {
081 return hasPermission.booleanValue();
082 }
083 }
084
085 if (MBBanLocalServiceUtil.hasBan(
086 groupId, permissionChecker.getUserId())) {
087
088 return false;
089 }
090
091 if (actionId.equals(ActionKeys.VIEW) &&
092 PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
093
094 long categoryId = message.getCategoryId();
095
096 if ((categoryId !=
097 MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) &&
098 (categoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
099
100 try {
101 MBCategory category =
102 MBCategoryLocalServiceUtil.getCategory(categoryId);
103
104 if (!MBCategoryPermission.contains(
105 permissionChecker, category, actionId)) {
106
107 return false;
108 }
109 }
110 catch (NoSuchCategoryException nsce) {
111 if (!message.isInTrashThread()) {
112 throw nsce;
113 }
114 }
115 }
116 }
117
118 if (permissionChecker.hasOwnerPermission(
119 message.getCompanyId(), MBMessage.class.getName(),
120 message.getRootMessageId(), message.getUserId(), actionId)) {
121
122 return true;
123 }
124
125 return permissionChecker.hasPermission(
126 groupId, MBMessage.class.getName(), message.getMessageId(),
127 actionId);
128 }
129
130 }