Skip to content

Commit e9b4147

Browse files
feat: Add virtual ConfigureCache method to CustomUserManager (#2821)
1 parent ccbfd88 commit e9b4147

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

src/VirtoCommerce.Platform.Security/CustomUserManager.cs

+41-40
Original file line numberDiff line numberDiff line change
@@ -43,82 +43,82 @@ public CustomUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOpt
4343
_passwordHasher = passwordHasher;
4444
}
4545

46-
public override async Task<ApplicationUser> FindByLoginAsync(string loginProvider, string providerKey)
46+
public override Task<ApplicationUser> FindByLoginAsync(string loginProvider, string providerKey)
4747
{
4848
var cacheKey = CacheKey.With(GetType(), nameof(FindByLoginAsync), loginProvider, providerKey);
49-
var result = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
49+
return _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
5050
{
5151
var user = await base.FindByLoginAsync(loginProvider, providerKey);
52-
if (user != null)
52+
if (user is not null)
5353
{
5454
await LoadUserDetailsAsync(user);
55-
cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeTokenForUser(user));
55+
ConfigureCache(cacheEntry, user);
5656
}
5757
return user;
5858
}, cacheNullValue: false);
59-
60-
return result;
6159
}
6260

63-
public override async Task<ApplicationUser> FindByEmailAsync(string email)
61+
public override Task<ApplicationUser> FindByEmailAsync(string email)
6462
{
6563
var cacheKey = CacheKey.With(GetType(), nameof(FindByEmailAsync), email);
66-
var result = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
64+
return _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
6765
{
6866
var user = await base.FindByEmailAsync(email);
69-
if (user != null)
67+
if (user is not null)
7068
{
7169
await LoadUserDetailsAsync(user);
72-
cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeTokenForUser(user));
70+
ConfigureCache(cacheEntry, user);
7371
}
7472
return user;
7573
}, cacheNullValue: false);
76-
return result;
7774
}
7875

79-
public override async Task<ApplicationUser> FindByNameAsync(string userName)
76+
public override Task<ApplicationUser> FindByNameAsync(string userName)
8077
{
8178
var cacheKey = CacheKey.With(GetType(), nameof(FindByNameAsync), userName);
82-
var result = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
79+
return _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
8380
{
8481
var user = await base.FindByNameAsync(userName);
85-
if (user != null)
82+
if (user is not null)
8683
{
8784
await LoadUserDetailsAsync(user);
88-
cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeTokenForUser(user));
85+
ConfigureCache(cacheEntry, user);
8986
}
9087
return user;
9188
}, cacheNullValue: false);
92-
return result;
9389
}
9490

95-
public override async Task<ApplicationUser> FindByIdAsync(string userId)
91+
public override Task<ApplicationUser> FindByIdAsync(string userId)
9692
{
9793
var cacheKey = CacheKey.With(GetType(), nameof(FindByIdAsync), userId);
98-
var result = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
94+
return _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async cacheEntry =>
9995
{
10096
var user = await base.FindByIdAsync(userId);
101-
if (user != null)
97+
if (user is not null)
10298
{
10399
await LoadUserDetailsAsync(user);
104-
cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeTokenForUser(user));
100+
ConfigureCache(cacheEntry, user);
105101
}
106102
return user;
107103
}, cacheNullValue: false);
108-
return result;
104+
}
105+
106+
protected virtual void ConfigureCache(MemoryCacheEntryOptions cacheOptions, ApplicationUser user)
107+
{
108+
cacheOptions.AddExpirationToken(SecurityCacheRegion.CreateChangeTokenForUser(user));
109109
}
110110

111111
public override Task<IdentityResult> ResetPasswordAsync(ApplicationUser user, string token, string newPassword)
112112
{
113113
return UpdatePasswordAsync(user, newPassword,
114-
(user, newPassword) => base.ResetPasswordAsync(user, token, newPassword),
114+
(appUser, password) => base.ResetPasswordAsync(appUser, token, password),
115115
(userId, customPasswordHash) => new UserResetPasswordEvent(userId, customPasswordHash));
116116
}
117117

118118
public override Task<IdentityResult> ChangePasswordAsync(ApplicationUser user, string currentPassword, string newPassword)
119119
{
120120
return UpdatePasswordAsync(user, newPassword,
121-
(user, newPassword) => base.ChangePasswordAsync(user, currentPassword, newPassword),
121+
(appUser, password) => base.ChangePasswordAsync(appUser, currentPassword, password),
122122
(userId, customPasswordHash) => new UserChangedPasswordEvent(userId, customPasswordHash));
123123
}
124124

