-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathUserStore.cs
More file actions
1016 lines (947 loc) · 34.8 KB
/
UserStore.cs
File metadata and controls
1016 lines (947 loc) · 34.8 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation, Inc. All rights reserved.
// Licensed under the MIT License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Identity.EntityFramework
{
/// <summary>
/// EntityFramework based user store implementation that supports IUserStore, IUserLoginStore, IUserClaimStore and
/// IUserRoleStore
/// </summary>
/// <typeparam name="TUser"></typeparam>
public class UserStore<TUser> :
UserStore<TUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>,
IUserStore<TUser> where TUser : IdentityUser
{
/// <summary>
/// Default constuctor which uses a new instance of a default IdentityDbContext
/// </summary>
public UserStore()
: this(new IdentityDbContext())
{
DisposeContext = true;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="context"></param>
public UserStore(DbContext context)
: base(context)
{
}
}
/// <summary>
/// EntityFramework based user store implementation that supports IUserStore, IUserLoginStore, IUserClaimStore and
/// IUserRoleStore
/// </summary>
/// <typeparam name="TUser"></typeparam>
/// <typeparam name="TRole"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TUserLogin"></typeparam>
/// <typeparam name="TUserRole"></typeparam>
/// <typeparam name="TUserClaim"></typeparam>
public class UserStore<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> :
IUserLoginStore<TUser, TKey>,
IUserClaimStore<TUser, TKey>,
IUserRoleStore<TUser, TKey>,
IUserPasswordStore<TUser, TKey>,
IUserSecurityStampStore<TUser, TKey>,
IQueryableUserStore<TUser, TKey>,
IUserEmailStore<TUser, TKey>,
IUserPhoneNumberStore<TUser, TKey>,
IUserTwoFactorStore<TUser, TKey>,
IUserLockoutStore<TUser, TKey>
where TKey : IEquatable<TKey>
where TUser : IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : IdentityRole<TKey, TUserRole>
where TUserLogin : IdentityUserLogin<TKey>, new()
where TUserRole : IdentityUserRole<TKey>, new()
where TUserClaim : IdentityUserClaim<TKey>, new()
{
private readonly IDbSet<TUserLogin> _logins;
private readonly EntityStore<TRole> _roleStore;
private readonly IDbSet<TUserClaim> _userClaims;
private readonly IDbSet<TUserRole> _userRoles;
private bool _disposed;
private EntityStore<TUser> _userStore;
/// <summary>
/// Constructor which takes a db context and wires up the stores with default instances using the context
/// </summary>
/// <param name="context"></param>
public UserStore(DbContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
Context = context;
AutoSaveChanges = true;
_userStore = new EntityStore<TUser>(context);
_roleStore = new EntityStore<TRole>(context);
_logins = Context.Set<TUserLogin>();
_userClaims = Context.Set<TUserClaim>();
_userRoles = Context.Set<TUserRole>();
}
/// <summary>
/// Context for the store
/// </summary>
public DbContext Context { get; private set; }
/// <summary>
/// If true will call dispose on the DbContext during Dispose
/// </summary>
public bool DisposeContext { get; set; }
/// <summary>
/// If true will call SaveChanges after Create/Update/Delete
/// </summary>
public bool AutoSaveChanges { get; set; }
/// <summary>
/// Returns an IQueryable of users
/// </summary>
public IQueryable<TUser> Users
{
get { return _userStore.EntitySet; }
}
/// <summary>
/// Return the claims for a user
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual async Task<IList<Claim>> GetClaimsAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
await EnsureClaimsLoaded(user).WithCurrentCulture();
return user.Claims.Select(c => new Claim(c.ClaimType, c.ClaimValue)).ToList();
}
/// <summary>
/// Add a claim to a user
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <returns></returns>
public virtual Task AddClaimAsync(TUser user, Claim claim)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (claim == null)
{
throw new ArgumentNullException("claim");
}
_userClaims.Add(new TUserClaim { UserId = user.Id, ClaimType = claim.Type, ClaimValue = claim.Value });
return Task.FromResult(0);
}
/// <summary>
/// Remove a claim from a user
/// </summary>
/// <param name="user"></param>
/// <param name="claim"></param>
/// <returns></returns>
public virtual async Task RemoveClaimAsync(TUser user, Claim claim)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (claim == null)
{
throw new ArgumentNullException("claim");
}
IEnumerable<TUserClaim> claims;
var claimValue = claim.Value;
var claimType = claim.Type;
if (AreClaimsLoaded(user))
{
claims = user.Claims.Where(uc => uc.ClaimValue == claimValue && uc.ClaimType == claimType).ToList();
}
else
{
var userId = user.Id;
claims = await _userClaims.Where(uc => uc.ClaimValue == claimValue && uc.ClaimType == claimType && uc.UserId.Equals(userId)).ToListAsync().WithCurrentCulture();
}
foreach (var c in claims)
{
_userClaims.Remove(c);
}
}
/// <summary>
/// Returns whether the user email is confirmed
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<bool> GetEmailConfirmedAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.EmailConfirmed);
}
/// <summary>
/// Set IsConfirmed on the user
/// </summary>
/// <param name="user"></param>
/// <param name="confirmed"></param>
/// <returns></returns>
public virtual Task SetEmailConfirmedAsync(TUser user, bool confirmed)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.EmailConfirmed = confirmed;
return Task.FromResult(0);
}
/// <summary>
/// Set the user email
/// </summary>
/// <param name="user"></param>
/// <param name="email"></param>
/// <returns></returns>
public virtual Task SetEmailAsync(TUser user, string email)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.Email = email;
return Task.FromResult(0);
}
/// <summary>
/// Get the user's email
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<string> GetEmailAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.Email);
}
/// <summary>
/// Find a user by email
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public virtual Task<TUser> FindByEmailAsync(string email)
{
ThrowIfDisposed();
return GetUserAggregateAsync(u => u.Email.ToUpper() == email.ToUpper());
}
/// <summary>
/// Returns the DateTimeOffset that represents the end of a user's lockout, any time in the past should be considered
/// not locked out.
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<DateTimeOffset> GetLockoutEndDateAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return
Task.FromResult(user.LockoutEndDateUtc.HasValue
? new DateTimeOffset(DateTime.SpecifyKind(user.LockoutEndDateUtc.Value, DateTimeKind.Utc))
: new DateTimeOffset());
}
/// <summary>
/// Locks a user out until the specified end date (set to a past date, to unlock a user)
/// </summary>
/// <param name="user"></param>
/// <param name="lockoutEnd"></param>
/// <returns></returns>
public virtual Task SetLockoutEndDateAsync(TUser user, DateTimeOffset lockoutEnd)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.LockoutEndDateUtc = lockoutEnd == DateTimeOffset.MinValue ? (DateTime?)null : lockoutEnd.UtcDateTime;
return Task.FromResult(0);
}
/// <summary>
/// Used to record when an attempt to access the user has failed
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<int> IncrementAccessFailedCountAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.AccessFailedCount++;
return Task.FromResult(user.AccessFailedCount);
}
/// <summary>
/// Used to reset the account access count, typically after the account is successfully accessed
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task ResetAccessFailedCountAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.AccessFailedCount = 0;
return Task.FromResult(0);
}
/// <summary>
/// Returns the current number of failed access attempts. This number usually will be reset whenever the password is
/// verified or the account is locked out.
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<int> GetAccessFailedCountAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.AccessFailedCount);
}
/// <summary>
/// Returns whether the user can be locked out.
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<bool> GetLockoutEnabledAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.LockoutEnabled);
}
/// <summary>
/// Sets whether the user can be locked out.
/// </summary>
/// <param name="user"></param>
/// <param name="enabled"></param>
/// <returns></returns>
public virtual Task SetLockoutEnabledAsync(TUser user, bool enabled)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.LockoutEnabled = enabled;
return Task.FromResult(0);
}
/// <summary>
/// Find a user by id
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public virtual Task<TUser> FindByIdAsync(TKey userId)
{
ThrowIfDisposed();
return GetUserAggregateAsync(u => u.Id.Equals(userId));
}
/// <summary>
/// Find a user by name
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public virtual Task<TUser> FindByNameAsync(string userName)
{
ThrowIfDisposed();
return GetUserAggregateAsync(u => u.UserName.ToUpper() == userName.ToUpper());
}
/// <summary>
/// Insert an entity
/// </summary>
/// <param name="user"></param>
public virtual async Task CreateAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
_userStore.Create(user);
await SaveChanges().WithCurrentCulture();
}
/// <summary>
/// Mark an entity for deletion
/// </summary>
/// <param name="user"></param>
public virtual async Task DeleteAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
_userStore.Delete(user);
await SaveChanges().WithCurrentCulture();
}
/// <summary>
/// Update an entity
/// </summary>
/// <param name="user"></param>
public virtual async Task UpdateAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
_userStore.Update(user);
await SaveChanges().WithCurrentCulture();
}
/// <summary>
/// Dispose the store
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// IUserLogin implementation
/// <summary>
/// Returns the user associated with this login
/// </summary>
/// <returns></returns>
public virtual async Task<TUser> FindAsync(UserLoginInfo login)
{
ThrowIfDisposed();
if (login == null)
{
throw new ArgumentNullException("login");
}
var provider = login.LoginProvider;
var key = login.ProviderKey;
var userLogin =
await _logins.FirstOrDefaultAsync(l => l.LoginProvider == provider && l.ProviderKey == key).WithCurrentCulture();
if (userLogin != null)
{
var userId = userLogin.UserId;
return await GetUserAggregateAsync(u => u.Id.Equals(userId)).WithCurrentCulture();
}
return null;
}
/// <summary>
/// Add a login to the user
/// </summary>
/// <param name="user"></param>
/// <param name="login"></param>
/// <returns></returns>
public virtual Task AddLoginAsync(TUser user, UserLoginInfo login)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (login == null)
{
throw new ArgumentNullException("login");
}
_logins.Add(new TUserLogin
{
UserId = user.Id,
ProviderKey = login.ProviderKey,
LoginProvider = login.LoginProvider
});
return Task.FromResult(0);
}
/// <summary>
/// Remove a login from a user
/// </summary>
/// <param name="user"></param>
/// <param name="login"></param>
/// <returns></returns>
public virtual async Task RemoveLoginAsync(TUser user, UserLoginInfo login)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (login == null)
{
throw new ArgumentNullException("login");
}
TUserLogin entry;
var provider = login.LoginProvider;
var key = login.ProviderKey;
if (AreLoginsLoaded(user))
{
entry = user.Logins.SingleOrDefault(ul => ul.LoginProvider == provider && ul.ProviderKey == key);
}
else
{
var userId = user.Id;
entry = await _logins.SingleOrDefaultAsync(ul => ul.LoginProvider == provider && ul.ProviderKey == key && ul.UserId.Equals(userId)).WithCurrentCulture();
}
if (entry != null)
{
_logins.Remove(entry);
}
}
/// <summary>
/// Get the logins for a user
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual async Task<IList<UserLoginInfo>> GetLoginsAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
await EnsureLoginsLoaded(user).WithCurrentCulture();
return user.Logins.Select(l => new UserLoginInfo(l.LoginProvider, l.ProviderKey)).ToList();
}
/// <summary>
/// Set the password hash for a user
/// </summary>
/// <param name="user"></param>
/// <param name="passwordHash"></param>
/// <returns></returns>
public virtual Task SetPasswordHashAsync(TUser user, string passwordHash)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.PasswordHash = passwordHash;
return Task.FromResult(0);
}
/// <summary>
/// Get the password hash for a user
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<string> GetPasswordHashAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.PasswordHash);
}
/// <summary>
/// Returns true if the user has a password set
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<bool> HasPasswordAsync(TUser user)
{
return Task.FromResult(user.PasswordHash != null);
}
/// <summary>
/// Set the user's phone number
/// </summary>
/// <param name="user"></param>
/// <param name="phoneNumber"></param>
/// <returns></returns>
public virtual Task SetPhoneNumberAsync(TUser user, string phoneNumber)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.PhoneNumber = phoneNumber;
return Task.FromResult(0);
}
/// <summary>
/// Get a user's phone number
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<string> GetPhoneNumberAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.PhoneNumber);
}
/// <summary>
/// Returns whether the user phoneNumber is confirmed
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<bool> GetPhoneNumberConfirmedAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.PhoneNumberConfirmed);
}
/// <summary>
/// Set PhoneNumberConfirmed on the user
/// </summary>
/// <param name="user"></param>
/// <param name="confirmed"></param>
/// <returns></returns>
public virtual Task SetPhoneNumberConfirmedAsync(TUser user, bool confirmed)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.PhoneNumberConfirmed = confirmed;
return Task.FromResult(0);
}
/// <summary>
/// Add a user to a role
/// </summary>
/// <param name="user"></param>
/// <param name="roleName"></param>
/// <returns></returns>
public virtual async Task AddToRoleAsync(TUser user, string roleName)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (String.IsNullOrWhiteSpace(roleName))
{
throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
}
var roleEntity = await _roleStore.DbEntitySet.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper()).WithCurrentCulture();
if (roleEntity == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
IdentityResources.RoleNotFound, roleName));
}
var ur = new TUserRole { UserId = user.Id, RoleId = roleEntity.Id };
_userRoles.Add(ur);
}
/// <summary>
/// Remove a user from a role
/// </summary>
/// <param name="user"></param>
/// <param name="roleName"></param>
/// <returns></returns>
public virtual async Task RemoveFromRoleAsync(TUser user, string roleName)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (String.IsNullOrWhiteSpace(roleName))
{
throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
}
var roleEntity = await _roleStore.DbEntitySet.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper()).WithCurrentCulture();
if (roleEntity != null)
{
var roleId = roleEntity.Id;
var userId = user.Id;
var userRole = await _userRoles.FirstOrDefaultAsync(r => roleId.Equals(r.RoleId) && r.UserId.Equals(userId)).WithCurrentCulture();
if (userRole != null)
{
_userRoles.Remove(userRole);
}
}
}
/// <summary>
/// Get the names of the roles a user is a member of
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual async Task<IList<string>> GetRolesAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
var userId = user.Id;
var query = from userRole in _userRoles
where userRole.UserId.Equals(userId)
join role in _roleStore.DbEntitySet on userRole.RoleId equals role.Id
select role.Name;
return await query.ToListAsync().WithCurrentCulture();
}
/// <summary>
/// Returns true if the user is in the named role
/// </summary>
/// <param name="user"></param>
/// <param name="roleName"></param>
/// <returns></returns>
public virtual async Task<bool> IsInRoleAsync(TUser user, string roleName)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
if (String.IsNullOrWhiteSpace(roleName))
{
throw new ArgumentException(IdentityResources.ValueCannotBeNullOrEmpty, "roleName");
}
var role = await _roleStore.DbEntitySet.SingleOrDefaultAsync(r => r.Name.ToUpper() == roleName.ToUpper()).WithCurrentCulture();
if (role != null)
{
var userId = user.Id;
var roleId = role.Id;
return await _userRoles.AnyAsync(ur => ur.RoleId.Equals(roleId) && ur.UserId.Equals(userId)).WithCurrentCulture();
}
return false;
}
/// <summary>
/// Set the security stamp for the user
/// </summary>
/// <param name="user"></param>
/// <param name="stamp"></param>
/// <returns></returns>
public virtual Task SetSecurityStampAsync(TUser user, string stamp)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.SecurityStamp = stamp;
return Task.FromResult(0);
}
/// <summary>
/// Get the security stamp for a user
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<string> GetSecurityStampAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.SecurityStamp);
}
/// <summary>
/// Set whether two factor authentication is enabled for the user
/// </summary>
/// <param name="user"></param>
/// <param name="enabled"></param>
/// <returns></returns>
public virtual Task SetTwoFactorEnabledAsync(TUser user, bool enabled)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
user.TwoFactorEnabled = enabled;
return Task.FromResult(0);
}
/// <summary>
/// Gets whether two factor authentication is enabled for the user
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual Task<bool> GetTwoFactorEnabledAsync(TUser user)
{
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.TwoFactorEnabled);
}
// Only call save changes if AutoSaveChanges is true
private async Task SaveChanges()
{
if (AutoSaveChanges)
{
await Context.SaveChangesAsync().WithCurrentCulture();
}
}
private bool AreClaimsLoaded(TUser user)
{
return Context.Entry(user).Collection(u => u.Claims).IsLoaded;
}
private async Task EnsureClaimsLoaded(TUser user)
{
if (!AreClaimsLoaded(user))
{
var userId = user.Id;
await _userClaims.Where(uc => uc.UserId.Equals(userId)).LoadAsync().WithCurrentCulture();
Context.Entry(user).Collection(u => u.Claims).IsLoaded = true;
}
}
private async Task EnsureRolesLoaded(TUser user)
{
if (!Context.Entry(user).Collection(u => u.Roles).IsLoaded)
{
var userId = user.Id;
await _userRoles.Where(uc => uc.UserId.Equals(userId)).LoadAsync().WithCurrentCulture();
Context.Entry(user).Collection(u => u.Roles).IsLoaded = true;
}
}
private bool AreLoginsLoaded(TUser user)
{
return Context.Entry(user).Collection(u => u.Logins).IsLoaded;
}
private async Task EnsureLoginsLoaded(TUser user)
{
if (!AreLoginsLoaded(user))
{
var userId = user.Id;
await _logins.Where(uc => uc.UserId.Equals(userId)).LoadAsync().WithCurrentCulture();
Context.Entry(user).Collection(u => u.Logins).IsLoaded = true;
}
}
/// <summary>
/// Used to attach child entities to the User aggregate, i.e. Roles, Logins, and Claims
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
protected virtual async Task<TUser> GetUserAggregateAsync(Expression<Func<TUser, bool>> filter)
{
TKey id;
TUser user;
if (FindByIdFilterParser.TryMatchAndGetId(filter, out id))
{
user = await _userStore.GetByIdAsync(id).WithCurrentCulture();
}
else
{
user = await Users.FirstOrDefaultAsync(filter).WithCurrentCulture();
}
if (user != null)
{
await EnsureClaimsLoaded(user).WithCurrentCulture();
await EnsureLoginsLoaded(user).WithCurrentCulture();
await EnsureRolesLoaded(user).WithCurrentCulture();
}
return user;
}
private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name);
}
}
/// <summary>
/// If disposing, calls dispose on the Context. Always nulls out the Context
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (DisposeContext && disposing && Context != null)
{
Context.Dispose();
}
_disposed = true;
Context = null;
_userStore = null;
}
// We want to use FindAsync() when looking for an User.Id instead of LINQ to avoid extra
// database roundtrips. This class cracks open the filter expression passed by
// UserStore.FindByIdAsync() to obtain the value of the id we are looking for
private static class FindByIdFilterParser
{
// expression pattern we need to match
private static readonly Expression<Func<TUser, bool>> Predicate = u => u.Id.Equals(default(TKey));
// method we need to match: Object.Equals()
private static readonly MethodInfo EqualsMethodInfo = ((MethodCallExpression)Predicate.Body).Method;
// property access we need to match: User.Id
private static readonly MemberInfo UserIdMemberInfo = ((MemberExpression)((MethodCallExpression)Predicate.Body).Object).Member;
internal static bool TryMatchAndGetId(Expression<Func<TUser, bool>> filter, out TKey id)
{
// default value in case we can’t obtain it
id = default(TKey);
// lambda body should be a call
if (filter.Body.NodeType != ExpressionType.Call)
{
return false;
}
// actually a call to object.Equals(object)
var callExpression = (MethodCallExpression)filter.Body;
if (callExpression.Method != EqualsMethodInfo)
{
return false;
}
// left side of Equals() should be an access to User.Id
if (callExpression.Object == null
|| callExpression.Object.NodeType != ExpressionType.MemberAccess
|| ((MemberExpression)callExpression.Object).Member != UserIdMemberInfo)
{
return false;
}
// There should be only one argument for Equals()
if (callExpression.Arguments.Count != 1)
{
return false;
}
MemberExpression fieldAccess;
if (callExpression.Arguments[0].NodeType == ExpressionType.Convert)
{
// convert node should have an member access access node
// This is for cases when primary key is a value type
var convert = (UnaryExpression)callExpression.Arguments[0];
if (convert.Operand.NodeType != ExpressionType.MemberAccess)
{
return false;
}
fieldAccess = (MemberExpression)convert.Operand;
}
else if (callExpression.Arguments[0].NodeType == ExpressionType.MemberAccess)
{
// Get field member for when key is reference type
fieldAccess = (MemberExpression)callExpression.Arguments[0];
}
else
{
return false;
}
// and member access should be a field access to a variable captured in a closure