Fixed issue with users when disabling then setting admin

Concurrency issue; moved user update from controller to service
This commit is contained in:
rustywave 2022-06-06 18:42:59 -07:00 committed by Andrew Camilleri
parent fcbe1dd8eb
commit 2ad2ce6c3b
2 changed files with 43 additions and 6 deletions

View File

@ -109,12 +109,15 @@ namespace BTCPayServer.Controllers
if (viewModel.IsAdmin != wasAdmin)
{
if (viewModel.IsAdmin)
await _UserManager.AddToRoleAsync(user, Roles.ServerAdmin);
var success = await _userService.SetAdminUser(user.Id, viewModel.IsAdmin);
if (success)
{
TempData[WellKnownTempData.SuccessMessage] = "User successfully updated";
}
else
await _UserManager.RemoveFromRoleAsync(user, Roles.ServerAdmin);
TempData[WellKnownTempData.SuccessMessage] = "User successfully updated";
{
TempData[WellKnownTempData.ErrorMessage] = "Error updating user";
}
}
return RedirectToAction(nameof(User), new { userId = userId });

View File

@ -99,6 +99,31 @@ namespace BTCPayServer.Services
return IsRoleAdmin(await _userManager.GetRolesAsync(user));
}
public async Task<bool> SetAdminUser(string userId, bool enableAdmin)
{
var user = await _userManager.FindByIdAsync(userId);
IdentityResult res;
if (enableAdmin)
{
res = await _userManager.AddToRoleAsync(user, Roles.ServerAdmin);
}
else
{
res = await _userManager.RemoveFromRoleAsync(user, Roles.ServerAdmin);
}
if (res.Succeeded)
{
_logger.LogInformation($"Successfully set admin status for user {user.Id}");
}
else
{
_logger.LogError($"Error setting admin status for user {user.Id}");
}
return res.Succeeded;
}
public async Task DeleteUserAndAssociatedData(ApplicationUser user)
{
var userId = user.Id;
@ -110,7 +135,16 @@ namespace BTCPayServer.Services
await Task.WhenAll(files.Select(file => _fileService.RemoveFile(file.Id, userId)));
user = await _userManager.FindByIdAsync(userId);
await _userManager.DeleteAsync(user);
var res = await _userManager.DeleteAsync(user);
if (res.Succeeded)
{
_logger.LogInformation($"User {user.Id} was successfully deleted");
}
else
{
_logger.LogError($"Failed to delete user {user.Id}");
}
await _storeRepository.CleanUnreachableStores();
}