mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
Admins can approve registered users (#5647)
* Users list: Cleanups * Policies: Flip registration settings * Policies: Add RequireUserApproval setting * Add approval to user * Require approval on login and for API key * API handling * AccountController cleanups * Test fix * Apply suggestions from code review Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com> * Add missing imports * Communicate login requirements to user on account creation * Add login requirements to basic auth handler * Cleanups and test fix * Encapsulate approval logic in user service and log approval changes * Send follow up "Account approved" email Closes #5656. * Add notification for admins * Fix creating a user via the admin view * Update list: Unify flags into status column, add approve action * Adjust "Resend email" wording * Incorporate feedback from code review * Remove duplicate test server policy reset --------- Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer;
|
||||
using BTCPayServer.Client.Models;
|
||||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Events;
|
||||
using BTCPayServer.Services.Stores;
|
||||
using BTCPayServer.Storage.Services;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
@@ -20,6 +23,7 @@ namespace BTCPayServer.Services
|
||||
private readonly StoredFileRepository _storedFileRepository;
|
||||
private readonly FileService _fileService;
|
||||
private readonly StoreRepository _storeRepository;
|
||||
private readonly EventAggregator _eventAggregator;
|
||||
private readonly ApplicationDbContextFactory _applicationDbContextFactory;
|
||||
private readonly ILogger<UserService> _logger;
|
||||
|
||||
@@ -27,6 +31,7 @@ namespace BTCPayServer.Services
|
||||
IServiceProvider serviceProvider,
|
||||
StoredFileRepository storedFileRepository,
|
||||
FileService fileService,
|
||||
EventAggregator eventAggregator,
|
||||
StoreRepository storeRepository,
|
||||
ApplicationDbContextFactory applicationDbContextFactory,
|
||||
ILogger<UserService> logger)
|
||||
@@ -34,6 +39,7 @@ namespace BTCPayServer.Services
|
||||
_serviceProvider = serviceProvider;
|
||||
_storedFileRepository = storedFileRepository;
|
||||
_fileService = fileService;
|
||||
_eventAggregator = eventAggregator;
|
||||
_storeRepository = storeRepository;
|
||||
_applicationDbContextFactory = applicationDbContextFactory;
|
||||
_logger = logger;
|
||||
@@ -46,26 +52,89 @@ namespace BTCPayServer.Services
|
||||
(userRole, role) => role.Name).ToArray()))).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public static ApplicationUserData FromModel(ApplicationUser data, string?[] roles)
|
||||
{
|
||||
return new ApplicationUserData()
|
||||
return new ApplicationUserData
|
||||
{
|
||||
Id = data.Id,
|
||||
Email = data.Email,
|
||||
EmailConfirmed = data.EmailConfirmed,
|
||||
RequiresEmailConfirmation = data.RequiresEmailConfirmation,
|
||||
Approved = data.Approved,
|
||||
RequiresApproval = data.RequiresApproval,
|
||||
Created = data.Created,
|
||||
Roles = roles,
|
||||
Disabled = data.LockoutEnabled && data.LockoutEnd is not null && DateTimeOffset.UtcNow < data.LockoutEnd.Value.UtcDateTime
|
||||
};
|
||||
}
|
||||
|
||||
private bool IsDisabled(ApplicationUser user)
|
||||
private static bool IsEmailConfirmed(ApplicationUser user)
|
||||
{
|
||||
return user.EmailConfirmed || !user.RequiresEmailConfirmation;
|
||||
}
|
||||
|
||||
private static bool IsApproved(ApplicationUser user)
|
||||
{
|
||||
return user.Approved || !user.RequiresApproval;
|
||||
}
|
||||
|
||||
private static bool IsDisabled(ApplicationUser user)
|
||||
{
|
||||
return user.LockoutEnabled && user.LockoutEnd is not null &&
|
||||
DateTimeOffset.UtcNow < user.LockoutEnd.Value.UtcDateTime;
|
||||
}
|
||||
|
||||
public static bool TryCanLogin([NotNullWhen(true)] ApplicationUser? user, [MaybeNullWhen(true)] out string error)
|
||||
{
|
||||
error = null;
|
||||
if (user == null)
|
||||
{
|
||||
error = "Invalid login attempt.";
|
||||
return false;
|
||||
}
|
||||
if (!IsEmailConfirmed(user))
|
||||
{
|
||||
error = "You must have a confirmed email to log in.";
|
||||
return false;
|
||||
}
|
||||
if (!IsApproved(user))
|
||||
{
|
||||
error = "Your user account requires approval by an admin before you can log in.";
|
||||
return false;
|
||||
}
|
||||
if (IsDisabled(user))
|
||||
{
|
||||
error = "Your user account is currently disabled.";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> SetUserApproval(string userId, bool approved, Uri requestUri)
|
||||
{
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
var user = await userManager.FindByIdAsync(userId);
|
||||
if (user is null || !user.RequiresApproval || user.Approved == approved)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
user.Approved = approved;
|
||||
var succeeded = await userManager.UpdateAsync(user) is { Succeeded: true };
|
||||
if (succeeded)
|
||||
{
|
||||
_logger.LogInformation("User {UserId} is now {Status}", user.Id, approved ? "approved" : "unapproved");
|
||||
_eventAggregator.Publish(new UserApprovedEvent { User = user, Approved = approved, RequestUri = requestUri });
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogError("Failed to {Action} user {UserId}", approved ? "approve" : "unapprove", user.Id);
|
||||
}
|
||||
|
||||
return succeeded;
|
||||
}
|
||||
|
||||
public async Task<bool?> ToggleUser(string userId, DateTimeOffset? lockedOutDeadline)
|
||||
{
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
@@ -163,7 +232,6 @@ namespace BTCPayServer.Services
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> IsUserTheOnlyOneAdmin(ApplicationUser user)
|
||||
{
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
@@ -175,7 +243,7 @@ namespace BTCPayServer.Services
|
||||
}
|
||||
var adminUsers = await userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||
var enabledAdminUsers = adminUsers
|
||||
.Where(applicationUser => !IsDisabled(applicationUser))
|
||||
.Where(applicationUser => !IsDisabled(applicationUser) && IsApproved(applicationUser))
|
||||
.Select(applicationUser => applicationUser.Id).ToList();
|
||||
|
||||
return enabledAdminUsers.Count == 1 && enabledAdminUsers.Contains(user.Id);
|
||||
|
||||
Reference in New Issue
Block a user