Optimize admin user list

This commit is contained in:
Kukks 2021-11-15 10:27:19 +01:00
parent 56c8e43ce8
commit 31cf2671f5
5 changed files with 21 additions and 10 deletions

View File

@ -75,6 +75,7 @@ namespace BTCPayServer.Data
// some of the data models don't have OnModelCreating for now, commenting them
ApplicationUser.OnModelCreating(builder);
AddressInvoiceData.OnModelCreating(builder);
APIKeyData.OnModelCreating(builder);
AppData.OnModelCreating(builder);

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace BTCPayServer.Data
{
@ -18,5 +19,14 @@ namespace BTCPayServer.Data
public List<NotificationData> Notifications { get; set; }
public List<UserStore> UserStores { get; set; }
public List<Fido2Credential> Fido2Credentials { get; set; }
public List<IdentityUserRole<string>> UserRoles { get; set; }
public static void OnModelCreating(ModelBuilder builder)
{
builder.Entity<ApplicationUser>()
.HasMany<IdentityUserRole<string>>(user => user.UserRoles)
.WithOne().HasForeignKey(role => role.UserId);
}
}
}

View File

@ -20,7 +20,8 @@ namespace BTCPayServer.Controllers
{
[Route("server/users")]
public async Task<IActionResult> ListUsers(
UsersViewModel model,
[FromServices] RoleManager<IdentityRole> roleManager,
UsersViewModel model,
string sortOrder = null
)
{
@ -50,7 +51,9 @@ namespace BTCPayServer.Controllers
}
}
model.Roles = roleManager.Roles.ToDictionary(role => role.Id, role => role.Name);
model.Users = await usersQuery
.Include(user => user.UserRoles)
.Skip(model.Skip)
.Take(model.Count)
.Select(u => new UsersViewModel.UserViewModel
@ -59,16 +62,11 @@ namespace BTCPayServer.Controllers
Email = u.Email,
Id = u.Id,
Verified = u.EmailConfirmed || !u.RequiresEmailConfirmation,
Created = u.Created
Created = u.Created,
Roles = u.UserRoles.Select(role => role.RoleId)
})
.ToListAsync();
model.Total = await usersQuery.CountAsync();
foreach (UsersViewModel.UserViewModel uvm in model.Users)
{
var userId = await _UserManager.FindByIdAsync(uvm.Id);
uvm.IsAdmin = await _userService.IsAdminUser(userId);;
}
return View(model);
}

View File

@ -13,8 +13,10 @@ namespace BTCPayServer.Models.ServerViewModels
public bool Verified { get; set; }
public bool IsAdmin { get; set; }
public DateTimeOffset? Created { get; set; }
public IEnumerable<string> Roles { get; set; }
}
public List<UserViewModel> Users { get; set; } = new List<UserViewModel>();
public Dictionary<string, string> Roles { get; set; }
}
}

View File

@ -64,9 +64,9 @@
<tr>
<td class="d-flex align-items-center">
<span class="me-2">@user.Email</span>
@if (user.IsAdmin)
@foreach (var role in user.Roles)
{
<span class="badge bg-info">Admin</span>
<span class="badge bg-info">@Model.Roles[role]</span>
}
</td>
<td>@user.Created?.ToBrowserDate()</td>