mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
Add IsRoleAdmin to user service
This commit is contained in:
@@ -198,7 +198,7 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
|
|
||||||
var roles = await _userManager.GetRolesAsync(user);
|
var roles = await _userManager.GetRolesAsync(user);
|
||||||
// We can safely delete the user if it's not an admin user
|
// We can safely delete the user if it's not an admin user
|
||||||
if (!IsAdmin(roles))
|
if (!_userService.IsRoleAdmin(roles))
|
||||||
{
|
{
|
||||||
await _userService.DeleteUserAndAssociatedData(user);
|
await _userService.DeleteUserAndAssociatedData(user);
|
||||||
|
|
||||||
@@ -218,8 +218,6 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private async Task<Boolean> IsAdmin()
|
private async Task<Boolean> IsAdmin()
|
||||||
{
|
{
|
||||||
var anyAdmin = (await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin)).Any();
|
var anyAdmin = (await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin)).Any();
|
||||||
@@ -235,11 +233,6 @@ namespace BTCPayServer.Controllers.GreenField
|
|||||||
&& isAuth;
|
&& isAuth;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsAdmin(IList<string> roles)
|
|
||||||
{
|
|
||||||
return roles.Contains(Roles.ServerAdmin, StringComparer.Ordinal);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<ApplicationUserData> FromModel(ApplicationUser data)
|
private async Task<ApplicationUserData> FromModel(ApplicationUser data)
|
||||||
{
|
{
|
||||||
var roles = (await _userManager.GetRolesAsync(data)).ToArray();
|
var roles = (await _userManager.GetRolesAsync(data)).ToArray();
|
||||||
|
|||||||
@@ -79,16 +79,11 @@ namespace BTCPayServer.Controllers
|
|||||||
Id = user.Id,
|
Id = user.Id,
|
||||||
Email = user.Email,
|
Email = user.Email,
|
||||||
Verified = user.EmailConfirmed || !user.RequiresEmailConfirmation,
|
Verified = user.EmailConfirmed || !user.RequiresEmailConfirmation,
|
||||||
IsAdmin = IsAdmin(roles)
|
IsAdmin = _userService.IsRoleAdmin(roles)
|
||||||
};
|
};
|
||||||
return View(userVM);
|
return View(userVM);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsAdmin(IList<string> roles)
|
|
||||||
{
|
|
||||||
return roles.Contains(Roles.ServerAdmin, StringComparer.Ordinal);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Route("server/users/{userId}")]
|
[Route("server/users/{userId}")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public new async Task<IActionResult> User(string userId, UsersViewModel.UserViewModel viewModel)
|
public new async Task<IActionResult> User(string userId, UsersViewModel.UserViewModel viewModel)
|
||||||
@@ -99,7 +94,7 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||||
var roles = await _UserManager.GetRolesAsync(user);
|
var roles = await _UserManager.GetRolesAsync(user);
|
||||||
var wasAdmin = IsAdmin(roles);
|
var wasAdmin = _userService.IsRoleAdmin(roles);
|
||||||
if (!viewModel.IsAdmin && admins.Count == 1 && wasAdmin)
|
if (!viewModel.IsAdmin && admins.Count == 1 && wasAdmin)
|
||||||
{
|
{
|
||||||
TempData[WellKnownTempData.ErrorMessage] = "This is the only Admin, so their role can't be removed until another Admin is added.";
|
TempData[WellKnownTempData.ErrorMessage] = "This is the only Admin, so their role can't be removed until another Admin is added.";
|
||||||
@@ -206,7 +201,7 @@ namespace BTCPayServer.Controllers
|
|||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
var roles = await _UserManager.GetRolesAsync(user);
|
var roles = await _UserManager.GetRolesAsync(user);
|
||||||
if (IsAdmin(roles))
|
if (_userService.IsRoleAdmin(roles))
|
||||||
{
|
{
|
||||||
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||||
if (admins.Count == 1)
|
if (admins.Count == 1)
|
||||||
|
|||||||
@@ -1,27 +1,33 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
using BTCPayServer.Data;
|
using BTCPayServer.Data;
|
||||||
using BTCPayServer.Storage.Services;
|
using BTCPayServer.Storage.Services;
|
||||||
using BTCPayServer.Services.Stores;
|
using BTCPayServer.Services.Stores;
|
||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
|
|
||||||
namespace BTCPayServer.Services
|
namespace BTCPayServer.Services
|
||||||
{
|
{
|
||||||
public class UserService
|
public class UserService
|
||||||
{
|
{
|
||||||
private readonly UserManager<ApplicationUser> _userManager;
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
|
private readonly IAuthorizationService _authorizationService;
|
||||||
private readonly StoredFileRepository _storedFileRepository;
|
private readonly StoredFileRepository _storedFileRepository;
|
||||||
private readonly FileService _fileService;
|
private readonly FileService _fileService;
|
||||||
private readonly StoreRepository _storeRepository;
|
private readonly StoreRepository _storeRepository;
|
||||||
|
|
||||||
public UserService(
|
public UserService(
|
||||||
UserManager<ApplicationUser> userManager,
|
UserManager<ApplicationUser> userManager,
|
||||||
|
IAuthorizationService authorizationService,
|
||||||
StoredFileRepository storedFileRepository,
|
StoredFileRepository storedFileRepository,
|
||||||
FileService fileService,
|
FileService fileService,
|
||||||
StoreRepository storeRepository
|
StoreRepository storeRepository
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
|
_authorizationService = authorizationService;
|
||||||
_storedFileRepository = storedFileRepository;
|
_storedFileRepository = storedFileRepository;
|
||||||
_fileService = fileService;
|
_fileService = fileService;
|
||||||
_storeRepository = storeRepository;
|
_storeRepository = storeRepository;
|
||||||
@@ -40,5 +46,10 @@ namespace BTCPayServer.Services
|
|||||||
await _userManager.DeleteAsync(user);
|
await _userManager.DeleteAsync(user);
|
||||||
await _storeRepository.CleanUnreachableStores();
|
await _storeRepository.CleanUnreachableStores();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsRoleAdmin(IList<string> roles)
|
||||||
|
{
|
||||||
|
return roles.Contains(Roles.ServerAdmin, StringComparer.Ordinal);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user