Refactor UserService.IsDisabled to a property

This commit is contained in:
nicolas.dorier
2025-04-04 16:41:28 +09:00
parent ead74a985f
commit c373f1e08f
4 changed files with 10 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
@@ -29,6 +30,11 @@ namespace BTCPayServer.Data
public List<IdentityUserRole<string>> UserRoles { get; set; } public List<IdentityUserRole<string>> 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) public static void OnModelCreating(ModelBuilder builder, DatabaseFacade databaseFacade)
{ {
builder.Entity<ApplicationUser>() builder.Entity<ApplicationUser>()

View File

@@ -459,7 +459,6 @@ namespace BTCPayServer.Controllers.Greenfield
private async Task<ApplicationUserData> ForAPI(ApplicationUser data) private async Task<ApplicationUserData> ForAPI(ApplicationUser data)
{ {
var roles = (await _userManager.GetRolesAsync(data)).ToArray(); var roles = (await _userManager.GetRolesAsync(data)).ToArray();
var blob = data.GetBlob();
return await UserService.ForAPI<ApplicationUserData>(data, roles, _callbackGenerator, _uriResolver, Request); return await UserService.ForAPI<ApplicationUserData>(data, roles, _callbackGenerator, _uriResolver, Request);
} }
} }

View File

@@ -75,8 +75,7 @@ namespace BTCPayServer.Controllers
Approved = u.RequiresApproval ? u.Approved : null, Approved = u.RequiresApproval ? u.Approved : null,
Created = u.Created, Created = u.Created,
Roles = u.UserRoles.Select(role => role.RoleId), Roles = u.UserRoles.Select(role => role.RoleId),
Disabled = u.LockoutEnabled && u.LockoutEnd != null && Disabled = u.IsDisabled,
DateTimeOffset.UtcNow < u.LockoutEnd.Value.UtcDateTime,
Stores = u.UserStores.OrderBy(s => !s.StoreData.Archived).ToList() Stores = u.UserStores.OrderBy(s => !s.StoreData.Archived).ToList()
}; };
}) })

View File

@@ -76,7 +76,7 @@ namespace BTCPayServer.Services
Created = data.Created, Created = data.Created,
Name = blob.Name, Name = blob.Name,
Roles = roles, Roles = roles,
Disabled = IsDisabled(data), Disabled = data.IsDisabled,
ImageUrl = string.IsNullOrEmpty(blob.ImageUrl) ImageUrl = string.IsNullOrEmpty(blob.ImageUrl)
? null ? null
: await uriResolver.Resolve(request.GetAbsoluteRootUri(), UnresolvedUri.Create(blob.ImageUrl)), : await uriResolver.Resolve(request.GetAbsoluteRootUri(), UnresolvedUri.Create(blob.ImageUrl)),
@@ -94,12 +94,6 @@ namespace BTCPayServer.Services
{ {
return user.Approved || !user.RequiresApproval; 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) 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."; error = "Your user account requires approval by an admin before you can log in.";
return false; return false;
} }
if (IsDisabled(user)) if (user.IsDisabled)
{ {
error = "Your user account is currently disabled."; error = "Your user account is currently disabled.";
return false; return false;
@@ -253,7 +247,7 @@ namespace BTCPayServer.Services
} }
var adminUsers = await userManager.GetUsersInRoleAsync(Roles.ServerAdmin); var adminUsers = await userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
var enabledAdminUsers = adminUsers var enabledAdminUsers = adminUsers
.Where(applicationUser => !IsDisabled(applicationUser) && IsApproved(applicationUser)) .Where(applicationUser => !applicationUser.IsDisabled && IsApproved(applicationUser))
.Select(applicationUser => applicationUser.Id).ToList(); .Select(applicationUser => applicationUser.Id).ToList();
return enabledAdminUsers.Count == 1 && enabledAdminUsers.Contains(user.Id); return enabledAdminUsers.Count == 1 && enabledAdminUsers.Contains(user.Id);