Browse Source

fix: keep entity group owner unchanged on update

(cherry picked from commit 4341c0b7efe6f2818e2d03af340a210c74110491)
pull/16844/head
Dmytro Skarzhynets 1 month ago
committed by Viacheslav Klimov
parent
commit
b01e54bf96
Failed to extract signature
  1. 11
      application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java
  2. 79
      application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java
  3. 3
      dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java
  4. 33
      dao/src/test/java/org/thingsboard/server/dao/service/EntityGroupServiceTest.java

11
application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java

@ -210,9 +210,8 @@ public class EntityGroupController extends AutoCommitController {
@RequestBody EntityGroup entityGroup) throws Exception {
SecurityUser currentUser = getCurrentUser();
checkEntityGroupType(entityGroup.getType());
Operation operation = entityGroup.getId() == null ? Operation.CREATE : Operation.WRITE;
EntityId parentEntityId = entityGroup.getOwnerId();
if (operation == Operation.CREATE) {
if (entityGroup.getId() == null) {
if (parentEntityId == null || parentEntityId.isNullUid()) {
parentEntityId = currentUser.getOwnerId();
} else {
@ -221,14 +220,12 @@ public class EntityGroupController extends AutoCommitController {
"Invalid entity group ownerId!", ThingsboardErrorCode.PERMISSION_DENIED);
}
}
EntityGroupInfo entityGroupInfo = new EntityGroupInfo(entityGroup, ownersCacheService.fetchOwnersHierarchy(getTenantId(), parentEntityId));
accessControlService.checkEntityGroupInfoPermission(currentUser, Operation.CREATE, entityGroupInfo);
} else {
validateEntityId(parentEntityId, id -> "Incorrect entity group ownerId " + id);
parentEntityId = checkEntityGroupId(entityGroup.getId(), Operation.WRITE).getOwnerId();
}
EntityGroupInfo entityGroupInfo = new EntityGroupInfo(entityGroup, ownersCacheService.fetchOwnersHierarchy(getTenantId(), parentEntityId));
accessControlService.checkEntityGroupInfoPermission(currentUser, operation, entityGroupInfo);
return tbEntityGroupService.save(getTenantId(), parentEntityId, entityGroup, getCurrentUser());
}

79
application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java

