mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-03-03 17:36:59 +01:00
Simplify DeleteUser method
This commit is contained in:
parent
2ed8341403
commit
104092702a
1 changed files with 3 additions and 6 deletions
|
@ -183,9 +183,8 @@ namespace BTCPayServer.Controllers.GreenField
|
|||
[Authorize(Policy = Policies.CanDeleteUser, AuthenticationSchemes = AuthenticationSchemes.GreenfieldAPIKeys)]
|
||||
public async Task<ActionResult<ApplicationUserData>> DeleteUser(string userId)
|
||||
{
|
||||
var isAdmin = await IsAdmin();
|
||||
// Only admins should be allowed to delete users
|
||||
if (!isAdmin)
|
||||
if (!User.IsInRole(Roles.ServerAdmin))
|
||||
{
|
||||
return Forbid(AuthenticationSchemes.GreenfieldBasic);
|
||||
}
|
||||
|
@ -196,18 +195,16 @@ namespace BTCPayServer.Controllers.GreenField
|
|||
return NotFound();
|
||||
}
|
||||
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
// We can safely delete the user if it's not an admin user
|
||||
if (!_userService.IsRoleAdmin(roles))
|
||||
if (!_userService.IsRoleAdmin(await _userManager.GetRolesAsync(user)))
|
||||
{
|
||||
await _userService.DeleteUserAndAssociatedData(user);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||
// User shouldn't be deleted if it's the only admin
|
||||
if (admins.Count == 1)
|
||||
if ((await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin)).Count == 1)
|
||||
{
|
||||
return Forbid(AuthenticationSchemes.GreenfieldBasic);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue