Skip to content

Commit

Permalink
fix: improve paginating a little in the InMemory sample
Browse files Browse the repository at this point in the history
Fixes #83
  • Loading branch information
Sefriol committed May 29, 2024
1 parent a930ba0 commit d894821
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
15 changes: 15 additions & 0 deletions Microsoft.SCIM.WebHostSample/Provider/InMemoryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
throw new NotImplementedException();
}

public override Task<QueryResponseBase> PaginateQueryAsync(IRequest<IQueryParameters> request)
{
if (request.Payload.SchemaIdentifier.Equals(SchemaIdentifiers.Core2EnterpriseUser))
{
return this.userProvider.PaginateQueryAsync(request);
}

if (request.Payload.SchemaIdentifier.Equals(SchemaIdentifiers.Core2Group))
{
return this.groupProvider.PaginateQueryAsync(request);
}

throw new NotImplementedException();
}

public override Task<Resource> ReplaceAsync(Resource resource, string correlationIdentifier)
{
if (resource is Core2EnterpriseUser)
Expand Down
78 changes: 53 additions & 25 deletions Microsoft.SCIM.WebHostSample/Provider/InMemoryUserProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,34 @@ public override Task<Resource> DeleteAsync(IResourceIdentifier resourceIdentifie
this.storage.Users.Remove(identifier);
return Task.FromResult((Resource)user);
}

throw new CustomHttpResponseException(HttpStatusCode.NotFound);
}

public override async Task<QueryResponseBase> PaginateQueryAsync(IRequest<IQueryParameters> request)
{
if (null == request)
{
throw new ArgumentNullException(nameof(request));
}

IReadOnlyCollection<Resource> resources = await this.QueryAsync(request).ConfigureAwait(false);
int totalCount = resources.Count;
if (request.Payload.PaginationParameters != null)
{
int count = request.Payload.PaginationParameters?.Count ?? 0;
resources = (IReadOnlyCollection<Resource>)resources.Take(count);
}


QueryResponseBase result = new QueryResponse(resources);
result.TotalResults = totalCount;
result.ItemsPerPage = resources.Count;

result.StartIndex = resources.Any() ? 1 : null;
return result;
}

public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string correlationIdentifier)
{
if (parameters == null)
Expand Down Expand Up @@ -110,7 +135,6 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
}
else
{

foreach (IFilter queryFilter in parameters.AlternateFilters)
{
predicateAnd = PredicateBuilder.True<Core2EnterpriseUser>();
Expand All @@ -121,16 +145,19 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
{
if (string.IsNullOrWhiteSpace(andFilter.AttributePath))
{
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources.ExceptionInvalidParameters);
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources
.ExceptionInvalidParameters);
}

else if (string.IsNullOrWhiteSpace(andFilter.ComparisonValue))
{
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources.ExceptionInvalidParameters);
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources
.ExceptionInvalidParameters);
}

// ID filter
else if (andFilter.AttributePath.Equals(AttributeNames.Identifier, StringComparison.OrdinalIgnoreCase))
else if (andFilter.AttributePath.Equals(AttributeNames.Identifier,
StringComparison.OrdinalIgnoreCase))
{
if (andFilter.FilterOperator != ComparisonOperator.Equals)
{
Expand All @@ -141,18 +168,21 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
}

var id = andFilter.ComparisonValue;
predicateAnd = predicateAnd.And(p => string.Equals(p.Identifier, id, StringComparison.OrdinalIgnoreCase));
predicateAnd = predicateAnd.And(p =>
string.Equals(p.Identifier, id, StringComparison.OrdinalIgnoreCase));
}

// UserName filter
else if (andFilter.AttributePath.Equals(AttributeNames.UserName, StringComparison.OrdinalIgnoreCase))
else if (andFilter.AttributePath.Equals(AttributeNames.UserName,
StringComparison.OrdinalIgnoreCase))
{
if (andFilter.FilterOperator != ComparisonOperator.Equals)
{
throw new ScimTypeException(ErrorType.invalidFilter,
string.Format(
SystemForCrossDomainIdentityManagementServiceResources
.ExceptionFilterOperatorNotSupportedTemplate, andFilter.FilterOperator));
.ExceptionFilterOperatorNotSupportedTemplate,
andFilter.FilterOperator));
}

string userName = andFilter.ComparisonValue;
Expand All @@ -173,13 +203,13 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
}

string externalIdentifier = andFilter.ComparisonValue;
predicateAnd = predicateAnd.And(p => string.Equals(p.ExternalIdentifier, externalIdentifier, StringComparison.OrdinalIgnoreCase));


predicateAnd = predicateAnd.And(p => string.Equals(p.ExternalIdentifier, externalIdentifier,
StringComparison.OrdinalIgnoreCase));
}

//Active Filter
else if (andFilter.AttributePath.Equals(AttributeNames.Active, StringComparison.OrdinalIgnoreCase))
else if (andFilter.AttributePath.Equals(AttributeNames.Active,
StringComparison.OrdinalIgnoreCase))
{
if (andFilter.FilterOperator != ComparisonOperator.Equals)
{
Expand All @@ -194,7 +224,8 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
}

// DisplayName Filter
else if (andFilter.AttributePath.Equals(AttributeNames.DisplayName, StringComparison.OrdinalIgnoreCase))
else if (andFilter.AttributePath.Equals(AttributeNames.DisplayName,
StringComparison.OrdinalIgnoreCase))
{
if (andFilter.FilterOperator != ComparisonOperator.Equals)
{
Expand Down Expand Up @@ -245,15 +276,7 @@ public override Task<Resource[]> QueryAsync(IQueryParameters parameters, string
results = this.storage.Users.Values.Where(predicate.Compile());
}

if (parameters.PaginationParameters != null)
{
int count = parameters.PaginationParameters.Count.HasValue
? parameters.PaginationParameters.Count.Value
: 0;
return Task.FromResult(results.Take(count).ToArray());
}
else
return Task.FromResult(results.ToArray());
return Task.FromResult(results.ToArray());
}

public override Task<Resource> ReplaceAsync(Resource resource, string correlationIdentifier)
Expand All @@ -278,7 +301,9 @@ public override Task<Resource> ReplaceAsync(Resource resource, string correlatio
!string.Equals(exisitingUser.Identifier, user.Identifier, StringComparison.OrdinalIgnoreCase))
)
{
throw new CustomHttpResponseException(HttpStatusCode.Conflict);
throw new ScimTypeException(ErrorType.uniqueness, string.Format(
SystemForCrossDomainIdentityManagementServiceResources
.ExceptionResourceConflict));
}

Core2EnterpriseUser exisitingUser = this.storage.Users.Values
Expand Down Expand Up @@ -339,17 +364,20 @@ public override Task<Resource> UpdateAsync(IPatch patch, string correlationIdent

if (null == patch.ResourceIdentifier)
{
throw new ArgumentException(string.Format(SystemForCrossDomainIdentityManagementServiceResources.ExceptionInvalidOperation));
throw new ArgumentException(string.Format(SystemForCrossDomainIdentityManagementServiceResources
.ExceptionInvalidOperation));
}

if (string.IsNullOrWhiteSpace(patch.ResourceIdentifier.Identifier))
{
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources.ExceptionInvalidOperation);
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources
.ExceptionInvalidOperation);
}

if (null == patch.PatchRequest)
{
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources.ExceptionInvalidOperation);
throw new ArgumentException(SystemForCrossDomainIdentityManagementServiceResources
.ExceptionInvalidOperation);
}

PatchRequest2 patchRequest =
Expand Down

0 comments on commit d894821

Please sign in to comment.