Browse Source

Treat the All users group of another tenant as not found when sharing a group

pull/16844/head
Viacheslav Klimov 3 weeks ago
parent
commit
653b2a76db
Failed to extract signature
  1. 12
      application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java
  2. 66
      application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java

12
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<EntityGroupInfo> userGroupOptional = entityGroupService.findEntityGroupInfoByTypeAndName(getTenantId(), shareGroupRequest.getOwnerId(),
EntityType.USER, EntityGroup.GROUP_ALL_NAME);
Optional<EntityGroupInfo> 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<EntityGroupInfo> userGroupOptional = entityGroupService.findEntityGroupInfoByTypeAndName(getTenantId(), shareGroupRequest.getOwnerId(),
EntityType.USER, EntityGroup.GROUP_ALL_NAME);
Optional<EntityGroupInfo> userGroupOptional = findAllUserGroup(shareGroupRequest.getOwnerId());
if (userGroupOptional.isPresent()) {
userGroup = userGroupOptional.get();
} else {
@ -1333,6 +1331,12 @@ public class EntityGroupController extends AutoCommitController {
return userGroup;
}
private Optional<EntityGroupInfo> 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<Role> resolveRoles(ShareGroupRequest shareGroupRequest) throws ThingsboardException {
List<Role> roles = new ArrayList<>(1);
if (shareGroupRequest.getRoleIds() != null && !shareGroupRequest.getRoleIds().isEmpty()) {

66
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<EntityGroupId> findSharedUserGroupIds(EntityGroup entityGroup) {
return groupPermissionService.findGroupPermissionByTenantIdAndEntityGroupId(tenantId, entityGroup.getId(), new PageLink(100))
.getData().stream().map(GroupPermission::getUserGroupId).collect(Collectors.toList());
}
}

Loading…
Cancel
Save