Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Data/Repositories/ApplicationUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,35 @@ public async Task<List<ApplicationUser>> GetAll(bool includeBanned = false)
return result;
}

public async Task<(List<ApplicationUser> users, int totalCount)> GetPaginatedAsync(
int pageNumber,
int pageSize,
bool includeBanned = false)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

var query = applicationDbContext.ApplicationUsers
.Include(user => user.Keys)
.Include(user => user.Nodes)
.AsQueryable();

if (!includeBanned)
{
query = query.Where(x => x.LockoutEnd <= DateTimeOffset.UtcNow || x.LockoutEnd == null);
}

query = query.OrderBy(x => x.UserName);

var totalCount = await query.CountAsync();

var users = await query
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToListAsync();

return (users, totalCount);
}

public async Task<(bool, string?)> AddAsync(ApplicationUser type, string? password = null)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public interface IApplicationUserRepository

Task<List<ApplicationUser>> GetAll(bool includeBanned = false);

Task<(List<ApplicationUser> users, int totalCount)> GetPaginatedAsync(
int pageNumber,
int pageSize,
bool includeBanned = false);

Task<(bool, string?)> AddAsync(ApplicationUser type, string? password = null);

Task<(bool, string?)> AddRangeAsync(List<ApplicationUser> type);
Expand Down
73 changes: 61 additions & 12 deletions src/Pages/Users.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@using Humanizer
@using Microsoft.AspNetCore.Identity
@using System.Security.Claims
@using System.Threading
@using Blazorise.Extensions
@using Blazorise
@using Blazorise.DataGrid
Expand All @@ -11,7 +12,10 @@
<Row>
<Column ColumnSize="ColumnSize.Is12">
<DataGrid TItem="ApplicationUser"
@ref="_usersDataGrid"
Data="@_users"
ReadData="@OnReadData"
TotalItems="@_totalItems"
Editable="true"
EditMode="DataGridEditMode.Popup"
Responsive="true"
Expand All @@ -24,7 +28,7 @@
ShowPager="true"
ShowPageSizes="true"
PageSize="25"
Filterable="true"
Filterable="false"
ShowValidationFeedback="true"
ShowValidationsSummary="false"
UseValidation="true">
Expand Down Expand Up @@ -56,7 +60,7 @@
</DataGridCommandColumn>
<DataGridColumn TItem="ApplicationUser" Editable="true" Field="@nameof(ApplicationUser.UserName)" Caption="Username" Sortable="false" Displayable="@IsColumnVisible(UsersColumnName.Username)" Filterable="false">
<EditTemplate>
<Validation Validator="@((ValidatorEventArgs obj) => @ValidationHelper.ValidateUsername(obj, _users, context.Item.Id))">
<Validation AsyncValidator="(args, token) => ValidateUsernameAsync(args, token, context.Item.Id)">
<TextEdit Text="@((string)context.CellValue)" TextChanged="(text) => { context.CellValue = text; }">
<Feedback>
<ValidationError/>
Expand Down Expand Up @@ -121,6 +125,8 @@
@attribute [Authorize(Roles = "Superadmin")]
@code {
private List<ApplicationUser> _users = new();
private DataGrid<ApplicationUser>? _usersDataGrid;
private int _totalItems;

[CascadingParameter]
private ApplicationUser? LoggedUser { get; set; }
Expand Down Expand Up @@ -151,17 +157,26 @@
{
if (LoggedUser != null)
{
await GetData();
await LoadNodesList();
}
}

private async Task GetData()
private async Task LoadNodesList()
{
_nodesList = await NodeRepository.GetAllManagedByNodeGuard();
}

_users = await ApplicationUserRepository.GetAll(true);
private async Task OnReadData(DataGridReadDataEventArgs<ApplicationUser> e)
{
if (LoggedUser == null) return;

_nodesList = await NodeRepository.GetAllManagedByNodeGuard();
var (users, totalCount) = await ApplicationUserRepository.GetPaginatedAsync(
e.Page,
e.PageSize,
includeBanned: true);

_users = users;
_totalItems = totalCount;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
Expand Down Expand Up @@ -218,7 +233,10 @@
AuditObjectType.User,
arg.Item.Id,
new { Username = arg.Item.UserName, Roles = string.Join(", ", _selectedRoles) });
await GetData();
if (_usersDataGrid != null)
{
await _usersDataGrid.Reload();
}
}
else
{
Expand Down Expand Up @@ -258,7 +276,10 @@
else
{
ToastService.ShowSuccess("Success");
await GetData();
if (_usersDataGrid != null)
{
await _usersDataGrid.Reload();
}
}
}
}
Expand Down Expand Up @@ -306,7 +327,10 @@
new { Username = arg.Item.UserName });
}

await GetData();
if (_usersDataGrid != null)
{
await _usersDataGrid.Reload();
}
}

private void NewItemDefaultSetter(ApplicationUser obj)
Expand Down Expand Up @@ -390,7 +414,10 @@
new { Username = contextItem.UserName });
}

await GetData();
if (_usersDataGrid != null)
{
await _usersDataGrid.Reload();
}
}
}

Expand Down Expand Up @@ -422,8 +449,30 @@
new { Username = contextItem.UserName });
}

await GetData();
} }
if (_usersDataGrid != null)
{
await _usersDataGrid.Reload();
}
}
}

private async Task ValidateUsernameAsync(ValidatorEventArgs obj, CancellationToken cancellationToken, string currentUserId)
{
obj.Status = ValidationStatus.Success;
if (string.IsNullOrWhiteSpace((string)obj.Value))
{
obj.ErrorText = "The Username cannot be empty";
obj.Status = ValidationStatus.Error;
return;
}

var existingUser = await ApplicationUserRepository.GetByUsername(obj.Value.ToString());
if (existingUser != null && existingUser.Id != currentUserId)
{
obj.ErrorText = "A user with the same username already exists";
obj.Status = ValidationStatus.Error;
}
}

private void ValidateRole(ValidatorEventArgs obj)
{
Expand Down
Loading