mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
Refactor UserService.IsDisabled to a property
This commit is contained in:
@@ -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<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)
|
||||
{
|
||||
builder.Entity<ApplicationUser>()
|
||||
|
||||
@@ -459,7 +459,6 @@ namespace BTCPayServer.Controllers.Greenfield
|
||||
private async Task<ApplicationUserData> ForAPI(ApplicationUser data)
|
||||
{
|
||||
var roles = (await _userManager.GetRolesAsync(data)).ToArray();
|
||||
var blob = data.GetBlob();
|
||||
return await UserService.ForAPI<ApplicationUserData>(data, roles, _callbackGenerator, _uriResolver, Request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
};
|
||||
})
|
||||
|
||||
@@ -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)),
|
||||
@@ -95,12 +95,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)
|
||||
{
|
||||
error = null;
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user