2023-02-17 18:02:01 -06:00
|
|
|
@page "/Settings/Users"
|
2024-08-04 18:44:33 -05:00
|
|
|
@using LANCommander.Server.UI.Pages.Settings.Users.Components
|
2025-02-01 02:21:34 -06:00
|
|
|
@inject UserService UserService
|
2023-12-04 19:57:30 -06:00
|
|
|
@inject ModalService ModalService
|
2023-03-02 18:50:24 -06:00
|
|
|
@inject IMessageService MessageService
|
2023-09-17 17:26:48 -05:00
|
|
|
@inject NavigationManager NavigationManager
|
2024-10-16 02:06:02 -05:00
|
|
|
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
|
2023-02-17 18:02:01 -06:00
|
|
|
|
2023-03-03 17:34:27 -06:00
|
|
|
<PageHeader Title="Users" />
|
|
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
<DataTable
|
|
|
|
|
TItem="User"
|
|
|
|
|
@ref="Table"
|
|
|
|
|
Size="TableSize.Small"
|
|
|
|
|
@bind-Total="Total"
|
|
|
|
|
Responsive
|
|
|
|
|
ColumnPicker
|
|
|
|
|
Searchable
|
|
|
|
|
SearchProperty="u => u.UserName">
|
|
|
|
|
<Columns>
|
|
|
|
|
<BoundDataColumn Title="Username" Property="u => u.UserName" Sortable DefaultSortOrder="SortDirection.Ascending" />
|
|
|
|
|
|
|
|
|
|
<DataColumn TData="string" Title="Roles" Include="UserRoles,UserRoles.Role">
|
|
|
|
|
@foreach (var role in context.Roles)
|
2025-02-04 19:32:33 -06:00
|
|
|
{
|
2025-03-09 01:43:12 -06:00
|
|
|
<Tag>@role.Name</Tag>
|
2025-02-04 19:32:33 -06:00
|
|
|
}
|
2025-03-09 01:43:12 -06:00
|
|
|
</DataColumn>
|
|
|
|
|
|
|
|
|
|
<DataColumn TData="long" Title="Saves" Include="GameSaves">
|
|
|
|
|
<ByteSize Value="@(context?.GameSaves?.Sum(s => s.Size) ?? 0)" />
|
|
|
|
|
</DataColumn>
|
|
|
|
|
|
2025-03-17 02:02:17 -05:00
|
|
|
<BoundDataColumn Title="Created On" Property="u => u.CreatedOn" Sortable>
|
|
|
|
|
<LocalTime Value="context.CreatedOn" />
|
|
|
|
|
</BoundDataColumn>
|
2025-03-09 01:43:12 -06:00
|
|
|
|
|
|
|
|
@if (Settings.Authentication.RequireApproval)
|
|
|
|
|
{
|
|
|
|
|
<DataColumn TData="bool" Title="Approved">
|
|
|
|
|
<Checkbox Checked="context.Approved" />
|
|
|
|
|
</DataColumn>
|
|
|
|
|
|
2025-03-17 02:02:17 -05:00
|
|
|
<BoundDataColumn Title="Approved On" Property="u => u.ApprovedOn" Sortable>
|
|
|
|
|
<LocalTime Value="context.ApprovedOn" />
|
|
|
|
|
</BoundDataColumn>
|
2025-03-09 01:43:12 -06:00
|
|
|
}
|
|
|
|
|
|
2025-05-23 02:17:06 -05:00
|
|
|
<DataActions TData="string">
|
2025-03-09 01:43:12 -06:00
|
|
|
<DropdownButton Type="ButtonType.Primary" OnClick="@(() => NavigationManager.NavigateTo($"/Settings/Users/{context.UserName}"))">
|
|
|
|
|
<Overlay>
|
|
|
|
|
<Menu>
|
|
|
|
|
@if (Settings.Authentication.RequireApproval && !context.Approved)
|
|
|
|
|
{
|
|
|
|
|
<MenuItem OnClick="() => ApproveUser(context)" Type="@ButtonType.Primary">Approve</MenuItem>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
<MenuItem OnClick="() => ManageRoles(context)">Manage Roles</MenuItem>
|
|
|
|
|
<MenuItem OnClick="() => ChangePassword(context)">Change Password</MenuItem>
|
|
|
|
|
|
|
|
|
|
@if (!context.Roles?.Any(r => r.Name == RoleService.AdministratorRoleName) ?? true)
|
|
|
|
|
{
|
|
|
|
|
<MenuItem OnClick="() => PromoteUser(context)">Promote</MenuItem>
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
<MenuItem OnClick="() => DemoteUser(context)">Demote</MenuItem>
|
|
|
|
|
}
|
|
|
|
|
</Menu>
|
|
|
|
|
</Overlay>
|
|
|
|
|
<ChildContent>
|
|
|
|
|
Edit
|
|
|
|
|
</ChildContent>
|
|
|
|
|
</DropdownButton>
|
|
|
|
|
|
|
|
|
|
<Popconfirm Title="Are you sure you want to delete this user?" OnConfirm="() => DeleteUser(context)">
|
|
|
|
|
<Button Type="ButtonType.Text" Icon="@IconType.Outline.Close" Danger />
|
|
|
|
|
</Popconfirm>
|
|
|
|
|
</DataActions>
|
|
|
|
|
</Columns>
|
|
|
|
|
|
|
|
|
|
<PaginationTemplate>
|
|
|
|
|
<Pagination
|
|
|
|
|
Total="context.Total"
|
|
|
|
|
PageSize="context.PageSize"
|
|
|
|
|
Current="context.PageIndex"
|
|
|
|
|
DefaultPageSize="25"
|
|
|
|
|
PageSizeOptions="new [] { 25, 50, 100, 200 }"
|
|
|
|
|
ShowSizeChanger
|
|
|
|
|
OnChange="context.HandlePageChange" />
|
|
|
|
|
</PaginationTemplate>
|
|
|
|
|
</DataTable>
|
2023-02-17 18:02:01 -06:00
|
|
|
|
|
|
|
|
@code {
|
2024-11-22 02:04:04 -06:00
|
|
|
ICollection<UserViewModel> UserList { get; set; } = new List<UserViewModel>();
|
2023-03-03 17:34:27 -06:00
|
|
|
|
2024-10-07 18:38:27 -05:00
|
|
|
Settings Settings = SettingService.GetSettings();
|
2023-03-03 17:34:27 -06:00
|
|
|
bool Loading = true;
|
2023-02-17 18:02:01 -06:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
DataTable<User> Table;
|
|
|
|
|
int Total;
|
2023-02-17 18:02:01 -06:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
private async Task ApproveUser(User user)
|
2023-08-11 13:30:29 -05:00
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
var dbUser = await UserService.GetAsync(user.Id);
|
2024-11-25 22:29:56 -06:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
if (dbUser != null)
|
|
|
|
|
{
|
|
|
|
|
dbUser.Approved = true;
|
|
|
|
|
dbUser.ApprovedOn = DateTime.UtcNow;
|
2023-08-11 13:30:29 -05:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
await UserService.UpdateAsync(dbUser);
|
2023-08-11 13:30:29 -05:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
user.Approved = true;
|
2023-08-11 13:30:29 -05:00
|
|
|
|
2025-05-23 02:17:45 -05:00
|
|
|
await MessageService.SuccessAsync($"Approved {user.UserName}!");
|
2023-08-11 13:30:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
private async Task PromoteUser(User user)
|
2023-02-17 18:02:01 -06:00
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
var dbUser = await UserService.GetAsync(user.Id);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
await UserService.AddToRoleAsync(dbUser.UserName, RoleService.AdministratorRoleName);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2025-05-23 02:17:45 -05:00
|
|
|
await MessageService.SuccessAsync($"Promoted {user.UserName}!");
|
2023-02-17 18:02:01 -06:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
Table.Reload();
|
2023-02-17 18:02:01 -06:00
|
|
|
}
|
|
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
private async Task DemoteUser(User user)
|
2023-02-17 18:02:01 -06:00
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
var dbUser = await UserService.GetAsync(user.Id);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
if (UserList.SelectMany(u => u.Roles).Count(r => r == RoleService.AdministratorRoleName) == 1)
|
|
|
|
|
{
|
|
|
|
|
MessageService.Error("Cannot demote the only administrator!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await UserService.RemoveFromRole(dbUser.UserName, RoleService.AdministratorRoleName);
|
2024-11-25 22:29:56 -06:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
Table.Reload();
|
2023-02-17 18:02:01 -06:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-08 21:14:09 -05:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
private async Task DeleteUser(User user)
|
2023-08-08 21:14:09 -05:00
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
var dbUser = await UserService.GetAsync(user.Id);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
if (UserList.SelectMany(u => u.Roles).Count(r => r == RoleService.AdministratorRoleName) == 1 && user.Roles.Any(r => r.Name == RoleService.AdministratorRoleName))
|
2025-02-01 02:21:34 -06:00
|
|
|
{
|
|
|
|
|
MessageService.Error("Cannot delete the only administrator!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await UserService.DeleteAsync(dbUser);
|
2024-11-25 22:29:56 -06:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
Table.Reload();
|
2023-08-08 21:14:09 -05:00
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
MessageService.Success($"Deleted {user.UserName}!");
|
2023-08-08 21:14:09 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-17 17:26:48 -05:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
private void ChangePassword(User user)
|
2023-09-17 17:26:48 -05:00
|
|
|
{
|
|
|
|
|
NavigationManager.NavigateTo($"/Settings/Users/{user.Id}/ChangePassword", true);
|
|
|
|
|
}
|
2023-12-04 19:57:30 -06:00
|
|
|
|
2025-03-09 01:43:12 -06:00
|
|
|
private async Task ManageRoles(User user)
|
2023-12-04 19:57:30 -06:00
|
|
|
{
|
|
|
|
|
var modalOptions = new ModalOptions()
|
|
|
|
|
{
|
2025-03-09 01:43:12 -06:00
|
|
|
Title = "Manage Roles",
|
2023-12-04 19:57:30 -06:00
|
|
|
Maximizable = false,
|
|
|
|
|
DefaultMaximized = false,
|
|
|
|
|
Closable = true,
|
|
|
|
|
Draggable = true,
|
|
|
|
|
OkText = "Update Roles"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var modalRef = await ModalService.CreateModalAsync<ManageRolesDialog, Guid, Guid>(modalOptions, user.Id);
|
|
|
|
|
|
|
|
|
|
modalRef.OnOk = async (Guid) =>
|
|
|
|
|
{
|
2025-03-09 01:43:12 -06:00
|
|
|
Table.Reload();
|
2023-12-04 19:57:30 -06:00
|
|
|
};
|
|
|
|
|
}
|
2023-02-17 18:02:01 -06:00
|
|
|
}
|