private static SearchSCIMRepresentationsResponse EvaluateOrderByMetadata( SCIMDbContext dbContext, SCIMAttributeExpression attrExpression, IQueryable <SCIMRepresentation> representations, SearchSCIMRepresentationOrders order, int startIndex, int count, IEnumerable <SCIMAttributeExpression> includedAttributes, IEnumerable <SCIMAttributeExpression> excludedAttributes) { int total = representations.Count(); var fullPath = attrExpression.GetFullPath(); if (!ParserConstants.MappingStandardAttributePathToProperty.ContainsKey(fullPath)) { return(null); } var representationParameter = Expression.Parameter(typeof(SCIMRepresentation), "rp"); var propertyName = ParserConstants.MappingStandardAttributePathToProperty[fullPath]; var property = Expression.Property(representationParameter, ParserConstants.MappingStandardAttributePathToProperty[fullPath]); var propertyType = typeof(SCIMRepresentation).GetProperty(propertyName).PropertyType; var orderBy = GetOrderByType(order, propertyType); var innerLambda = Expression.Lambda(property, new ParameterExpression[] { representationParameter }); var orderExpr = Expression.Call(orderBy, Expression.Constant(representations), innerLambda); var finalSelectArg = Expression.Parameter(typeof(IQueryable <SCIMRepresentation>), "f"); var finalOrderRequestBody = Expression.Lambda(orderExpr, new ParameterExpression[] { finalSelectArg }); var result = (IQueryable <SCIMRepresentation>)finalOrderRequestBody.Compile().DynamicInvoke(representations); var content = result.Skip(startIndex <= 1 ? 0 : startIndex - 1).Take(count); return(BuildResult(content, dbContext, includedAttributes, excludedAttributes, total)); }
public static IServiceCollection AddScimStoreMongoDB( this IServiceCollection services, Action <MongoDbOptions> mongoDbSetup, List <SCIMSchema> initialSchemas = null, List <SCIMAttributeMapping> initialAttributeMapping = null) { SCIMDbContext.RegisterMappings(); if (mongoDbSetup != null) { services.Configure(mongoDbSetup); var options = new MongoDbOptions(); mongoDbSetup(options); services.AddTransient <IMongoClient>(_ => new MongoClient(options.ConnectionString)); services.AddTransient(provider => provider.GetService <IMongoClient>().GetDatabase(options.Database)); services.AddTransient <SCIMDbContext>(); MongoDbClientExtensions.EnsureMongoDbSCIMDatabaseIsCreated(options, initialSchemas, initialAttributeMapping); } services.AddTransient <ISCIMRepresentationCommandRepository, SCIMRepresentationCommandRepository>(); services.AddTransient <ISCIMRepresentationQueryRepository, SCIMRepresentationQueryRepository>(); services.AddTransient <ISCIMSchemaQueryRepository, SCIMSchemaQueryRepository>(); services.AddTransient <ISCIMSchemaCommandRepository, SCIMSchemaCommandRepository>(); services.AddTransient <ISCIMAttributeMappingQueryRepository, SCIMAttributeMappingQueryRepository>(); services.AddTransient <IProvisioningConfigurationRepository, ProvisioningConfigurationRepository>(); return(services); }
private static void EnsureSeedData(SCIMDbContext context) { Console.WriteLine("Seeding database..."); context.SCIMSchemaLst.Add(SCIMConstants.StandardSchemas.GroupSchema); context.SCIMSchemaLst.Add(SCIMConstants.StandardSchemas.UserSchema); context.SCIMSchemaLst.Add(SCIMConstants.StandardSchemas.CommonSchema); context.SaveChanges(); }
private static async Task <SearchSCIMRepresentationsResponse> EvaluateOrderByProperty( SCIMDbContext dbContext, SCIMAttributeExpression attrExpression, IQueryable <SCIMRepresentationModel> representations, SearchSCIMRepresentationOrders order, int startIndex, int count, CancellationToken cancellationToken) { var lastChild = attrExpression.GetLastChild(); var query = from rep in (from s in representations join attr in dbContext.SCIMRepresentationAttributeLst on s.Id equals attr.RepresentationId join attrv in dbContext.SCIMRepresentationAttributeValueLst on attr.Id equals attrv.SCIMRepresentationAttributeId select new { representation = s, representationId = s.Id, orderedValue = (lastChild.SchemaAttribute.Id == attr.SchemaAttributeId) ? attrv.ValueString : "" }) orderby rep.orderedValue descending select rep.representationId; var orderedIds = await query.Skip(startIndex).Take(count).ToListAsync(cancellationToken); var result = await dbContext.SCIMRepresentationLst.Where(s => orderedIds.Contains(s.Id)).ToListAsync(cancellationToken); var comparer = new RepresentationComparer(orderedIds); List <SCIMRepresentationModel> content = null; switch (order) { case SearchSCIMRepresentationOrders.Ascending: content = result.OrderBy(r => r, comparer).ToList(); break; case SearchSCIMRepresentationOrders.Descending: content = result.OrderByDescending(r => r, comparer).ToList(); break; } var total = await representations.CountAsync(cancellationToken); return(new SearchSCIMRepresentationsResponse(total, content.Select(r => r.ToDomain()))); }
public static async Task <SearchSCIMRepresentationsResponse> EvaluateOrderBy( this SCIMExpression expression, SCIMDbContext dbContext, IQueryable <SCIMRepresentation> representations, SearchSCIMRepresentationOrders order, int startIndex, int count, IEnumerable <SCIMAttributeExpression> includedAttributes, IEnumerable <SCIMAttributeExpression> excludedAttributes) { var attrExpression = expression as SCIMAttributeExpression; if (attrExpression == null) { return(null); } var result = EvaluateOrderByMetadata(dbContext, attrExpression, representations, order, startIndex, count, includedAttributes, excludedAttributes); if (result == null) { result = EvaluateOrderByProperty( dbContext, attrExpression, representations, order, startIndex, count, includedAttributes, excludedAttributes); } return(result); }
private static SearchSCIMRepresentationsResponse EvaluateOrderByProperty( SCIMDbContext dbContext, SCIMAttributeExpression attrExpression, IQueryable <SCIMRepresentation> representations, SearchSCIMRepresentationOrders order, int startIndex, int count, IEnumerable <SCIMAttributeExpression> includedAttributes, IEnumerable <SCIMAttributeExpression> excludedAttributes) { int total = representations.Count(); var lastChild = attrExpression.GetLastChild(); var result = from s in representations join attr in dbContext.SCIMRepresentationAttributeLst on s.Id equals attr.RepresentationId select new { Attr = attr, Rep = s, orderedValue = (lastChild.SchemaAttribute.Id == attr.SchemaAttributeId) ? attr.ValueString : "" }; switch (order) { case SearchSCIMRepresentationOrders.Ascending: result = result.OrderBy(q => q.orderedValue); break; case SearchSCIMRepresentationOrders.Descending: result = result.OrderByDescending(q => q.orderedValue); break; } var content = result.Select(r => r.Rep).Skip(startIndex <= 1 ? 0 : startIndex - 1).Take(count); return(BuildResult(content, dbContext, includedAttributes, excludedAttributes, total)); }
public static async Task <SearchSCIMRepresentationsResponse> EvaluateOrderBy(this SCIMExpression expression, SCIMDbContext dbContext, IQueryable <SCIMRepresentationModel> representations, SearchSCIMRepresentationOrders order, int startIndex, int count, CancellationToken cancellationToken) { var attrExpression = expression as SCIMAttributeExpression; if (attrExpression == null) { return(null); } var result = await EvaluateOrderByMetadata(attrExpression, representations, order, startIndex, count, cancellationToken); if (result == null) { result = await EvaluateOrderByProperty(dbContext, attrExpression, representations, order, startIndex, count, cancellationToken); } return(result); }
public static SearchSCIMRepresentationsResponse BuildResult( this IQueryable <SCIMRepresentation> representations, SCIMDbContext dbContext, IEnumerable <SCIMAttributeExpression> includedAttributes, IEnumerable <SCIMAttributeExpression> excludedAttributes, int total) { IQueryable <SCIMRepresentationAttribute> filteredAttrs = null; if (includedAttributes != null && includedAttributes.Any()) { filteredAttrs = dbContext.SCIMRepresentationAttributeLst.Include(r => r.Children).FilterAttributes(includedAttributes); } if (excludedAttributes != null && excludedAttributes.Any()) { filteredAttrs = dbContext.SCIMRepresentationAttributeLst.Include(r => r.Children).FilterAttributes(excludedAttributes, false); } if (filteredAttrs != null) { var result = from rep in representations join at in filteredAttrs on rep.Id equals at.RepresentationId into a from attr in a.DefaultIfEmpty() select new { Attr = attr, Id = rep.Id, Created = rep.Created, LastModified = rep.LastModified, ExternalId = rep.ExternalId, ResourceType = rep.ResourceType, DisplayName = rep.DisplayName, Version = rep.Version }; var content = result.AsEnumerable().GroupBy(r => r.Id); var includedFullPathLst = (includedAttributes != null && includedAttributes.Any()) ? includedAttributes.Where(i => i is SCIMComplexAttributeExpression).Select(i => i.GetFullPath()) : new List <string>(); return(new SearchSCIMRepresentationsResponse(total, content.Select(c => new SCIMRepresentation { FlatAttributes = c.Where(_ => _.Attr != null).SelectMany(_ => { var lst = new List <SCIMRepresentationAttribute> { _.Attr }; lst.AddRange(_.Attr.Children.Where(c => includedFullPathLst.Any(f => c.FullPath.StartsWith(f)))); return lst; }).ToList(), Id = c.Key, Created = c.First().Created, DisplayName = c.First().DisplayName, ExternalId = c.First().ExternalId, LastModified = c.First().LastModified, ResourceType = c.First().ResourceType, Version = c.First().Version }))); } var reps = representations.ToList(); return(new SearchSCIMRepresentationsResponse(total, reps)); }