-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathMongoDbClientExtensions.cs
More file actions
120 lines (107 loc) · 5.22 KB
/
MongoDbClientExtensions.cs
File metadata and controls
120 lines (107 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using MongoDB.Bson;
using MongoDB.Driver;
using SimpleIdServer.Scim.Domains;
using SimpleIdServer.Scim.Persistence.MongoDB.Models;
using System.Collections.Generic;
using System.Linq;
namespace SimpleIdServer.Scim.Persistence.MongoDB.Extensions;
public static class MongoDbClientExtensions
{
public static void EnsureMongoDbSCIMDatabaseIsCreated(MongoDbOptions options, List<SCIMSchema> initialSchemas, List<SCIMAttributeMapping> initialAttributeMapping, List<Realm> realms)
{
var mongoClient = new MongoClient(options.ConnectionString);
var db = mongoClient.GetDatabase(options.Database);
EnsureCollectionIsCreated<SCIMRepresentationModel>(db, options.CollectionRepresentations);
EnsureCollectionIsCreated<SCIMRepresentationAttribute>(db, options.CollectionRepresentationAttributes);
EnsureCollectionIsCreated<ProvisioningConfiguration>(db, options.CollectionProvisioningLst);
EnsureSCIMRepresentationAttributeIndexesAreCreated(db, options.CollectionRepresentationAttributes);
var schemasCollection = EnsureCollectionIsCreated<SCIMSchema>(db, options.CollectionSchemas);
var mappingsCollection = EnsureCollectionIsCreated<SCIMAttributeMapping>(db, options.CollectionMappings);
var realmsCollection = EnsureCollectionIsCreated<Realm>(db, options.CollectionRealms);
var query = schemasCollection.AsQueryable();
if (query.Count() == 0)
{
if (initialSchemas != null)
{
schemasCollection.InsertMany(initialSchemas);
}
else
{
var schemas = new List<SCIMSchema>
{
StandardSchemas.GroupSchema,
StandardSchemas.UserSchema
};
schemasCollection.InsertMany(schemas);
}
}
if (mappingsCollection.AsQueryable().Count() == 0)
{
if (initialAttributeMapping != null)
{
mappingsCollection.InsertMany(initialAttributeMapping);
}
else
{
mappingsCollection.InsertMany(SCIMConstants.StandardAttributeMapping);
}
}
if (realmsCollection.AsQueryable().Count() == 0)
{
if (realms != null)
{
realmsCollection.InsertMany(realms);
}
else
{
realmsCollection.InsertMany(SCIMConstants.StandardRealms);
}
}
}
private static void EnsureSCIMRepresentationAttributeIndexesAreCreated(IMongoDatabase db, string name)
{
var compoundIndex = Builders<SCIMRepresentationAttribute>.IndexKeys.Ascending(a => a.RepresentationId).Ascending(a => a.SchemaAttributeId).Ascending(a => a.ValueString);
var representationIdIndex = Builders<SCIMRepresentationAttribute>.IndexKeys.Ascending(a => a.RepresentationId);
// Add optimized index for filtering by SchemaAttributeId and ValueString (commonly used in SCIM queries)
var schemaAttributeValueIndex = Builders<SCIMRepresentationAttribute>.IndexKeys.Ascending(a => a.SchemaAttributeId).Ascending(a => a.ValueString);
// Add index for ValueString with case-insensitive collation (useful for regex queries)
var caseInsensitiveCollation = new Collation("en", strength: CollationStrength.Secondary);
var valueStringCaseInsensitiveOptions = new CreateIndexOptions { Collation = caseInsensitiveCollation };
var valueStringIndex = Builders<SCIMRepresentationAttribute>.IndexKeys.Ascending(a => a.ValueString);
EnsureIndexCreated(db, "RepresentationId_1_SchemaAttributeId_1_ValueString_1", name, compoundIndex);
EnsureIndexCreated(db, "RepresentationId_1", name, representationIdIndex);
EnsureIndexCreated(db, "SchemaAttributeId_1_ValueString_1", name, schemaAttributeValueIndex);
EnsureIndexCreated(db, "ValueString_1_case_insensitive", name, valueStringIndex, valueStringCaseInsensitiveOptions);
}
private static async void EnsureIndexCreated(IMongoDatabase db, string indexName, string name, IndexKeysDefinition<SCIMRepresentationAttribute> indexDefinition, CreateIndexOptions options = null)
{
var collection = db.GetCollection<SCIMRepresentationAttribute>(name);
var indexes = await collection.Indexes.List().ToListAsync();
if (indexes.Any(i => i.Elements.Any(e => e.Name == "name" && e.Value.AsString == indexName))) return;
if (options != null)
{
var indexModel = new CreateIndexModel<SCIMRepresentationAttribute>(indexDefinition, options);
collection.Indexes.CreateOne(indexModel);
}
else
{
collection.Indexes.CreateOne(indexDefinition);
}
}
private static IMongoCollection<T> EnsureCollectionIsCreated<T>(IMongoDatabase db, string name)
{
if (!CollectionExists(db, name))
{
db.CreateCollection(name);
}
return db.GetCollection<T>(name);
}
private static bool CollectionExists(IMongoDatabase db, string collectionName)
{
var filter = new BsonDocument("name", collectionName);
var collections = db.ListCollections(new ListCollectionsOptions { Filter = filter });
return collections.Any();
}
}