Use IExecutionStrategy
This commit is contained in:
@ -44,49 +44,54 @@ namespace Hcs.WebApp.Services
|
||||
public async Task UpdateUserAsync(string userId, string roleId, string password)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var transaction = await context.Database.BeginTransactionAsync();
|
||||
try
|
||||
var executionStrategy = context.Database.CreateExecutionStrategy();
|
||||
await executionStrategy.ExecuteAsync(async () =>
|
||||
{
|
||||
var curUserRole = await (from userRole in context.UserRoles
|
||||
where userRole.UserId == userId
|
||||
select userRole).SingleAsync();
|
||||
var curRole = await (from role in context.Roles
|
||||
where role.Id == curUserRole.RoleId
|
||||
select role).SingleAsync();
|
||||
if (curRole.Id != roleId)
|
||||
using var transaction = await context.Database.BeginTransactionAsync();
|
||||
try
|
||||
{
|
||||
context.UserRoles.Remove(curUserRole);
|
||||
|
||||
var newRole = await (from role in context.Roles
|
||||
where role.Id == roleId
|
||||
var curUserRole = await (from userRole in context.UserRoles
|
||||
where userRole.UserId == userId
|
||||
select userRole).SingleAsync();
|
||||
var curRole = await (from role in context.Roles
|
||||
where role.Id == curUserRole.RoleId
|
||||
select role).SingleAsync();
|
||||
await context.UserRoles.AddAsync(new IdentityUserRole<string>()
|
||||
if (curRole.Id != roleId)
|
||||
{
|
||||
UserId = userId,
|
||||
RoleId = newRole.Id
|
||||
});
|
||||
}
|
||||
context.UserRoles.Remove(curUserRole);
|
||||
|
||||
var curUser = await (from user in context.Users
|
||||
where user.Id == userId
|
||||
select user).SingleAsync();
|
||||
var newPasswordHash = passwordHasher.HashPassword(curUser, password);
|
||||
if (curUser.PasswordHash != newPasswordHash)
|
||||
var newRole = await (from role in context.Roles
|
||||
where role.Id == roleId
|
||||
select role).SingleAsync();
|
||||
await context.UserRoles.AddAsync(new IdentityUserRole<string>()
|
||||
{
|
||||
UserId = userId,
|
||||
RoleId = newRole.Id
|
||||
});
|
||||
}
|
||||
|
||||
var curUser = await (from user in context.Users
|
||||
where user.Id == userId
|
||||
select user).SingleAsync();
|
||||
var newPasswordHash = passwordHasher.HashPassword(curUser, password);
|
||||
if (curUser.PasswordHash != newPasswordHash)
|
||||
{
|
||||
curUser.PasswordHash = newPasswordHash;
|
||||
|
||||
context.Users.Update(curUser);
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
curUser.PasswordHash = newPasswordHash;
|
||||
await transaction.RollbackAsync();
|
||||
|
||||
context.Users.Update(curUser);
|
||||
throw;
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await transaction.RollbackAsync();
|
||||
|
||||
throw;
|
||||
}
|
||||
await transaction.CommitAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IdentityResult> DeleteUserAsync(string userId)
|
||||
|
||||
Reference in New Issue
Block a user