From c373f1e08f8f5968df78c00634e1fe14e21e3fad Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 4 Apr 2025 16:41:28 +0900 Subject: [PATCH] Refactor UserService.IsDisabled to a property --- BTCPayServer.Data/Data/ApplicationUser.cs | 6 ++++++ .../GreenField/GreenfieldUsersController.cs | 1 - BTCPayServer/Controllers/UIServerController.Users.cs | 3 +-- BTCPayServer/Services/UserService.cs | 12 +++--------- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/BTCPayServer.Data/Data/ApplicationUser.cs b/BTCPayServer.Data/Data/ApplicationUser.cs index f6d2260c6..f2318c444 100644 --- a/BTCPayServer.Data/Data/ApplicationUser.cs +++ b/BTCPayServer.Data/Data/ApplicationUser.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -29,6 +30,11 @@ namespace BTCPayServer.Data public List> UserRoles { get; set; } + [NotMapped] + public bool IsDisabled => + this is { LockoutEnabled: true, LockoutEnd: { } lockoutEnd } && + DateTimeOffset.UtcNow < lockoutEnd.UtcDateTime; + public static void OnModelCreating(ModelBuilder builder, DatabaseFacade databaseFacade) { builder.Entity() diff --git a/BTCPayServer/Controllers/GreenField/GreenfieldUsersController.cs b/BTCPayServer/Controllers/GreenField/GreenfieldUsersController.cs index ff4169494..04e4d013e 100644 --- a/BTCPayServer/Controllers/GreenField/GreenfieldUsersController.cs +++ b/BTCPayServer/Controllers/GreenField/GreenfieldUsersController.cs @@ -459,7 +459,6 @@ namespace BTCPayServer.Controllers.Greenfield private async Task ForAPI(ApplicationUser data) { var roles = (await _userManager.GetRolesAsync(data)).ToArray(); - var blob = data.GetBlob(); return await UserService.ForAPI(data, roles, _callbackGenerator, _uriResolver, Request); } } diff --git a/BTCPayServer/Controllers/UIServerController.Users.cs b/BTCPayServer/Controllers/UIServerController.Users.cs index 922368600..c30289fca 100644 --- a/BTCPayServer/Controllers/UIServerController.Users.cs +++ b/BTCPayServer/Controllers/UIServerController.Users.cs @@ -75,8 +75,7 @@ namespace BTCPayServer.Controllers Approved = u.RequiresApproval ? u.Approved : null, Created = u.Created, Roles = u.UserRoles.Select(role => role.RoleId), - Disabled = u.LockoutEnabled && u.LockoutEnd != null && - DateTimeOffset.UtcNow < u.LockoutEnd.Value.UtcDateTime, + Disabled = u.IsDisabled, Stores = u.UserStores.OrderBy(s => !s.StoreData.Archived).ToList() }; }) diff --git a/BTCPayServer/Services/UserService.cs b/BTCPayServer/Services/UserService.cs index 0dd45df2a..8676148eb 100644 --- a/BTCPayServer/Services/UserService.cs +++ b/BTCPayServer/Services/UserService.cs @@ -76,7 +76,7 @@ namespace BTCPayServer.Services Created = data.Created, Name = blob.Name, Roles = roles, - Disabled = IsDisabled(data), + Disabled = data.IsDisabled, ImageUrl = string.IsNullOrEmpty(blob.ImageUrl) ? null : await uriResolver.Resolve(request.GetAbsoluteRootUri(), UnresolvedUri.Create(blob.ImageUrl)), @@ -94,12 +94,6 @@ namespace BTCPayServer.Services { return user.Approved || !user.RequiresApproval; } - - private static bool IsDisabled(ApplicationUser user) - { - return user is { LockoutEnabled: true, LockoutEnd: {} lockoutEnd } && - DateTimeOffset.UtcNow < lockoutEnd.UtcDateTime; - } public static bool TryCanLogin([NotNullWhen(true)] ApplicationUser? user, [MaybeNullWhen(true)] out string error) { @@ -119,7 +113,7 @@ namespace BTCPayServer.Services error = "Your user account requires approval by an admin before you can log in."; return false; } - if (IsDisabled(user)) + if (user.IsDisabled) { error = "Your user account is currently disabled."; return false; @@ -253,7 +247,7 @@ namespace BTCPayServer.Services } var adminUsers = await userManager.GetUsersInRoleAsync(Roles.ServerAdmin); var enabledAdminUsers = adminUsers - .Where(applicationUser => !IsDisabled(applicationUser) && IsApproved(applicationUser)) + .Where(applicationUser => !applicationUser.IsDisabled && IsApproved(applicationUser)) .Select(applicationUser => applicationUser.Id).ToList(); return enabledAdminUsers.Count == 1 && enabledAdminUsers.Contains(user.Id);