From 653b2a76db7388dc449ccffe31e3863099cbd843 Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Fri, 13 Nov 2026 11:57:52 +0200 Subject: [PATCH] Treat the All users group of another tenant as not found when sharing a group --- .../controller/EntityGroupController.java | 12 ++-- .../controller/EntityGroupControllerTest.java | 66 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 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 9e0f5c582a..40499b2d2f 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java @@ -1168,8 +1168,7 @@ public class EntityGroupController extends AutoCommitController { EntityGroupInfo userGroup; if (shareGroupRequest.isAllUserGroup()) { - Optional userGroupOptional = entityGroupService.findEntityGroupInfoByTypeAndName(getTenantId(), shareGroupRequest.getOwnerId(), - EntityType.USER, EntityGroup.GROUP_ALL_NAME); + Optional userGroupOptional = findAllUserGroup(shareGroupRequest.getOwnerId()); if (userGroupOptional.isPresent()) { userGroup = userGroupOptional.get(); } else { @@ -1317,8 +1316,7 @@ public class EntityGroupController extends AutoCommitController { private EntityGroupInfo resolveUserGroup(ShareGroupRequest shareGroupRequest) throws ThingsboardException { EntityGroupInfo userGroup; if (shareGroupRequest.isAllUserGroup()) { - Optional userGroupOptional = entityGroupService.findEntityGroupInfoByTypeAndName(getTenantId(), shareGroupRequest.getOwnerId(), - EntityType.USER, EntityGroup.GROUP_ALL_NAME); + Optional userGroupOptional = findAllUserGroup(shareGroupRequest.getOwnerId()); if (userGroupOptional.isPresent()) { userGroup = userGroupOptional.get(); } else { @@ -1333,6 +1331,12 @@ public class EntityGroupController extends AutoCommitController { return userGroup; } + private Optional findAllUserGroup(EntityId ownerId) throws ThingsboardException { + TenantId tenantId = getTenantId(); + return entityGroupService.findEntityGroupInfoByTypeAndName(tenantId, ownerId, EntityType.USER, EntityGroup.GROUP_ALL_NAME) + .map(group -> entityGroupService.findEntityGroupInfoByTenantIdAndId(tenantId, group.getId())); + } + private List resolveRoles(ShareGroupRequest shareGroupRequest) throws ThingsboardException { List roles = new ArrayList<>(1); if (shareGroupRequest.getRoleIds() != null && !shareGroupRequest.getRoleIds().isEmpty()) { 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 c14f97b2f8..23be30acc4 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java @@ -47,6 +47,7 @@ import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -268,6 +269,66 @@ public class EntityGroupControllerTest extends AbstractControllerTest { .andExpect(statusReason(not(containsString(otherTenantUserGroup.getName())))); } + @Test + public void testShareGroupWithAllUsersGroupOfOtherTenantReturnsNotFound() throws Exception { + loginDifferentTenant(); + loginTenantAdmin(); + EntityGroup deviceGroup = createDeviceGroup("Shared Device Group", tenantId); + + ShareGroupRequest unknownOwnerRequest = new ShareGroupRequest(TenantId.fromUUID(UUID.randomUUID()), true, null, true, null); + doPost("/api/entityGroup/{entityGroupId}/share", unknownOwnerRequest, deviceGroup.getId().toString()) + .andExpect(status().isNotFound()) + .andExpect(statusReason(is(msgErrorNotFound))); + ShareGroupRequest otherTenantRequest = new ShareGroupRequest(differentTenantId, true, null, true, null); + doPost("/api/entityGroup/{entityGroupId}/share", otherTenantRequest, deviceGroup.getId().toString()) + .andExpect(status().isNotFound()) + .andExpect(statusReason(is(msgErrorNotFound))); + assertThat(findSharedUserGroupIds(deviceGroup)).isEmpty(); + } + + @Test + public void testShareGroupWithAllUsersGroupOfOwnCustomerSharesWithThatGroup() throws Exception { + loginTenantAdmin(); + EntityGroup deviceGroup = createDeviceGroup("Shared Device Group", tenantId); + EntityGroup customerAllUsersGroup = entityGroupService.findEntityGroupByTypeAndName(tenantId, customerId, + EntityType.USER, EntityGroup.GROUP_ALL_NAME).get(); + + ShareGroupRequest shareGroupRequest = new ShareGroupRequest(customerId, true, null, true, null); + doPost("/api/entityGroup/{entityGroupId}/share", shareGroupRequest, deviceGroup.getId().toString()) + .andExpect(status().isOk()); + assertThat(findSharedUserGroupIds(deviceGroup)).containsExactly(customerAllUsersGroup.getId()); + } + + @Test + public void testShareGroupV2WithAllUsersGroupOfOtherTenantReturnsNotFound() throws Exception { + loginDifferentTenant(); + loginTenantAdmin(); + EntityGroup deviceGroup = createDeviceGroup("Shared Device Group", tenantId); + + ShareGroupRequest unknownOwnerRequest = new ShareGroupRequest(TenantId.fromUUID(UUID.randomUUID()), true, null, true, null); + doPost("/api/v2/entityGroup/{entityGroupId}/share", unknownOwnerRequest, deviceGroup.getId().toString()) + .andExpect(status().isNotFound()) + .andExpect(statusReason(is(msgErrorNotFound))); + ShareGroupRequest otherTenantRequest = new ShareGroupRequest(differentTenantId, true, null, true, null); + doPost("/api/v2/entityGroup/{entityGroupId}/share", otherTenantRequest, deviceGroup.getId().toString()) + .andExpect(status().isNotFound()) + .andExpect(statusReason(is(msgErrorNotFound))); + assertThat(findSharedUserGroupIds(deviceGroup)).isEmpty(); + } + + @Test + public void testShareGroupV2WithAllUsersGroupOfOwnCustomerSharesWithThatGroup() throws Exception { + loginTenantAdmin(); + EntityGroup deviceGroup = createDeviceGroup("Shared Device Group", tenantId); + EntityGroup customerAllUsersGroup = entityGroupService.findEntityGroupByTypeAndName(tenantId, customerId, + EntityType.USER, EntityGroup.GROUP_ALL_NAME).get(); + + ShareGroupRequest shareGroupRequest = new ShareGroupRequest(customerId, true, null, true, null); + doPost("/api/v2/entityGroup/{entityGroupId}/share", shareGroupRequest, deviceGroup.getId().toString()) + .andExpect(status().isOk()); + assertThat(findSharedUserGroupIds(deviceGroup)).containsExactly(customerAllUsersGroup.getId()); + } + @Test public void testShareGroupToChildOwnerUserGroupWithGroupsOfOtherTenantReturnsNotFound() throws Exception { loginDifferentTenant(); @@ -720,4 +781,9 @@ public class EntityGroupControllerTest extends AbstractControllerTest { .getData().stream().map(GroupPermission::getRoleId).collect(Collectors.toList()); } + private List findSharedUserGroupIds(EntityGroup entityGroup) { + return groupPermissionService.findGroupPermissionByTenantIdAndEntityGroupId(tenantId, entityGroup.getId(), new PageLink(100)) + .getData().stream().map(GroupPermission::getUserGroupId).collect(Collectors.toList()); + } + }