forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryAuthRepository.cs
More file actions
375 lines (323 loc) · 13.6 KB
/
Copy pathInMemoryAuthRepository.cs
File metadata and controls
375 lines (323 loc) · 13.6 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ServiceStack.Auth
{
public class InMemoryAuthRepository : InMemoryAuthRepository<UserAuth, UserAuthDetails>
{
}
public interface IMemoryAuthRepository
: IUserAuthRepository, IClearable, IManageApiKeys, ICustomUserAuth
{
Dictionary<string, HashSet<string>> Sets { get; }
Dictionary<string, Dictionary<string, string>> Hashes { get; }
}
/// <summary>
/// Thread-safe In memory UserAuth data store so it can be used without a dependency on Redis.
/// </summary>
public class InMemoryAuthRepository<TUserAuth, TUserAuthDetails>
: RedisAuthRepository<TUserAuth, TUserAuthDetails>, IMemoryAuthRepository
where TUserAuth : class, IUserAuth
where TUserAuthDetails : class, IUserAuthDetails
{
public static readonly InMemoryAuthRepository<TUserAuth, TUserAuthDetails> Instance =
new InMemoryAuthRepository<TUserAuth, TUserAuthDetails>();
public Dictionary<string, HashSet<string>> Sets { get; set; }
public Dictionary<string, Dictionary<string, string>> Hashes { get; set; }
internal List<IClearable> TrackedTypes = new List<IClearable>();
internal class TypedData<T> : IClearable
{
internal static TypedData<T> Instance = new TypedData<T>();
private TypedData()
{
lock (InMemoryAuthRepository.Instance.TrackedTypes)
InMemoryAuthRepository.Instance.TrackedTypes.Add(this);
}
internal readonly List<T> Items = new List<T>();
internal int Sequence = 0;
public void Clear()
{
lock (Items) Items.Clear();
Interlocked.CompareExchange(ref Sequence, 0, Sequence);
}
}
public InMemoryAuthRepository()
: base(new InMemoryManagerFacade(Instance))
{
this.Sets = new Dictionary<string, HashSet<string>>();
this.Hashes = new Dictionary<string, Dictionary<string, string>>();
}
internal class InMemoryManagerFacade : IRedisClientManagerFacade
{
private readonly IMemoryAuthRepository root;
public InMemoryManagerFacade(IMemoryAuthRepository root)
{
this.root = root;
}
public IRedisClientFacade GetClient()
{
return new InMemoryClientFacade(root);
}
public void Clear()
{
lock (Instance.Sets) Instance.Sets.Clear();
lock (Instance.Hashes) Instance.Hashes.Clear();
lock (Instance.TrackedTypes) Instance.TrackedTypes.ForEach(x => x.Clear());
}
}
internal class InMemoryClientFacade : IRedisClientFacade
{
private readonly IMemoryAuthRepository root;
public InMemoryClientFacade(IMemoryAuthRepository root)
{
this.root = root;
}
class InMemoryTypedClientFacade<T> : ITypedRedisClientFacade<T>
{
private readonly IMemoryAuthRepository root;
public InMemoryTypedClientFacade(IMemoryAuthRepository root)
{
this.root = root;
}
public int GetNextSequence()
{
return Interlocked.Increment(ref TypedData<T>.Instance.Sequence);
}
public T GetById(object id)
{
if (id == null) return default(T);
lock (TypedData<T>.Instance.Items)
{
return TypedData<T>.Instance.Items.FirstOrDefault(x => id.ToString() == x.ToId().ToString());
}
}
public List<T> GetByIds(IEnumerable ids)
{
var idsSet = new HashSet<object>();
foreach (var id in ids) idsSet.Add(id.ToString());
lock (TypedData<T>.Instance.Items)
{
return TypedData<T>.Instance.Items.Where(x => idsSet.Contains(x.ToId().ToString())).ToList();
}
}
public void DeleteById(string id)
{
lock (TypedData<T>.Instance.Items)
{
TypedData<T>.Instance.Items.RemoveAll(x => x.GetId().ToString() == id);
}
}
public void DeleteByIds(IEnumerable ids)
{
var idsSet = new HashSet<object>();
foreach (var id in ids) idsSet.Add(id.ToString());
lock (TypedData<T>.Instance.Items)
{
TypedData<T>.Instance.Items.RemoveAll(x => idsSet.Contains(x.ToId().ToString()));
}
}
public List<T> GetAll(int? skip=null, int? take=null)
{
lock (TypedData<T>.Instance.Items)
{
if (skip != null || take != null)
{
var to = TypedData<T>.Instance.Items.AsEnumerable();
if (skip != null)
to = to.Skip(skip.Value);
if (take != null)
to = to.Take(take.Value);
return to.ToList();
}
return TypedData<T>.Instance.Items.ToList();
}
}
}
public HashSet<string> GetAllItemsFromSet(string setId)
{
lock (root.Sets)
{
return root.Sets.TryGetValue(setId, out var set) ? set : new HashSet<string>();
}
}
public void Store<T>(T item)
{
if (Equals(item, default(T))) return;
lock (TypedData<T>.Instance.Items)
{
for (var i = 0; i < TypedData<T>.Instance.Items.Count; i++)
{
var o = TypedData<T>.Instance.Items[i];
if (o.ToId().ToString() != item.ToId().ToString()) continue;
TypedData<T>.Instance.Items[i] = item;
return;
}
TypedData<T>.Instance.Items.Add(item);
}
}
public void DeleteById<T>(string id)
{
lock (TypedData<T>.Instance.Items)
{
TypedData<T>.Instance.Items.RemoveAll(x => x.GetId().ToString() == id);
}
}
public string GetValueFromHash(string hashId, string key)
{
if (hashId == null)
throw new ArgumentNullException(nameof(hashId));
if (key == null)
throw new ArgumentNullException(nameof(key));
lock (root.Hashes)
{
if (!root.Hashes.TryGetValue(hashId, out var hash)) return null;
hash.TryGetValue(key, out var value);
return value;
}
}
public void SetEntryInHash(string hashId, string key, string value)
{
if (hashId == null)
throw new ArgumentNullException(nameof(hashId));
if (key == null)
throw new ArgumentNullException(nameof(key));
lock (root.Hashes)
{
if (!root.Hashes.TryGetValue(hashId, out var hash))
root.Hashes[hashId] = hash = new Dictionary<string, string>();
hash[key] = value;
}
}
public void RemoveEntryFromHash(string hashId, string key)
{
if (hashId == null)
throw new ArgumentNullException(nameof(hashId));
if (key == null)
throw new ArgumentNullException(nameof(key));
lock (root.Hashes)
{
if (!root.Hashes.TryGetValue(hashId, out var hash))
root.Hashes[hashId] = hash = new Dictionary<string, string>();
hash.Remove(key);
}
}
public void AddItemToSet(string setId, string item)
{
lock (root.Sets)
{
if (!root.Sets.TryGetValue(setId, out var set))
root.Sets[setId] = set = new HashSet<string>();
set.Add(item);
}
}
public ITypedRedisClientFacade<T> As<T>()
{
return new InMemoryTypedClientFacade<T>(root);
}
public void Dispose()
{
}
}
}
public static class AuthRepositoryUtils
{
public static string ParseOrderBy(string orderBy, out bool desc)
{
desc = false;
if (string.IsNullOrEmpty(orderBy))
return null;
if (orderBy.IndexOf(' ') >= 0)
{
desc = orderBy.LastRightPart(' ').EqualsIgnoreCase("DESC");
orderBy = orderBy.LeftPart(' ');
}
else if (orderBy[0] == '-')
{
orderBy = orderBy.Substring(1);
desc = true;
}
return orderBy;
}
public static IEnumerable<TUserAuth> SortAndPage<TUserAuth>(this IEnumerable<TUserAuth> q, string orderBy, int? skip, int? take)
where TUserAuth : IUserAuth
{
if (!string.IsNullOrEmpty(orderBy))
{
orderBy = ParseOrderBy(orderBy, out var desc);
if (orderBy.EqualsIgnoreCase(nameof(IUserAuth.Id)))
{
q = desc
? q.OrderByDescending(x => x.Id)
: q.OrderBy(x => x.Id);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuth.PrimaryEmail)))
{
q = desc
? q.OrderByDescending(x => x.PrimaryEmail)
: q.OrderBy(x => x.PrimaryEmail);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuth.CreatedDate)))
{
q = desc
? q.OrderByDescending(x => x.CreatedDate)
: q.OrderBy(x => x.CreatedDate);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuth.ModifiedDate)))
{
q = desc
? q.OrderByDescending(x => x.ModifiedDate)
: q.OrderBy(x => x.ModifiedDate);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuth.LockedDate)))
{
q = desc
? q.OrderByDescending(x => x.LockedDate)
: q.OrderBy(x => x.LockedDate);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuthDetailsExtended.UserName)))
{
q = desc
? q.OrderByDescending(x => x is IUserAuthDetailsExtended u ? u.UserName : null)
: q.OrderBy(x => x is IUserAuthDetailsExtended u ? u.UserName : null);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuthDetailsExtended.DisplayName)))
{
q = desc
? q.OrderByDescending(x => x is IUserAuthDetailsExtended u ? u.DisplayName : null)
: q.OrderBy(x => x is IUserAuthDetailsExtended u ? u.DisplayName : null);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuthDetailsExtended.FirstName)))
{
q = desc
? q.OrderByDescending(x => x is IUserAuthDetailsExtended u ? u.FirstName : null)
: q.OrderBy(x => x is IUserAuthDetailsExtended u ? u.FirstName : null);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuthDetailsExtended.LastName)))
{
q = desc
? q.OrderByDescending(x => x is IUserAuthDetailsExtended u ? u.LastName : null)
: q.OrderBy(x => x is IUserAuthDetailsExtended u ? u.LastName : null);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuthDetailsExtended.Email)))
{
q = desc
? q.OrderByDescending(x => x is IUserAuthDetailsExtended u ? u.Email : null)
: q.OrderBy(x => x is IUserAuthDetailsExtended u ? u.Email : null);
}
else if (orderBy.EqualsIgnoreCase(nameof(IUserAuthDetailsExtended.Company)))
{
q = desc
? q.OrderByDescending(x => x is IUserAuthDetailsExtended u ? u.Company : null)
: q.OrderBy(x => x is IUserAuthDetailsExtended u ? u.Company : null);
}
}
if (skip != null)
q = q.Skip(skip.Value);
if (take != null)
q = q.Take(take.Value);
return q;
}
}
}