222 lines
8.2 KiB
Plaintext
222 lines
8.2 KiB
Plaintext
@page "/administration/users"
|
|
@rendermode InteractiveServer
|
|
@layout AdminLayout
|
|
|
|
@using System.Globalization
|
|
@using BlazorStrap
|
|
@using CurrieTechnologies.Razor.SweetAlert2
|
|
@using HopFrame.Database.Models
|
|
@using HopFrame.Security.Claims
|
|
@using HopFrame.Web.Pages.Administration.Layout
|
|
@using static Microsoft.AspNetCore.Components.Web.RenderMode
|
|
@using Microsoft.AspNetCore.Components.Web
|
|
@using HopFrame.Web.Components
|
|
@using BlazorStrap.V5
|
|
@using HopFrame.Database.Repositories
|
|
@using HopFrame.Web.Components.Administration
|
|
|
|
<PageTitle>Users</PageTitle>
|
|
<AuthorizedView Permission="@Security.AdminPermissions.ViewUsers" RedirectIfUnauthorized="administration/login?redirect=/administration/users"/>
|
|
|
|
<UserAddModal @ref="_userAddModal" ReloadPage="Reload"/>
|
|
<UserEditModal @ref="_userEditModal" ReloadPage="Reload"/>
|
|
|
|
<div class="title">
|
|
<h3>
|
|
Users administration
|
|
<span class="reload" @onclick="Reload">
|
|
<HopIconDisplay Type="HopIconDisplay.HopIcon.Reload"/>
|
|
</span>
|
|
</h3>
|
|
|
|
<form class="d-flex" role="search" @onsubmit="Search" id="search">
|
|
<input class="form-control me-2 input-dark" type="search" placeholder="Search" aria-label="Search" @bind="_searchText">
|
|
<BSButton Color="BSColor.Success" IsOutlined="true" type="submit">Search</BSButton>
|
|
</form>
|
|
<AuthorizedView Permission="@Security.AdminPermissions.AddUser">
|
|
<BSButton IsSubmit="false" Color="BSColor.Success" Target="add-user" OnClick="() => _userAddModal.ShowAsync()">Add User</BSButton>
|
|
</AuthorizedView>
|
|
</div>
|
|
|
|
<BSTable IsStriped="true" IsHoverable="true" IsDark="true" Color="BSColor.Dark">
|
|
<BSTHead>
|
|
<BSTR>
|
|
<BSTD>#</BSTD>
|
|
<BSTD>
|
|
<span class="sorter" @onclick="() => OrderBy(OrderType.Email)">E-Mail</span>
|
|
@if (_currentOrder == OrderType.Email) {
|
|
<HopIconDisplay Type="_currentOrderDirection == OrderDirection.Desc ? HopIconDisplay.HopIcon.ArrowDown : HopIconDisplay.HopIcon.ArrowUp"/>
|
|
}
|
|
</BSTD>
|
|
<BSTD>
|
|
<span class="sorter" @onclick="() => OrderBy(OrderType.Username)">Username</span>
|
|
@if (_currentOrder == OrderType.Username) {
|
|
<HopIconDisplay Type="_currentOrderDirection == OrderDirection.Desc ? HopIconDisplay.HopIcon.ArrowDown : HopIconDisplay.HopIcon.ArrowUp"/>
|
|
}
|
|
</BSTD>
|
|
<BSTD>
|
|
<span class="sorter" @onclick="() => OrderBy(OrderType.Registered)">Registered</span>
|
|
@if (_currentOrder == OrderType.Registered) {
|
|
<HopIconDisplay Type="_currentOrderDirection == OrderDirection.Desc ? HopIconDisplay.HopIcon.ArrowDown : HopIconDisplay.HopIcon.ArrowUp"/>
|
|
}
|
|
</BSTD>
|
|
<BSTD>Primary Group</BSTD>
|
|
|
|
@if (_hasEditPrivileges || _hasDeletePrivileges) {
|
|
<BSTD>Actions</BSTD>
|
|
}
|
|
</BSTR>
|
|
</BSTHead>
|
|
|
|
<BSTBody>
|
|
@foreach (var user in _users) {
|
|
<BSTR>
|
|
<BSTD class="bold">@user.Id</BSTD>
|
|
<BSTD>@user.Email</BSTD>
|
|
<BSTD>@user.Username</BSTD>
|
|
<BSTD>@user.CreatedAt</BSTD>
|
|
<BSTD>@GetFriendlyGroupName(user)</BSTD>
|
|
|
|
@if (_hasEditPrivileges || _hasDeletePrivileges) {
|
|
<BSTD>
|
|
<BSButtonGroup>
|
|
@if (_hasEditPrivileges) {
|
|
<BSButton Color="BSColor.Warning" OnClick="() => _userEditModal.ShowAsync(user)">Edit</BSButton>
|
|
}
|
|
|
|
@if (_hasDeletePrivileges) {
|
|
<BSButton Color="BSColor.Danger" OnClick="() => Delete(user)">Delete</BSButton>
|
|
}
|
|
</BSButtonGroup>
|
|
</BSTD>
|
|
}
|
|
</BSTR>
|
|
}
|
|
</BSTBody>
|
|
</BSTable>
|
|
|
|
@inject IUserRepository UserService
|
|
@inject IPermissionRepository PermissionsService
|
|
@inject IGroupRepository Groups
|
|
@inject SweetAlertService Alerts
|
|
@inject ITokenContext Auth
|
|
|
|
@code {
|
|
private IList<User> _users = new List<User>();
|
|
private IDictionary<Guid, PermissionGroup> _userGroups = new Dictionary<Guid, PermissionGroup>();
|
|
|
|
private OrderType _currentOrder = OrderType.None;
|
|
private OrderDirection _currentOrderDirection = OrderDirection.Asc;
|
|
|
|
private string _searchText;
|
|
|
|
private bool _hasEditPrivileges = false;
|
|
private bool _hasDeletePrivileges = false;
|
|
|
|
private UserAddModal _userAddModal;
|
|
private UserEditModal _userEditModal;
|
|
|
|
protected override async Task OnInitializedAsync() {
|
|
_users = await UserService.GetUsers();
|
|
|
|
foreach (var user in _users) {
|
|
var groups = await Groups.GetUserGroups(user);
|
|
_userGroups.Add(user.Id, groups.LastOrDefault());
|
|
}
|
|
|
|
_hasEditPrivileges = await PermissionsService.HasPermission(Auth.User, Security.AdminPermissions.EditUser);
|
|
_hasDeletePrivileges = await PermissionsService.HasPermission(Auth.User, Security.AdminPermissions.DeleteUser);
|
|
}
|
|
|
|
private async Task Reload() {
|
|
_users = new List<User>();
|
|
_userGroups = new Dictionary<Guid, PermissionGroup>();
|
|
|
|
_users = await UserService.GetUsers();
|
|
|
|
foreach (var user in _users) {
|
|
var groups = await Groups.GetUserGroups(user);
|
|
_userGroups.Add(user.Id, groups.LastOrDefault());
|
|
}
|
|
|
|
OrderBy(_currentOrder, false);
|
|
StateHasChanged();
|
|
}
|
|
|
|
private async Task Search() {
|
|
var users = await UserService.GetUsers();
|
|
|
|
if (!string.IsNullOrWhiteSpace(_searchText)) {
|
|
users = users
|
|
.Where(user =>
|
|
user.Email.Contains(_searchText) ||
|
|
user.Username.Contains(_searchText) ||
|
|
user.Id.ToString().Contains(_searchText) ||
|
|
user.CreatedAt.ToString(CultureInfo.InvariantCulture).Contains(_searchText) ||
|
|
_userGroups[user.Id]?.Name.Contains(_searchText) == true)
|
|
.ToList();
|
|
}
|
|
|
|
_users = users;
|
|
OrderBy(_currentOrder, false);
|
|
}
|
|
|
|
private string GetFriendlyGroupName(User user) {
|
|
var group = _userGroups[user.Id];
|
|
if (group is null) return null;
|
|
|
|
return group.Name.Replace("group.", "");
|
|
}
|
|
|
|
private void OrderBy(OrderType type, bool changeDir = true) {
|
|
if (_currentOrder == type && changeDir) _currentOrderDirection = (OrderDirection)(((byte)_currentOrderDirection + 1) % 2);
|
|
if (_currentOrder != type) _currentOrderDirection = OrderDirection.Asc;
|
|
|
|
if (type == OrderType.Email) {
|
|
_users = _currentOrderDirection == OrderDirection.Asc ? _users.OrderBy(user => user.Email).ToList() : _users.OrderByDescending(user => user.Email).ToList();
|
|
}
|
|
else if (type == OrderType.Username) {
|
|
_users = _currentOrderDirection == OrderDirection.Asc ? _users.OrderBy(user => user.Username).ToList() : _users.OrderByDescending(user => user.Username).ToList();
|
|
}
|
|
else if (type == OrderType.Registered) {
|
|
_users = _currentOrderDirection == OrderDirection.Asc ? _users.OrderBy(user => user.CreatedAt).ToList() : _users.OrderByDescending(user => user.CreatedAt).ToList();
|
|
}
|
|
|
|
_currentOrder = type;
|
|
}
|
|
|
|
private async Task Delete(User user) {
|
|
var result = await Alerts.FireAsync(new SweetAlertOptions {
|
|
Title = "Are you sure?",
|
|
Text = "You won't be able to revert this!",
|
|
Icon = SweetAlertIcon.Warning,
|
|
ConfirmButtonText = "Yes",
|
|
ShowCancelButton = true,
|
|
ShowConfirmButton = true
|
|
});
|
|
|
|
if (result.IsConfirmed) {
|
|
await UserService.DeleteUser(user);
|
|
await Reload();
|
|
|
|
await Alerts.FireAsync(new SweetAlertOptions {
|
|
Title = "Deleted!",
|
|
Icon = SweetAlertIcon.Success,
|
|
Timer = 1500,
|
|
ShowConfirmButton = false
|
|
});
|
|
}
|
|
}
|
|
|
|
private enum OrderType {
|
|
None,
|
|
Email,
|
|
Username,
|
|
Registered
|
|
}
|
|
|
|
private enum OrderDirection : byte {
|
|
Asc = 0,
|
|
Desc = 1
|
|
}
|
|
} |