diff --git a/application/src/main/java/org/thingsboard/server/controller/BaseController.java b/application/src/main/java/org/thingsboard/server/controller/BaseController.java index 13a122ed38..ab1ffaafe9 100644 --- a/application/src/main/java/org/thingsboard/server/controller/BaseController.java +++ b/application/src/main/java/org/thingsboard/server/controller/BaseController.java @@ -1244,7 +1244,7 @@ public abstract class BaseController { protected EntityGroupInfo checkEntityGroupId(EntityGroupId entityGroupId, Operation operation) throws ThingsboardException { try { validateId(entityGroupId, id -> "Incorrect entityGroupId " + id); - EntityGroupInfo entityGroup = entityGroupService.findEntityGroupInfoById(getTenantId(), entityGroupId); + EntityGroupInfo entityGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), entityGroupId); checkNotNull(entityGroup, "Entity group with id [" + entityGroupId + "] is not found"); accessControlService.checkEntityGroupInfoPermission(getCurrentUser(), operation, entityGroup); return entityGroup; 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 2146550658..179365bb6b 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EntityGroupController.java @@ -964,7 +964,11 @@ public class EntityGroupController extends AutoCommitController { for (String strEntityGroupId : strEntityGroupIds) { entityGroupIds.add(new EntityGroupId(toUUID(strEntityGroupId))); } - return checkNotNull(entityGroupService.findEntityGroupEntityInfosByIds(tenantId, entityGroupIds, new PageLink(Integer.MAX_VALUE)).getData()); + List entityGroups = checkNotNull( + entityGroupService.findEntityGroupInfosByIds(tenantId, entityGroupIds, new PageLink(Integer.MAX_VALUE)).getData()); + return filterEntityGroupsByReadPermission(entityGroups).stream() + .map(entityGroup -> new EntityInfo(entityGroup.getId(), entityGroup.getName())) + .toList(); } @ApiOperation(value = "Get Owners (getOwners)", @@ -1170,7 +1174,8 @@ public class EntityGroupController extends AutoCommitController { throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); } } else { - userGroup = entityGroupService.findEntityGroupInfoById(getTenantId(), shareGroupRequest.getUserGroupId()); + userGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), shareGroupRequest.getUserGroupId()); + checkNotNull(userGroup, "Entity group with id [" + shareGroupRequest.getUserGroupId() + "] is not found"); } accessControlService.checkEntityGroupInfoPermission(getCurrentUser(), Operation.WRITE, userGroup); @@ -1243,7 +1248,7 @@ public class EntityGroupController extends AutoCommitController { checkParameter("userGroupId", strUserGroupId); checkParameter("roleId", strRoleId); EntityGroupId userGroupId = new EntityGroupId(toUUID(strUserGroupId)); - EntityGroup userGroup = entityGroupService.findEntityGroupById(getTenantId(), userGroupId); + EntityGroup userGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), userGroupId); if (userGroup == null) { throw new ThingsboardException("User group with requested id: " + userGroupId + " wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); } @@ -1296,7 +1301,7 @@ public class EntityGroupController extends AutoCommitController { private EntityGroup resolveEntityGroup(UUID entityGroupUuid) throws ThingsboardException { EntityGroupId entityGroupId = new EntityGroupId(entityGroupUuid); - EntityGroup entityGroup = entityGroupService.findEntityGroupById(getTenantId(), entityGroupId); + EntityGroup entityGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), entityGroupId); if (entityGroup == null) { throw new ThingsboardException("Entity group with requested id: " + entityGroupId + " wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); } @@ -1314,7 +1319,7 @@ public class EntityGroupController extends AutoCommitController { throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); } } else { - userGroup = entityGroupService.findEntityGroupInfoById(getTenantId(), shareGroupRequest.getUserGroupId()); + userGroup = entityGroupService.findEntityGroupInfoByTenantIdAndId(getTenantId(), shareGroupRequest.getUserGroupId()); } if (userGroup == null) { throw new ThingsboardException("Specified user group wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND); diff --git a/application/src/main/java/org/thingsboard/server/service/security/AccessValidator.java b/application/src/main/java/org/thingsboard/server/service/security/AccessValidator.java index 8a7129668a..f8652a1a53 100644 --- a/application/src/main/java/org/thingsboard/server/service/security/AccessValidator.java +++ b/application/src/main/java/org/thingsboard/server/service/security/AccessValidator.java @@ -642,7 +642,7 @@ public class AccessValidator { if (currentUser.isSystemAdmin()) { callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); } else { - ListenableFuture entityGroupFuture = entityGroupService.findEntityGroupInfoByIdAsync(currentUser.getTenantId(), new EntityGroupId(entityId.getId())); + ListenableFuture entityGroupFuture = entityGroupService.findEntityGroupInfoByTenantIdAndIdAsync(currentUser.getTenantId(), new EntityGroupId(entityId.getId())); Futures.addCallback(entityGroupFuture, getCallback(callback, entityGroup -> { if (entityGroup == null) { return ValidationResult.entityNotFound("Entity group with requested id wasn't found!"); 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 53eeedd183..71914b0442 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EntityGroupControllerTest.java @@ -12,6 +12,7 @@ import org.testcontainers.shaded.org.apache.commons.lang3.RandomStringUtils; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.data.EntityInfo; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.User; @@ -23,6 +24,7 @@ import org.thingsboard.server.common.data.id.IdBased; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.permission.GroupPermissionInfo; +import org.thingsboard.server.common.data.permission.Operation; import org.thingsboard.server.common.data.permission.ShareGroupRequest; import org.thingsboard.server.common.data.role.Role; import org.thingsboard.server.common.data.role.RoleType; @@ -38,6 +40,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.not; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @DaoSqlTest @@ -167,11 +170,131 @@ public class EntityGroupControllerTest extends AbstractControllerTest { loginCustomerAdminUser(); otherTenantEntityGroup.setOwnerId(customerId); doPost("/api/entityGroup", otherTenantEntityGroup) - .andExpect(status().isForbidden()); + .andExpect(status().isNotFound()); assertThat(entityGroupService.findEntityGroupById(differentTenantId, otherTenantEntityGroup.getId()).getOwnerId()).isEqualTo(differentTenantId); } + @Test + public void testGetEntityGroupInfosByIdsReturnsOnlyOwnTenantGroups() throws Exception { + loginTenantAdmin(); + EntityGroup ownGroup = new EntityGroup(); + ownGroup.setName("Own Device Group"); + ownGroup.setType(EntityType.DEVICE); + ownGroup = doPost("/api/entityGroup", ownGroup, EntityGroup.class); + + loginDifferentTenant(); + EntityGroup otherTenantGroup = new EntityGroup(); + otherTenantGroup.setName("Other Tenant Device Group"); + otherTenantGroup.setType(EntityType.DEVICE); + otherTenantGroup = doPost("/api/entityGroup", otherTenantGroup, EntityGroup.class); + + loginTenantAdmin(); + List entityInfos = doGetTyped("/api/entityGroupInfos?entityGroupIds=" + ownGroup.getId().getId() + "," + otherTenantGroup.getId().getId(), + new TypeReference<>() {}); + assertThat(entityInfos).extracting(EntityInfo::getId).containsExactly(ownGroup.getId()); + } + + @Test + public void testGetEntityGroupInfosByIdsReturnsOnlyReadableGroups() throws Exception { + loginTenantAdmin(); + EntityGroup tenantGroup = new EntityGroup(); + tenantGroup.setName("Tenant Device Group"); + tenantGroup.setType(EntityType.DEVICE); + tenantGroup = doPost("/api/entityGroup", tenantGroup, EntityGroup.class); + + loginCustomerAdminUser(); + EntityGroup customerGroup = new EntityGroup(); + customerGroup.setName("Customer Device Group"); + customerGroup.setType(EntityType.DEVICE); + customerGroup.setOwnerId(customerId); + customerGroup = doPost("/api/entityGroup", customerGroup, EntityGroup.class); + + List entityInfos = doGetTyped("/api/entityGroupInfos?entityGroupIds=" + customerGroup.getId().getId() + "," + tenantGroup.getId().getId(), + new TypeReference<>() {}); + assertThat(entityInfos).extracting(EntityInfo::getId).containsExactly(customerGroup.getId()); + } + + @Test + public void testShareGroupWithUserGroupOfOtherTenantReturnsNotFound() throws Exception { + loginDifferentTenant(); + EntityGroup otherTenantUserGroup = new EntityGroup(); + otherTenantUserGroup.setName("Other Tenant User Group"); + otherTenantUserGroup.setType(EntityType.USER); + otherTenantUserGroup = doPost("/api/entityGroup", otherTenantUserGroup, EntityGroup.class); + + loginTenantAdmin(); + EntityGroup deviceGroup = new EntityGroup(); + deviceGroup.setName("Shared Device Group"); + deviceGroup.setType(EntityType.DEVICE); + deviceGroup = doPost("/api/entityGroup", deviceGroup, EntityGroup.class); + + ShareGroupRequest shareGroupRequest = new ShareGroupRequest(tenantId, false, otherTenantUserGroup.getId(), true, null); + doPost("/api/entityGroup/" + deviceGroup.getId().getId() + "/share", shareGroupRequest) + .andExpect(status().isNotFound()) + .andExpect(statusReason(not(containsString(otherTenantUserGroup.getName())))); + } + + @Test + public void testShareGroupToChildOwnerUserGroupWithGroupsOfOtherTenantReturnsNotFound() throws Exception { + loginDifferentTenant(); + EntityGroup otherTenantUserGroup = new EntityGroup(); + otherTenantUserGroup.setName("Other Tenant User Group"); + otherTenantUserGroup.setType(EntityType.USER); + otherTenantUserGroup = doPost("/api/entityGroup", otherTenantUserGroup, EntityGroup.class); + EntityGroup otherTenantDeviceGroup = new EntityGroup(); + otherTenantDeviceGroup.setName("Other Tenant Device Group"); + otherTenantDeviceGroup.setType(EntityType.DEVICE); + otherTenantDeviceGroup = doPost("/api/entityGroup", otherTenantDeviceGroup, EntityGroup.class); + + loginTenantAdmin(); + EntityGroup deviceGroup = new EntityGroup(); + deviceGroup.setName("Shared Device Group"); + deviceGroup.setType(EntityType.DEVICE); + deviceGroup = doPost("/api/entityGroup", deviceGroup, EntityGroup.class); + EntityGroup userGroup = new EntityGroup(); + userGroup.setName("Own User Group"); + userGroup.setType(EntityType.USER); + userGroup = doPost("/api/entityGroup", userGroup, EntityGroup.class); + Role role = new Role(); + role.setName("Read Group Role"); + role.setType(RoleType.GROUP); + role.setPermissions(JacksonUtil.valueToTree(List.of(Operation.READ))); + role = doPost("/api/role", role, Role.class); + + doPost("/api/entityGroup/" + deviceGroup.getId().getId() + "/" + otherTenantUserGroup.getId().getId() + "/" + role.getId().getId() + "/share") + .andExpect(status().isNotFound()); + doPost("/api/entityGroup/" + otherTenantDeviceGroup.getId().getId() + "/" + userGroup.getId().getId() + "/" + role.getId().getId() + "/share") + .andExpect(status().isNotFound()); + } + + @Test + public void testGetAttributesOfEntityGroupOfOtherTenantReturnsNotFound() throws Exception { + loginDifferentTenant(); + EntityGroup otherTenantGroup = new EntityGroup(); + otherTenantGroup.setName("Other Tenant Asset Group"); + otherTenantGroup.setType(EntityType.ASSET); + otherTenantGroup = doPost("/api/entityGroup", otherTenantGroup, EntityGroup.class); + + loginTenantAdmin(); + doGetAsync("/api/plugins/telemetry/ENTITY_GROUP/" + otherTenantGroup.getId().getId() + "/values/attributes") + .andExpect(status().isNotFound()); + } + + @Test + public void testGetEntityGroupOfOtherTenantReturnsNotFound() throws Exception { + loginDifferentTenant(); + EntityGroup otherTenantGroup = new EntityGroup(); + otherTenantGroup.setName("Other Tenant Asset Group"); + otherTenantGroup.setType(EntityType.ASSET); + otherTenantGroup = doPost("/api/entityGroup", otherTenantGroup, EntityGroup.class); + + loginTenantAdmin(); + doGet("/api/entityGroup/" + otherTenantGroup.getId().getId()) + .andExpect(status().isNotFound()) + .andExpect(statusReason(not(containsString(otherTenantGroup.getName())))); + } + @Test public void testShouldNotUpdateNonExistingEntityGroup() throws Exception { loginTenantAdmin();