Browse Source

fixed alarm comment permission bug

pull/15715/head
dashevchenko 6 months ago
parent
commit
2810edca61
  1. 10
      application/src/main/java/org/thingsboard/server/controller/AlarmCommentController.java
  2. 37
      application/src/test/java/org/thingsboard/server/controller/AlarmCommentControllerTest.java

10
application/src/main/java/org/thingsboard/server/controller/AlarmCommentController.java

@ -31,6 +31,7 @@ import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmComment;
import org.thingsboard.server.common.data.alarm.AlarmCommentInfo;
import org.thingsboard.server.common.data.alarm.AlarmCommentType;
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.AlarmCommentId;
import org.thingsboard.server.common.data.id.AlarmId;
@ -77,6 +78,7 @@ public class AlarmCommentController extends BaseController {
checkParameter(ALARM_ID, strAlarmId);
AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
Alarm alarm = checkAlarmInfoId(alarmId, Operation.WRITE);
checkUserCommentOwnership(alarmComment, Operation.WRITE);
alarmComment.setAlarmId(alarmId);
alarmComment.setType(AlarmCommentType.OTHER);
return tbAlarmCommentService.saveAlarmComment(alarm, alarmComment, getCurrentUser());
@ -93,6 +95,7 @@ public class AlarmCommentController extends BaseController {
AlarmCommentId alarmCommentId = new AlarmCommentId(toUUID(strCommentId));
AlarmComment alarmComment = checkAlarmCommentId(alarmCommentId, alarmId);
checkUserCommentOwnership(alarmComment, Operation.DELETE);
tbAlarmCommentService.deleteAlarmComment(alarm, alarmComment, getCurrentUser());
}
@ -120,4 +123,11 @@ public class AlarmCommentController extends BaseController {
return checkNotNull(alarmCommentService.findAlarmComments(alarm.getTenantId(), alarmId, pageLink));
}
private void checkUserCommentOwnership(AlarmComment alarmComment, Operation operation) throws ThingsboardException {
if (alarmComment.getUserId() != null && !alarmComment.getUserId().equals(getCurrentUser().getId())) {
throw new ThingsboardException("User is not allowed to " + operation.name().toLowerCase() + " other user's comment",
ThingsboardErrorCode.PERMISSION_DENIED);
}
}
}

37
application/src/test/java/org/thingsboard/server/controller/AlarmCommentControllerTest.java

@ -160,6 +160,26 @@ public class AlarmCommentControllerTest extends AbstractControllerTest {
testLogEntityActionEntityEqClass(alarm, alarm.getId(), tenantId, customerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.UPDATED_COMMENT, 1, updatedAlarmComment);
}
@Test
public void testUpdateOthersAlarmCommentByTenantAdmin() throws Exception {
// Tenant admins are NOT exempt from the ownership rule — even with full tenant-level
// privileges they cannot rewrite a comment authored by a different user.
loginCustomerUser();
AlarmComment alarmComment = createAlarmComment(alarm.getId());
loginTenantAdmin();
Mockito.reset(tbClusterService, auditLogService);
JsonNode newComment = JacksonUtil.newObjectNode().set("text", new TextNode("Tenant rewrite attempt"));
alarmComment.setComment(newComment);
doPost("/api/alarm/" + alarm.getId() + "/comment", alarmComment)
.andExpect(status().isForbidden())
.andExpect(statusReason(containsString("User is not allowed to write other user's comment")));
testNotifyEntityNever(alarm.getId(), alarmComment);
}
@Test
public void testUpdateAlarmViaDifferentTenant() throws Exception {
loginTenantAdmin();
@ -215,6 +235,23 @@ public class AlarmCommentControllerTest extends AbstractControllerTest {
testLogEntityActionEntityEqClass(alarm, alarm.getId(), tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.DELETED_COMMENT, 1, expectedAlarmComment);
}
@Test
public void testDeleteOthersAlarmCommentByTenantAdmin() throws Exception {
// Tenant admins are NOT exempt from the ownership rule on delete either — even with full
// tenant-level privileges they cannot delete a comment authored by a different user.
loginCustomerUser();
AlarmComment alarmComment = createAlarmComment(alarm.getId());
loginTenantAdmin();
Mockito.reset(tbClusterService, auditLogService);
doDelete("/api/alarm/" + alarm.getId() + "/comment/" + alarmComment.getId())
.andExpect(status().isForbidden())
.andExpect(statusReason(containsString("User is not allowed to delete other user's comment")));
testNotifyEntityNever(alarm.getId(), alarmComment);
}
@Test
public void testDeleteAlarmViaTenant() throws Exception {
loginTenantAdmin();

Loading…
Cancel
Save