Skip to content

Commit

Permalink
Merge pull request #310 from bci-oss/feature/287-extend-dtr-apis-for-…
Browse files Browse the repository at this point in the history
…access-management__06-clean-up

feat: Extend DTR APIs for Access management - Clean-up
  • Loading branch information
tunacicek authored Feb 12, 2024
2 parents b10dff2 + 93b3d46 commit 27e1ba6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ShellIdentifierRepository extends JpaRepository<ShellIdentifier, UUID> {

Expand All @@ -50,6 +51,52 @@ sid.shellId.id IN (
GROUP BY filtersid.shellId.id
HAVING COUNT(*) = :keyValueCombinationsSize
)
""")
""" )
List<ShellIdentifierMinimal> findMinimalShellIdsBySpecificAssetIds( List<String> keyValueCombinations, int keyValueCombinationsSize );

/**
* Returns external shell ids for the given keyValueCombinations.
* External shell ids matching the conditions below are returned:
* - specificAssetIds match exactly the keyValueCombinations
* - if externalSubjectId (tenantId) is not null it must match the tenantId
*
*
* To be able to properly index the key and value conditions, the query does not use any functions.
* Computed indexes cannot be created for mutable functions like CONCAT in Postgres.
*
* @param keyValueCombinations the keys values to search for as tuples
* @param keyValueCombinationsSize the size of the key value combinations
* @return external shell ids for the given key value combinations
*/
@Query( value = """
SELECT s.id_external
FROM shell s
JOIN shell_identifier si ON s.id = si.fk_shell_id
WHERE
CONCAT(si.namespace, si.identifier) IN (:keyValueCombinations)
AND (
:tenantId = :owningTenantId
OR si.namespace = :globalAssetId
OR EXISTS (
SELECT 1
FROM SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE_KEY sider
JOIN SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE sies ON sider.FK_SI_EXTERNAL_SUBJECT_REFERENCE_ID = sies.id
WHERE
(
sider.ref_key_value = :tenantId
OR ( sider.ref_key_value = :publicWildcardPrefix AND si.namespace IN (:publicWildcardAllowedTypes) )
)
AND sies.FK_SHELL_IDENTIFIER_EXTERNAL_SUBJECT_ID = si.id
)
)
GROUP BY s.id_external
HAVING COUNT(*) = :keyValueCombinationsSize
""", nativeQuery = true )
List<String> findExternalShellIdsByIdentifiersByExactMatch( @Param( "keyValueCombinations" ) List<String> keyValueCombinations,
@Param( "keyValueCombinationsSize" ) int keyValueCombinationsSize,
@Param( "tenantId" ) String tenantId,
@Param( "publicWildcardPrefix" ) String publicWildcardPrefix,
@Param( "publicWildcardAllowedTypes" ) List<String> publicWildcardAllowedTypes,
@Param( "owningTenantId" ) String owningTenantId,
@Param( "globalAssetId" ) String globalAssetId );
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,52 +79,6 @@ Optional<Shell> findByIdExternalAndExternalSubjectId( @Param( "idExternal" ) Str

List<Shell> findShellsByIdExternalIsIn( Set<String> idExternals );

/**
* Returns external shell ids for the given keyValueCombinations.
* External shell ids matching the conditions below are returned:
* - specificAssetIds match exactly the keyValueCombinations
* - if externalSubjectId (tenantId) is not null it must match the tenantId
*
*
* To be able to properly index the key and value conditions, the query does not use any functions.
* Computed indexes cannot be created for mutable functions like CONCAT in Postgres.
*
* @param keyValueCombinations the keys values to search for as tuples
* @param keyValueCombinationsSize the size of the key value combinations
* @return external shell ids for the given key value combinations
*/
@Query( value = """
SELECT s.id_external
FROM shell s
JOIN shell_identifier si ON s.id = si.fk_shell_id
WHERE
CONCAT(si.namespace, si.identifier) IN (:keyValueCombinations)
AND (
:tenantId = :owningTenantId
OR si.namespace = :globalAssetId
OR EXISTS (
SELECT 1
FROM SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE_KEY sider
JOIN SHELL_IDENTIFIER_EXTERNAL_SUBJECT_REFERENCE sies ON sider.FK_SI_EXTERNAL_SUBJECT_REFERENCE_ID = sies.id
WHERE
(
sider.ref_key_value = :tenantId
OR ( sider.ref_key_value = :publicWildcardPrefix AND si.namespace IN (:publicWildcardAllowedTypes) )
)
AND sies.FK_SHELL_IDENTIFIER_EXTERNAL_SUBJECT_ID = si.id
)
)
GROUP BY s.id_external
HAVING COUNT(*) = :keyValueCombinationsSize
""", nativeQuery = true )
List<String> findExternalShellIdsByIdentifiersByExactMatch( @Param( "keyValueCombinations" ) List<String> keyValueCombinations,
@Param( "keyValueCombinationsSize" ) int keyValueCombinationsSize,
@Param( "tenantId" ) String tenantId,
@Param( "publicWildcardPrefix" ) String publicWildcardPrefix,
@Param( "publicWildcardAllowedTypes" ) List<String> publicWildcardAllowedTypes,
@Param( "owningTenantId" ) String owningTenantId,
@Param( "globalAssetId" ) String globalAssetId );

/**
* Returns external shell ids for the given keyValueCombinations.
* External shell ids that match any keyValueCombinations are returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public ShellCollectionDto findAllShells( Integer pageSize, String cursorVal, Str
Page<Shell> currentPage = shellRepository.findAll( specification, ofSize( fetchSize ) );
List<Shell> shells = shellAccessHandler.filterListOfShellProperties( currentPage.stream().toList(), externalSubjectId );
shells.stream()
.limit( (long) pageSize - foundList.size() )
.limit( (long) fetchSize - foundList.size() )
.forEach( foundList::add );
hasNext = currentPage.hasNext();
}
Expand Down Expand Up @@ -278,7 +278,7 @@ public GetAllAssetAdministrationShellIdsByAssetLink200Response findExternalShell
.forEach( assetIdList::add );
nextCursor = getCursorEncoded( allVisible, assetIdList );
} else {
List<String> queryResult = shellRepository.findExternalShellIdsByIdentifiersByExactMatch( keyValueCombinations,
List<String> queryResult = shellIdentifierRepository.findExternalShellIdsByIdentifiersByExactMatch( keyValueCombinations,
keyValueCombinations.size(), externalSubjectId, externalSubjectIdWildcardPrefix, externalSubjectIdWildcardAllowedTypes, owningTenantId,
ShellIdentifier.GLOBAL_ASSET_ID_KEY );
pageSize = getPageSize( pageSize );
Expand Down

0 comments on commit 27e1ba6

Please sign in to comment.