@@ -23,7 +23,7 @@ namespace SimpleIdServer.Scim.Helpers
2323 public interface IRepresentationHelper
2424 {
2525 Task < SCIMRepresentationPatchResult > Apply ( SCIMRepresentation representation , IEnumerable < PatchOperationParameter > patchLst , IEnumerable < SCIMAttributeMapping > attributeMappings , bool ignoreUnsupportedCanonicalValues , CancellationToken cancellationToken ) ;
26- Task CheckUniqueness ( IEnumerable < SCIMRepresentationAttribute > attributes ) ;
26+ Task CheckUniqueness ( string realm , IEnumerable < SCIMRepresentationAttribute > attributes ) ;
2727 void CheckMutability ( List < SCIMPatchResult > patchOperations ) ;
2828 SCIMRepresentation ExtractSCIMRepresentationFromJSON ( JsonObject json , string externalId , SCIMSchema mainSchema , ICollection < SCIMSchema > extensionSchemas , IEnumerable < SCIMAttributeMapping > attributeMappings ) ;
2929 }
@@ -547,15 +547,15 @@ private static List<SCIMRepresentationAttribute> FilterDuplicate(IEnumerable<SCI
547547
548548 #region Check uniqueness
549549
550- public async Task CheckUniqueness ( IEnumerable < SCIMRepresentationAttribute > attributes )
550+ public async Task CheckUniqueness ( string realm , IEnumerable < SCIMRepresentationAttribute > attributes )
551551 {
552552 var uniqueServerAttributeIds = attributes . Where ( a => a . SchemaAttribute . Uniqueness == SCIMSchemaAttributeUniqueness . SERVER ) ;
553553 var uniqueGlobalAttributes = attributes . Where ( a => a . SchemaAttribute . Uniqueness == SCIMSchemaAttributeUniqueness . GLOBAL ) ;
554- await CheckSCIMRepresentationExistsForGivenUniqueAttributes ( uniqueServerAttributeIds ) ;
555- await CheckSCIMRepresentationExistsForGivenUniqueAttributes ( uniqueGlobalAttributes ) ;
554+ await CheckSCIMRepresentationExistsForGivenUniqueAttributes ( realm , uniqueServerAttributeIds ) ;
555+ await CheckSCIMRepresentationExistsForGivenUniqueAttributes ( realm , uniqueGlobalAttributes ) ;
556556 }
557557
558- private async Task CheckSCIMRepresentationExistsForGivenUniqueAttributes ( IEnumerable < SCIMRepresentationAttribute > attributes )
558+ private async Task CheckSCIMRepresentationExistsForGivenUniqueAttributes ( string realm , IEnumerable < SCIMRepresentationAttribute > attributes )
559559 {
560560 foreach ( var attribute in attributes )
561561 {
@@ -567,11 +567,21 @@ private async Task CheckSCIMRepresentationExistsForGivenUniqueAttributes(IEnumer
567567 break ;
568568 case SCIMSchemaAttributeTypes . INTEGER :
569569 if ( attribute . ValueInteger != null )
570+ {
570571 records = await _scimRepresentationCommandRepository . FindAttributesByValue ( attribute . SchemaAttribute . Id , attribute . ValueInteger . Value ) ;
572+ }
571573 break ;
572574 }
573575
574- if ( records != null && records . Any ( ) )
576+ var isUnique = records != null && records . Any ( ) ;
577+ if ( ! string . IsNullOrWhiteSpace ( realm ) )
578+ {
579+ var representations = await _scimRepresentationCommandRepository . FindRepresentations ( records . Select ( r => r . RepresentationId ) . Distinct ( ) . ToList ( ) ) ;
580+ var filteredRepresentations = representations . Where ( r => r . RealmName == realm ) ;
581+ isUnique = filteredRepresentations != null && filteredRepresentations . Any ( ) ;
582+ }
583+
584+ if ( isUnique )
575585 {
576586 throw new SCIMUniquenessAttributeException ( string . Format ( Global . AttributeMustBeUnique , attribute . SchemaAttribute . Name ) ) ;
577587 }
0 commit comments