@@ -171,9 +171,10 @@ public override async Task<IdentityResult> DeleteAsync(ApplicationUser user)
171171
{
172172
var changedEntries = new List<GenericChangedEntry<ApplicationUser>>
173173
{
174-
new GenericChangedEntry<ApplicationUser>(user, EntryState.Deleted)
174+
new(user, EntryState.Deleted),
175175
};
176176
await _eventPublisher.Publish(new UserChangingEvent(changedEntries));
177+
177178
var result = await base.DeleteAsync(user);
178179
if (result.Succeeded)
179180
{
@@ -185,18 +186,18 @@ public override async Task<IdentityResult> DeleteAsync(ApplicationUser user)
185186

186187
protected override async Task<IdentityResult> UpdateUserAsync(ApplicationUser user)
187188
{
188-
var newUser = (ApplicationUser)user.Clone();
189+
var newUser = user.CloneTyped();
189190
var existentUser = await LoadExistingUser(user);
190191

191192
//We cant update not existing user
192-
if (existentUser == null)
193+
if (existentUser is null)
193194
{
194195
return IdentityResult.Failed(ErrorDescriber.DefaultError());
195196
}
196197

197198
var changedEntries = new List<GenericChangedEntry<ApplicationUser>>
198199
{
199-
new(newUser, (ApplicationUser)existentUser.Clone(), EntryState.Modified)
200+
new(newUser, existentUser.CloneTyped(), EntryState.Modified),
200201
};
201202

202203
await _eventPublisher.Publish(new UserChangingEvent(changedEntries));
@@ -233,7 +234,7 @@ public override async Task<IdentityResult> UpdateAsync(ApplicationUser user)
233234

234235
protected virtual async Task UpdateUserRolesAsync(ApplicationUser user)
235236
{
236-
if (user.Roles == null)
237+
if (user.Roles is null)
237238
{
238239
return;
239240
}
@@ -256,7 +257,7 @@ protected virtual async Task UpdateUserRolesAsync(ApplicationUser user)
256257

257258
protected virtual async Task UpdateUserLoginsAsync(ApplicationUser user)
258259
{
259-
if (user.Logins == null)
260+
if (user.Logins is null)
260261
{
261262
return;
262263
}
@@ -279,13 +280,14 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
279280
{
280281
var changedEntries = new List<GenericChangedEntry<ApplicationUser>>
281282
{
282-
new GenericChangedEntry<ApplicationUser>(user, EntryState.Added)
283+
new(user, EntryState.Added),
283284
};
284285
await _eventPublisher.Publish(new UserChangingEvent(changedEntries));
286+
285287
var result = await base.CreateAsync(user);
286288
if (result.Succeeded)
287289
{
288-
if (!user.Roles.IsNullOrEmpty())
290+
if (user.Roles?.Count > 0)
289291
{
290292
//Add
291293
foreach (var newRole in user.Roles)
@@ -295,7 +297,7 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
295297
}
296298

297299
// add external logins
298-
if (!user.Logins.IsNullOrEmpty())
300+
if (user.Logins?.Length > 0)
299301
{
300302
foreach (var login in user.Logins)
301303
{
@@ -306,6 +308,7 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user)
306308
SecurityCacheRegion.ExpireUser(user);
307309
await _eventPublisher.Publish(new UserChangedEvent(changedEntries));
308310
}
311+
309312
return result;
310313
}
311314

@@ -319,7 +322,6 @@ public override async Task<IdentityResult> CreateAsync(ApplicationUser user, str
319322
}
320323

321324
return result;
322-
323325
}
324326

325327
public override async Task<IdentityResult> AddToRoleAsync(ApplicationUser user, string role)
@@ -329,6 +331,7 @@ public override async Task<IdentityResult> AddToRoleAsync(ApplicationUser user,
329331
{
330332
await _eventPublisher.Publish(new UserRoleAddedEvent(user, role));
331333
}
334+
332335
return result;
333336
}
334337

@@ -339,6 +342,7 @@ public override async Task<IdentityResult> RemoveFromRoleAsync(ApplicationUser u
339342
{
340343
await _eventPublisher.Publish(new UserRoleRemovedEvent(user, role));
341344
}
345+
342346
return result;
343347
}
344348

@@ -350,10 +354,7 @@ public override async Task<IdentityResult> RemoveFromRoleAsync(ApplicationUser u
350354
/// <returns></returns>
351355
protected virtual async Task LoadUserDetailsAsync(ApplicationUser user)
352356
{
353-
if (user == null)
354-
{
355-
throw new ArgumentNullException(nameof(user));
356-
}
357+
ArgumentNullException.ThrowIfNull(user);
357358

358359
// check password expiry policy and mark password as expired, if needed
359360
var lastPasswordChangeDate = user.LastPasswordChangedDate ?? user.CreatedDate;
@@ -369,7 +370,7 @@ protected virtual async Task LoadUserDetailsAsync(ApplicationUser user)
369370
foreach (var roleName in await base.GetRolesAsync(user))
370371
{
371372
var role = await _roleManager.FindByNameAsync(roleName);
372-
if (role != null)
373+
if (role is not null)
373374
{
374375
user.Roles.Add(role);
375376
}
@@ -397,13 +398,13 @@ protected virtual async Task<ApplicationUser> LoadExistingUser(ApplicationUser u
397398
//It is important to call base.FindByIdAsync method to avoid of update a cached user.
398399
result = await base.FindByIdAsync(user.Id);
399400
}
400-
if (result == null)
401+
if (result is null)
401402
{
402403
//It is important to call base.FindByNameAsync method to avoid of update a cached user.
403404
result = await base.FindByNameAsync(user.UserName);
404405
}
405406

406-
if (result != null)
407+
if (result is not null)
407408
{
408409
await LoadUserDetailsAsync(result);
409410
}

0 commit comments

Comments
 (0)