-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathSCIMRepresentationQueryRepository.cs
More file actions
281 lines (257 loc) · 14 KB
/
SCIMRepresentationQueryRepository.cs
File metadata and controls
281 lines (257 loc) · 14 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 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.Driver;
using MongoDB.Driver.Linq;
using SimpleIdServer.Persistence.Filters;
using SimpleIdServer.Scim.Domain;
using SimpleIdServer.Scim.Domains;
using SimpleIdServer.Scim.Parser.Expressions;
using SimpleIdServer.Scim.Persistence.MongoDB.Extensions;
using SimpleIdServer.Scim.Persistence.MongoDB.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleIdServer.Scim.Persistence.MongoDB
{
public class SCIMRepresentationQueryRepository : ISCIMRepresentationQueryRepository
{
private readonly SCIMDbContext _scimDbContext;
public SCIMRepresentationQueryRepository(SCIMDbContext scimDbContext)
{
_scimDbContext = scimDbContext;
}
public async Task<SearchSCIMRepresentationsResponse> FindSCIMRepresentations(SearchSCIMRepresentationsParameter parameter, CancellationToken cancellationToken)
{
List<string> filteredRepresentationIds = null;
int total = 0;
if (parameter.Filter != null)
{
var filteredRepresentationAttributes = _scimDbContext.SCIMRepresentationAttributeLst.AsQueryable();
filteredRepresentationAttributes = parameter.Filter.EvaluateMongoDbAttributesDirect(filteredRepresentationAttributes);
if (filteredRepresentationAttributes != null)
{
filteredRepresentationIds = (await
filteredRepresentationAttributes
.Select(a => a.RepresentationId)
.ToMongoListAsync())
.Distinct()
.ToList();
total = filteredRepresentationIds.Count;
}
}
else
{
total = await _scimDbContext.SCIMRepresentationLst.AsQueryable()
.Where(s => s.ResourceType == parameter.ResourceType)
.CountAsync(cancellationToken);
}
if (parameter.Count == 0)
return new SearchSCIMRepresentationsResponse(total, []);
var filteredRepresentations = _scimDbContext.SCIMRepresentationLst.AsQueryable()
.Where(r => r.ResourceType == parameter.ResourceType);
if (!string.IsNullOrWhiteSpace(parameter.Realm))
filteredRepresentations = filteredRepresentations.Where(r => r.RealmName == parameter.Realm);
if (parameter.Filter != null)
{
var tmp = parameter.Filter.EvaluateMongoDbRepresentations(filteredRepresentations);
if (tmp != null)
{
var tmpIds = await tmp.Select(r => r.Id).ToListAsync(cancellationToken);
if (filteredRepresentationIds == null) filteredRepresentationIds = [.. tmpIds];
else
{
if (parameter.Filter is SCIMLogicalExpression logical)
{
if (logical.LogicalOperator == Parser.Operators.SCIMLogicalOperators.AND)
filteredRepresentationIds = [.. filteredRepresentationIds.Intersect(tmpIds)];
else
filteredRepresentationIds = [.. filteredRepresentationIds.Union(tmpIds)];
}
}
total = filteredRepresentationIds.Count;
}
}
if (filteredRepresentationIds != null)
{
filteredRepresentations = filteredRepresentations.Where(r => filteredRepresentationIds.Contains(r.Id));
}
var paginationResult = await OrderByAndPaginate(filteredRepresentations, parameter, filteredRepresentationIds);
filteredRepresentations = paginationResult.Query;
var representationsList = await filteredRepresentations.ToListAsync(cancellationToken);
var representationIds = representationsList.Select(r => r.Id).ToList();
var attributes = await _scimDbContext.SCIMRepresentationAttributeLst.AsQueryable()
.Where(a => representationIds.Contains(a.RepresentationId))
.ToListAsync(cancellationToken);
var attributesByRepId = attributes.GroupBy(a => a.RepresentationId)
.ToDictionary(g => g.Key, g => g.ToList());
var representationWithAttributes = representationsList.Select(rep => new
{
Representation = rep,
Attributes = attributesByRepId.ContainsKey(rep.Id) ? attributesByRepId[rep.Id] : new List<SCIMRepresentationAttribute>()
}).ToList();
if (paginationResult.OrderedRepresentationIds != null)
{
representationWithAttributes = representationWithAttributes.Select(r => new
{
Representation = r,
Order = paginationResult.OrderedRepresentationIds.IndexOf(r.Representation.Id)
})
.OrderBy(r => r.Order)
.Select(r => r.Representation)
.ToList();
}
var representations = new List<SCIMRepresentation>();
foreach (var record in representationWithAttributes)
{
var representation = record.Representation;
representation.FlatAttributes = [.. record.Attributes];
representations.Add(representation);
}
representations.FilterAttributes(parameter.IncludedAttributes, parameter.ExcludedAttributes);
return new SearchSCIMRepresentationsResponse(total, representations);
}
public async Task<SCIMRepresentation> FindSCIMRepresentationById(string realm, string representationId, string resourceType, GetSCIMResourceParameter parameter, CancellationToken cancellationToken)
{
var collection = _scimDbContext.SCIMRepresentationLst;
SCIMRepresentationModel result = null;
if(!string.IsNullOrWhiteSpace(realm))
result = await collection.AsQueryable().Where(a => a.Id == representationId && a.ResourceType == resourceType && a.RealmName == realm).ToMongoFirstAsync();
else
result = await collection.AsQueryable().Where(a => a.Id == representationId && a.ResourceType == resourceType).ToMongoFirstAsync();
if (result == null)
{
return null;
}
await result.IncludeAll(_scimDbContext);
return result;
}
private async Task<PaginationResult> OrderByAndPaginate(
IMongoQueryable<SCIMRepresentationModel> representations,
SearchSCIMRepresentationsParameter parameter,
List<string> representationIds)
{
if (parameter.SortBy == null) return new PaginationResult
{
Query = representations
.OrderBy(r => r.Id)
.Skip(parameter.StartIndex <= 1 ? 0 : parameter.StartIndex - 1)
.Take(parameter.Count)
};
var sortBy = parameter.SortBy as SCIMAttributeExpression;
var order = parameter.SortOrder ?? SearchSCIMRepresentationOrders.Ascending;
var result = OrderByMetadataAndPaginate(sortBy,
representations,
order,
parameter.StartIndex,
parameter.Count);
if (result != null) return result;
return await OrderByPropertyAndPaginate(
representations,
sortBy,
parameter.SchemaNames,
order,
parameter.StartIndex,
parameter.Count,
representationIds);
}
private PaginationResult OrderByMetadataAndPaginate(
SCIMAttributeExpression attrExpression,
IMongoQueryable<SCIMRepresentationModel> representations,
SearchSCIMRepresentationOrders order,
int startIndex,
int count)
{
var fullPath = attrExpression.GetFullPath();
IMongoQueryable<SCIMRepresentationModel> result = null;
switch (fullPath)
{
case StandardSCIMRepresentationAttributes.Id:
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.Id) : representations.OrderByDescending(r => r.Id);
break;
case StandardSCIMRepresentationAttributes.ExternalId:
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.ExternalId) : representations.OrderByDescending(r => r.ExternalId);
break;
case $"{StandardSCIMRepresentationAttributes.Meta}.{StandardSCIMMetaAttributes.ResourceType}":
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.ResourceType) : representations.OrderByDescending(r => r.ResourceType);
break;
case $"{StandardSCIMRepresentationAttributes.Meta}.{StandardSCIMMetaAttributes.Created}":
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.Created) : representations.OrderByDescending(r => r.Created);
break;
case $"{StandardSCIMRepresentationAttributes.Meta}.{StandardSCIMMetaAttributes.LastModified}":
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.LastModified) : representations.OrderByDescending(r => r.LastModified);
break;
case $"{StandardSCIMRepresentationAttributes.Meta}.{StandardSCIMMetaAttributes.Version}":
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.Version) : representations.OrderByDescending(r => r.Version);
break;
case StandardSCIMRepresentationAttributes.DisplayName:
result = order == SearchSCIMRepresentationOrders.Ascending ? representations.OrderBy(r => r.DisplayName) : representations.OrderByDescending(r => r.DisplayName);
break;
default:
return null;
}
return new PaginationResult
{
Query = result.Skip(startIndex <= 1 ? 0 : startIndex - 1).Take(count)
};
}
private async Task<PaginationResult> OrderByPropertyAndPaginate(
IMongoQueryable<SCIMRepresentationModel> representations,
SCIMAttributeExpression attrExpression,
List<string> schemaNames,
SearchSCIMRepresentationOrders order,
int startIndex,
int count,
List<string> ids)
{
var fullPath = attrExpression.GetFullPath();
var attributes = _scimDbContext.SCIMRepresentationAttributeLst
.AsQueryable()
.Where(r => r.FullPath == fullPath && schemaNames.Contains(r.Namespace));
if (ids != null)
attributes = attributes.Where(a => ids.Contains(a.RepresentationId));
var lastExpr = attrExpression.GetLastChild();
switch(lastExpr.SchemaAttribute.Type)
{
case SCIMSchemaAttributeTypes.STRING:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueString) : attributes.OrderByDescending(a => a.ValueString);
break;
case SCIMSchemaAttributeTypes.BOOLEAN:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueBoolean) : attributes.OrderByDescending(a => a.ValueBoolean);
break;
case SCIMSchemaAttributeTypes.DECIMAL:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueDecimal) : attributes.OrderByDescending(a => a.ValueDecimal);
break;
case SCIMSchemaAttributeTypes.INTEGER:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueInteger) : attributes.OrderByDescending(a => a.ValueInteger);
break;
case SCIMSchemaAttributeTypes.DATETIME:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueDateTime) : attributes.OrderByDescending(a => a.ValueDateTime);
break;
case SCIMSchemaAttributeTypes.BINARY:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueBinary) : attributes.OrderByDescending(a => a.ValueBinary);
break;
case SCIMSchemaAttributeTypes.REFERENCE:
attributes = order == SearchSCIMRepresentationOrders.Ascending ? attributes.OrderBy(a => a.ValueReference) : attributes.OrderByDescending(a => a.ValueReference);
break;
}
var representationIds = (await attributes
.Select(a => a.RepresentationId)
.Skip(startIndex <= 1 ? 0 : startIndex - 1)
.Take(count)
.ToListAsync())
.Distinct()
.ToList();
return new PaginationResult
{
Query = representations.Where(r => representationIds.Contains(r.Id)),
OrderedRepresentationIds = representationIds
};
}
private record PaginationResult
{
public IMongoQueryable<SCIMRepresentationModel> Query { get; set; }
public List<string> OrderedRepresentationIds { get; set; }
}
}
}