-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathEFSCIMRepresentationCommandRepository.cs
267 lines (240 loc) · 14.7 KB
/
EFSCIMRepresentationCommandRepository.cs
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
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using SimpleIdServer.Scim.Domains;
using SimpleIdServer.Scim.Parser.Expressions;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SimpleIdServer.Scim.Persistence.EF;
public class EFSCIMRepresentationCommandRepository : ISCIMRepresentationCommandRepository
{
private readonly SCIMDbContext _scimDbContext;
private readonly SCIMEFOptions _options;
public EFSCIMRepresentationCommandRepository(SCIMDbContext scimDbContext, IOptions<SCIMEFOptions> options)
{
_scimDbContext = scimDbContext;
_options = options.Value;
}
public async Task<SCIMRepresentation> Get(string realm, string id, CancellationToken token = default)
{
var query = _scimDbContext.SCIMRepresentationLst
.Include(r => r.Schemas).ThenInclude(s => s.Attributes);
if(!string.IsNullOrWhiteSpace(realm))
return await query.FirstOrDefaultAsync(r => r.RealmName == realm && r.Id == id, token);
return await query.FirstOrDefaultAsync(r => r.Id == id, token);
}
public Task<bool> Add(SCIMRepresentation data, CancellationToken token)
{
_scimDbContext.SCIMRepresentationLst.Add(data);
foreach (var attr in data.FlatAttributes)
_scimDbContext.SCIMRepresentationAttributeLst.Add(attr);
return Task.FromResult(true);
}
public Task<bool> Update(SCIMRepresentation data, CancellationToken token)
{
_scimDbContext.SCIMRepresentationLst.Update(data);
return Task.FromResult(true);
}
public Task<bool> Delete(SCIMRepresentation data, CancellationToken token)
{
_scimDbContext.SCIMRepresentationLst.Remove(data);
return Task.FromResult(true);
}
public async Task<List<SCIMRepresentation>> FindRepresentations(List<string> representationIds, string resourceType = null, CancellationToken cancellationToken = default(CancellationToken))
{
var query = _scimDbContext.SCIMRepresentationLst
.Where(r => representationIds.Contains(r.Id))
.AsNoTracking();
if (!string.IsNullOrWhiteSpace(resourceType))
query = query.Where(r => r.ResourceType == resourceType);
var result = await query.ToListAsync(cancellationToken);
return result;
}
public async Task<List<SCIMRepresentationAttribute>> FindGraphAttributes(string valueStr, string schemaAttributeId, string sourceRepresentationId = null, CancellationToken cancellationToken = default(CancellationToken))
{
var parentIds = await _scimDbContext.SCIMRepresentationAttributeLst.AsNoTracking()
.Where(a => a.SchemaAttributeId == schemaAttributeId && a.ValueString == valueStr || (sourceRepresentationId != null && a.ValueString == sourceRepresentationId))
.OrderBy(r => r.ParentAttributeId)
.Select(r => r.ParentAttributeId)
.AsNoTracking()
.ToListAsync(cancellationToken);
var result = await _scimDbContext.SCIMRepresentationAttributeLst.Include(s => s.SchemaAttribute).AsNoTracking()
.Where(a => parentIds.Contains(a.Id) || parentIds.Contains(a.ParentAttributeId))
.ToListAsync(cancellationToken);
return result;
}
public async Task<List<SCIMRepresentationAttribute>> FindGraphAttributes(IEnumerable<string> representationIds, string valueStr, string schemaAttributeId, string sourceRepresentationId = null, CancellationToken cancellationToken = default(CancellationToken))
{
var parentIds = await _scimDbContext.SCIMRepresentationAttributeLst.AsNoTracking()
.Where(a => a.SchemaAttributeId == schemaAttributeId && representationIds.Contains(a.RepresentationId) && a.ValueString == valueStr || (sourceRepresentationId != null && a.ValueString == sourceRepresentationId))
.Select(r => r.ParentAttributeId)
.AsNoTracking()
.Distinct()
.ToListAsync(cancellationToken);
var result = await _scimDbContext.SCIMRepresentationAttributeLst.Include(s => s.SchemaAttribute).AsNoTracking()
.Where(a => parentIds.Contains(a.Id) || parentIds.Contains(a.ParentAttributeId))
.ToListAsync();
return result;
}
public async Task<List<SCIMRepresentationAttribute>> FindGraphAttributesBySchemaAttributeId(string representationId, string schemaAttributeId, CancellationToken cancellationToken)
{
var ids = await _scimDbContext.SCIMRepresentationAttributeLst.AsNoTracking()
.Where(a => a.SchemaAttributeId == schemaAttributeId && a.RepresentationId == representationId)
.OrderBy(r => r.Id)
.Select(r => r.Id)
.ToListAsync(cancellationToken);
var result = await _scimDbContext.SCIMRepresentationAttributeLst.Include(s => s.SchemaAttribute)
.AsNoTracking()
.Where(a => ids.Contains(a.Id) || ids.Contains(a.ParentAttributeId))
.ToListAsync(cancellationToken);
return result;
}
public async Task<List<SCIMRepresentationAttribute>> FindGraphAttributesBySchemaAttributeId(List<string> representationIds, string schemaAttributeId, CancellationToken cancellationToken)
{
var ids = await _scimDbContext.SCIMRepresentationAttributeLst.AsNoTracking()
.Where(a => a.SchemaAttributeId == schemaAttributeId && representationIds.Contains(a.RepresentationId))
.OrderBy(r => r.Id)
.Select(r => r.Id)
.ToListAsync(cancellationToken);
var result = await _scimDbContext.SCIMRepresentationAttributeLst.Include(s => s.SchemaAttribute).AsNoTracking()
.Where(a => ids.Contains(a.Id) || ids.Contains(a.ParentAttributeId))
.ToListAsync(cancellationToken);
return result;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributes(string representationId, SCIMAttributeExpression pathExpression, CancellationToken cancellationToken)
{
var representationAttributes = _scimDbContext.SCIMRepresentationAttributeLst
.Include(s => s.SchemaAttribute)
.Include(s => s.Children).ThenInclude(s => s.SchemaAttribute)
.Where(r => r.RepresentationId == representationId)
.AsNoTracking();
if (pathExpression.SchemaAttribute == null || string.IsNullOrWhiteSpace(pathExpression.SchemaAttribute.Id))
return new List<SCIMRepresentationAttribute>();
var filteredAttributes = await pathExpression.EvaluateAttributes(representationAttributes, true, "Children").ToListAsync(cancellationToken);
foreach (var a in filteredAttributes) a.CachedChildren = a.Children;
return filteredAttributes.SelectMany(a => a.ToFlat()).ToList();
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributes(string representationId, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Where(r => r.RepresentationId == representationId).ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByAproximativeFullPath(string representationId, string fullPath, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => a.RepresentationId == representationId && a.FullPath.StartsWith(fullPath))
.ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByExactFullPathAndValues(string fullPath, IEnumerable<string> values, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => values.Contains(a.ValueString) && a.FullPath == fullPath)
.ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByExactFullPathAndRepresentationIds(string fullPath, IEnumerable<string> values, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => values.Contains(a.RepresentationId) && a.FullPath == fullPath)
.ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesBySchemaAttributeAndValues(string schemaAttributeId, IEnumerable<string> values, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => values.Contains(a.ValueString) && a.SchemaAttributeId == schemaAttributeId)
.ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByComputedValueIndexAndRepresentationId(List<string> computedValueIndexLst, string representationId, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => computedValueIndexLst.Contains(a.ComputedValueIndex) && a.RepresentationId == representationId)
.ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByReference(List<string> representationIds, string schemaAttributeId, string value, CancellationToken cancellationToken)
{
var representationAttributes = await _scimDbContext.SCIMRepresentationAttributeLst.Include(s => s.SchemaAttribute).AsNoTracking()
.Where(a => representationIds.Contains(a.RepresentationId) && a.SchemaAttributeId == schemaAttributeId && a.ValueString == value)
.ToListAsync(cancellationToken);
return representationAttributes;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByValue(string attrSchemaId, string value)
{
var result = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => a.SchemaAttribute.Id == attrSchemaId && a.ValueString == value)
.ToListAsync();
return result;
}
public async Task<List<SCIMRepresentationAttribute>> FindAttributesByValue(string attrSchemaId, int value)
{
var result = await _scimDbContext.SCIMRepresentationAttributeLst.Include(a => a.SchemaAttribute).AsNoTracking()
.Where(a => a.SchemaAttribute.Id == attrSchemaId && a.ValueInteger == value)
.ToListAsync();
return result;
}
public async Task BulkInsert(IEnumerable<SCIMRepresentationAttribute> scimRepresentationAttributes, string currentRepresentationId, bool isReference = false)
{
scimRepresentationAttributes = scimRepresentationAttributes.Where(r => !string.IsNullOrWhiteSpace(r.RepresentationId)).ToList();
foreach (var attr in scimRepresentationAttributes)
attr.SchemaAttributeId = attr.SchemaAttribute?.Id;
await _scimDbContext.AddRangeAsync(scimRepresentationAttributes);
}
public async Task BulkDelete(IEnumerable<SCIMRepresentationAttribute> scimRepresentationAttributes, string currentRepresentationId, bool isReference = false)
{
scimRepresentationAttributes = scimRepresentationAttributes.Where(r => !string.IsNullOrWhiteSpace(r.RepresentationId)).ToList();
var merged = LinqToDB.LinqExtensions.DeleteWhenMatched(
LinqToDB.LinqExtensions.On(
LinqToDB.LinqExtensions.Using(
LinqToDB.LinqExtensions.Merge(
_scimDbContext.SCIMRepresentationAttributeLst.ToLinqToDBTable()),
scimRepresentationAttributes
),
(g1, g2) => g1.Id == g2.Id
));
await LinqToDB.LinqExtensions.MergeAsync(merged);
}
public async Task BulkUpdate(IEnumerable<SCIMRepresentationAttribute> scimRepresentationAttributes, bool isReference = false)
{
scimRepresentationAttributes = scimRepresentationAttributes.Where(r => !string.IsNullOrWhiteSpace(r.RepresentationId)).ToList();
foreach (var attr in scimRepresentationAttributes)
attr.SchemaAttributeId = attr.SchemaAttribute?.Id;
var merged = LinqToDB.LinqExtensions.UpdateWhenMatched(
LinqToDB.LinqExtensions.On(
LinqToDB.LinqExtensions.Using(
LinqToDB.LinqExtensions.Merge(
_scimDbContext.SCIMRepresentationAttributeLst.ToLinqToDBTable()),
scimRepresentationAttributes
),
(g1, g2) => g1.Id == g2.Id
), (target, source) => new SCIMRepresentationAttribute
{
ValueString = source.ValueString,
ValueDecimal = source.ValueDecimal,
ValueBoolean = source.ValueBoolean,
ValueBinary = source.ValueBinary,
ValueReference = source.ValueReference,
ValueInteger = source.ValueInteger,
ValueDateTime = source.ValueDateTime,
ComputedValueIndex = source.ComputedValueIndex
});
await LinqToDB.LinqExtensions.MergeAsync(merged);
}
public async Task<ITransaction> StartTransaction(CancellationToken token)
{
var transaction = await _scimDbContext.Database.BeginTransactionAsync(token);
return new EFTransaction(_scimDbContext, transaction);
}
private void ResolveChildren(IQueryable<SCIMRepresentationAttribute> representationAttributes, string parentId, List<SCIMRepresentationAttribute> children)
{
var filteredAttributes = representationAttributes.Where(a => a.ParentAttributeId == parentId);
children.AddRange(filteredAttributes);
foreach (var fAttr in filteredAttributes) ResolveChildren(representationAttributes, fAttr.Id, children);
}
}