From b01e54bf96dab1159fd066bb02fa0b86349b50d7 Mon Sep 17 00:00:00 2001 From: Dmytro Skarzhynets Date: Wed, 4 Nov 2026 13:50:27 +0200 Subject: [PATCH 1/3] fix: keep entity group owner unchanged on update (cherry picked from commit 4341c0b7efe6f2818e2d03af340a210c74110491) --- .../controller/EntityGroupController.java | 11 +-- .../controller/EntityGroupControllerTest.java | 79 +++++++++++++++++++ .../dao/group/BaseEntityGroupService.java | 3 + .../dao/service/EntityGroupServiceTest.java | 33 ++++++++ 4 files changed, 119 insertions(+), 7 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java b/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java index 0813fe6b5d..2146550658 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java +++ b/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()); } diff --git a/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java index 99fa4a51a6..1ae9194b1a 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java +++ b/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(); diff --git a/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java b/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java index 75b51d3442..6bc7fb9697 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java @@ -1211,6 +1211,9 @@ public class BaseEntityGroupService extends AbstractCachedEntityService 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 saveStringAttribute(EntityId entityId, String key, String value, AttributeScope scope) { KvEntry attrValue = new StringDataEntry(key, value); AttributeKvEntry attr = new BaseAttributeKvEntry(attrValue, 42L); From 87efb1784d98814508ef2d340c90cd4924d94cb6 Mon Sep 17 00:00:00 2001 From: Dmytro Skarzhynets Date: Wed, 4 Nov 2026 14:20:04 +0200 Subject: [PATCH 2/3] fix: keep entity group type unchanged on update (cherry picked from commit cf90bdcdf899037f03701c12b8be877d15b4b133) --- .../controller/EntityGroupControllerTest.java | 16 ++++++++++++++++ .../server/dao/group/BaseEntityGroupService.java | 3 +++ .../dao/service/EntityGroupServiceTest.java | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java index 1ae9194b1a..53eeedd183 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java @@ -106,6 +106,22 @@ public class EntityGroupControllerTest extends AbstractControllerTest { assertThat(entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId()).getOwnerId()).isEqualTo(tenantId); } + @Test + public void testShouldNotChangeEntityGroupType() throws Exception { + loginTenantAdmin(); + EntityGroup entityGroup = new EntityGroup(); + entityGroup.setName("Device Group"); + entityGroup.setType(EntityType.DEVICE); + EntityGroup savedEntityGroup = doPost("/api/entityGroup", entityGroup, EntityGroup.class); + + savedEntityGroup.setType(EntityType.USER); + doPost("/api/entityGroup", savedEntityGroup) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString("Entity group type can't be changed!"))); + + assertThat(entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId()).getType()).isEqualTo(EntityType.DEVICE); + } + @Test public void testShouldNotMoveTenantEntityGroupToCustomerAsCustomerAdmin() throws Exception { EntityGroup tenantAdministratorsGroup = entityGroupService.findEntityGroupByTypeAndName(tenantId, tenantId, diff --git a/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java b/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java index 6bc7fb9697..207470c488 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/group/BaseEntityGroupService.java @@ -1214,6 +1214,9 @@ public class BaseEntityGroupService extends AbstractCachedEntityService entityGroupService.saveEntityGroup(tenantId, tenantId, retypedEntityGroup)) + .isInstanceOf(DataValidationException.class) + .hasMessage("Entity group type can't be changed!"); + assertThat(entityGroupService.findEntityGroupById(tenantId, savedEntityGroup.getId()).getType()).isEqualTo(EntityType.DEVICE); + } + @Test public void testShouldUpdateEntityGroupWithSameOwner() { EntityGroup entityGroup = new EntityGroup(); From 15923ae7cd023740bc14e68605aa6576b1fb3cfc Mon Sep 17 00:00:00 2001 From: Dmytro Skarzhynets Date: Wed, 4 Nov 2026 14:41:39 +0200 Subject: [PATCH 3/3] fix: set entity group owner only on create in customer view (cherry picked from commit 9440da3d023cb9c37267106af03e1e60c5e4734e) --- .../modules/home/components/group/entity-groups-table-config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui-ngx/src/app/modules/home/components/group/entity-groups-table-config.ts b/ui-ngx/src/app/modules/home/components/group/entity-groups-table-config.ts index 5ce3fc9abd..be3ffd2313 100644 --- a/ui-ngx/src/app/modules/home/components/group/entity-groups-table-config.ts +++ b/ui-ngx/src/app/modules/home/components/group/entity-groups-table-config.ts @@ -115,7 +115,7 @@ export class EntityGroupsTableConfig extends EntityTableConfig this.saveEntity = (entityGroup, originalEntityGroup) => { entityGroup.type = this.groupType; - if (this.customerId) { + if (this.customerId && !entityGroup.id) { entityGroup.ownerId = { entityType: EntityType.CUSTOMER, id: this.customerId