@ -33,6 +33,7 @@ import org.thingsboard.server.dao.service.DaoSqlTest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
@ -89,6 +90,84 @@ public class EntityGroupControllerTest extends AbstractControllerTest {
Assert.assertEquals(savedEntityGroup.getName(), foundEntityGroup.getName());
}
@Test
public void testShouldNotChangeEntityGroupOwnerAsGroupOwner() throws Exception {
loginTenantAdmin();
EntityGroup entityGroup = new EntityGroup();
entityGroup.setName("Device Group");
entityGroup.setType(EntityType.DEVICE);
EntityGroup savedEntityGroup = doPost("/api/entityGroup", entityGroup, EntityGroup.class);
savedEntityGroup.setOwnerId(customerId);
doPost("/api/entityGroup", savedEntityGroup)
.andExpect(status().isBadRequest())
.andExpect(statusReason(containsString("Entity group owner can't be changed!")));
assertThat(entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId()).getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testShouldNotMoveTenantEntityGroupToCustomerAsCustomerAdmin() throws Exception {
EntityGroup tenantAdministratorsGroup = entityGroupService.findEntityGroupByTypeAndName(tenantId, tenantId,
EntityType.USER, EntityGroup.GROUP_TENANT_ADMINS_NAME).get();
loginCustomerAdminUser();
EntityGroup movedEntityGroup = new EntityGroup(tenantAdministratorsGroup);
movedEntityGroup.setOwnerId(customerId);
doPost("/api/entityGroup", movedEntityGroup)
.andExpect(status().isForbidden());
assertThat(entityGroupService.findEntityGroupById(tenantId, tenantAdministratorsGroup.getId()).getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testShouldNotMoveEntityGroupSharedWithWriteAccess() throws Exception {
loginTenantAdmin();
EntityGroup entityGroup = new EntityGroup();
entityGroup.setName("Shared Device Group");
entityGroup.setType(EntityType.DEVICE);
EntityGroup savedEntityGroup = doPost("/api/entityGroup", entityGroup, EntityGroup.class);
var shareGroupRequest = new ShareGroupRequest(customerId, true, null, false, Collections.emptyList());
doPost("/api/entityGroup/{entityGroupId}/share", shareGroupRequest, savedEntityGroup.getId().toString())
.andExpect(status().isOk());
loginCustomerAdminUser();
EntityGroup sharedEntityGroup = doGet("/api/entityGroup/" + savedEntityGroup.getId().getId(), EntityGroup.class);
sharedEntityGroup.setOwnerId(customerId);
doPost("/api/entityGroup", sharedEntityGroup)
.andExpect(status().isForbidden());
assertThat(entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId()).getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testShouldNotMoveOtherTenantEntityGroup() throws Exception {
loginDifferentTenant();
EntityGroup entityGroup = new EntityGroup();
entityGroup.setName("Other Tenant Device Group");
entityGroup.setType(EntityType.DEVICE);
EntityGroup otherTenantEntityGroup = doPost("/api/entityGroup", entityGroup, EntityGroup.class);
loginCustomerAdminUser();
otherTenantEntityGroup.setOwnerId(customerId);
doPost("/api/entityGroup", otherTenantEntityGroup)
.andExpect(status().isForbidden());
assertThat(entityGroupService.findEntityGroupById(differentTenantId, otherTenantEntityGroup.getId()).getOwnerId()).isEqualTo(differentTenantId);
}
@Test
public void testShouldNotUpdateNonExistingEntityGroup() throws Exception {
loginTenantAdmin();
EntityGroup entityGroup = new EntityGroup();
entityGroup.setId(new EntityGroupId(UUID.randomUUID()));
entityGroup.setName("Non-existing Device Group");
entityGroup.setType(EntityType.DEVICE);
entityGroup.setOwnerId(tenantId);
doPost("/api/entityGroup", entityGroup)
.andExpect(status().isNotFound());
}
@Test
public void testSaveEntityGroupWithSameName() throws Exception {
EntityGroup entityGroup = new EntityGroup();

3
dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java

@ -1211,6 +1211,9 @@ public class BaseEntityGroupService extends AbstractCachedEntityService<EntityGr
if (old == null) {
throw new DataValidationException("Can`t update non existing entity group!");
}
if (!data.getOwnerId().equals(old.getOwnerId())) {
throw new DataValidationException("Entity group owner can't be changed!");
}
return old;
}

33
dao/src/test/java/org/thingsboard/server/dao/service/EntityGroupServiceTest.java

@ -515,6 +515,39 @@ public class EntityGroupServiceTest extends AbstractServiceTest {
.hasMessage("Entity '" + entityGroupId.getEntityType() + "' is not a group entity!");
}
@Test
public void testShouldNotChangeEntityGroupOwnerOnUpdate() {
EntityGroup entityGroup = new EntityGroup();
entityGroup.setType(EntityType.DEVICE);
entityGroup.setName("Device Group");
EntityGroup savedEntityGroup = entityGroupService.saveEntityGroup(tenantId, tenantId, entityGroup);
CustomerId customerId = new CustomerId(UUID.randomUUID());
EntityGroup movedEntityGroup = new EntityGroup(savedEntityGroup);
movedEntityGroup.setOwnerId(customerId);
assertThatThrownBy(() -> entityGroupService.saveEntityGroup(tenantId, customerId, movedEntityGroup))
.isInstanceOf(DataValidationException.class)
.hasMessage("Entity group owner can't be changed!");
assertThat(entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId()).getOwnerId()).isEqualTo(tenantId);
}
@Test
public void testShouldUpdateEntityGroupWithSameOwner() {
EntityGroup entityGroup = new EntityGroup();
entityGroup.setType(EntityType.DEVICE);
entityGroup.setName("Device Group");
EntityGroup savedEntityGroup = entityGroupService.saveEntityGroup(tenantId, tenantId, entityGroup);
EntityGroup renamedEntityGroup = new EntityGroup(savedEntityGroup);
renamedEntityGroup.setName("Renamed Device Group");
entityGroupService.saveEntityGroup(tenantId, tenantId, renamedEntityGroup);
EntityGroup foundEntityGroup = entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId());
assertThat(foundEntityGroup.getName()).isEqualTo("Renamed Device Group");
assertThat(foundEntityGroup.getOwnerId()).isEqualTo(tenantId);
}
private ListenableFuture<AttributesSaveResult> saveStringAttribute(EntityId entityId, String key, String value, AttributeScope scope) {
KvEntry attrValue = new StringDataEntry(key, value);
AttributeKvEntry attr = new BaseAttributeKvEntry(attrValue, 42L);

Loading…
Cancel
Save