296 lines
12 KiB
Plaintext
296 lines
12 KiB
Plaintext
@page "/Admin/PK/UsersManager"
|
|
|
|
@using System.Security.Claims
|
|
@using Microsoft.AspNetCore.Authorization
|
|
@using OrdersManagementDataModel.Dtos
|
|
@using Syncfusion.Blazor.Grids
|
|
@using Action = Syncfusion.Blazor.Grids.Action
|
|
@using UserService = OrdersManagement.Services.UserService
|
|
@using Syncfusion.Blazor.Cards
|
|
@using Syncfusion.Blazor.Popups
|
|
@using Syncfusion.Blazor.Buttons
|
|
@inject UserService UserService
|
|
@inject RoleService RoleService
|
|
@inject FunctionService FunctionService
|
|
@inject NavigationManager NavigationManager
|
|
@inject CustomAuthenticationStateProvider CustomAuthenticationStateProvider
|
|
|
|
<div class="h-100 d-flex justify-content-center align-items-start">
|
|
<SfCard CssClass="shadow" style="width: 100%; max-width: 1200px;">
|
|
<CardHeader>
|
|
<h3 class="text-primary">Zarządzanie Użytkownikami i Rolami</h3>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<h5 class="text-primary mb-3">Użytkownicy</h5>
|
|
<SfGrid DataSource="@UserList"
|
|
AllowPaging="true"
|
|
ShowColumnMenu="true"
|
|
Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })">
|
|
<GridColumns>
|
|
<GridColumn Field="@nameof(UserDto.RowPointer)" AllowEditing="false" IsPrimaryKey="true" HeaderText="ID" Width="70"></GridColumn>
|
|
<GridColumn Field="@nameof(UserDto.Login)" HeaderText="Login" Width="100"></GridColumn>
|
|
<GridColumn Field="@nameof(UserDto.Email)" HeaderText="Email" Width="150"></GridColumn>
|
|
<GridColumn Field="@nameof(UserDto.FirstName)" HeaderText="Imię" Width="100"></GridColumn>
|
|
<GridColumn Field="@nameof(UserDto.LastName)" HeaderText="Nazwisko" Width="100"></GridColumn>
|
|
<GridColumn Field="@nameof(UserDto.IsActive)" HeaderText="Aktywny" Width="80"></GridColumn>
|
|
<GridColumn Field="@nameof(UserDto.CreatedDate)" AllowEditing="false" HeaderText="Utworzono" Format="d" Width="120"></GridColumn>
|
|
<GridColumn HeaderText="" Width="100">
|
|
<Template>
|
|
@{
|
|
var user = (context as UserDto);
|
|
<SfButton CssClass="e-small e-primary" @onclick="() => ResetPassword(user)">Zresetuj haslo</SfButton>
|
|
}
|
|
</Template>
|
|
</GridColumn>
|
|
</GridColumns>
|
|
<GridEditSettings AllowDeleting="true"
|
|
ShowDeleteConfirmDialog="true"
|
|
AllowAdding="true"
|
|
NewRowPosition="NewRowPosition.Bottom"
|
|
AllowEditing="true">
|
|
</GridEditSettings>
|
|
<GridEvents OnActionBegin="UserActionBegin"
|
|
OnActionComplete="UserActionComplete"
|
|
TValue="UserDto">
|
|
</GridEvents>
|
|
<GridPageSettings PageSize="10"></GridPageSettings>
|
|
</SfGrid>
|
|
|
|
<h5 class="text-primary mb-3 mt-4">Role</h5>
|
|
<SfGrid DataSource="@Roles"
|
|
AllowPaging="true"
|
|
ShowColumnMenu="true"
|
|
Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })">
|
|
<GridColumns>
|
|
<GridColumn Field="@nameof(RoleDto.Id)" IsPrimaryKey="true" HeaderText="ID" Width="70"></GridColumn>
|
|
<GridColumn Field="@nameof(RoleDto.Name)" HeaderText="Nazwa" Width="150"></GridColumn>
|
|
</GridColumns>
|
|
<GridEditSettings AllowDeleting="true"
|
|
ShowDeleteConfirmDialog="true"
|
|
AllowAdding="true"
|
|
AllowEditing="true"
|
|
Mode="EditMode.Normal">
|
|
</GridEditSettings>
|
|
<GridEvents OnActionBegin="RoleActionBegin"
|
|
OnActionComplete="RoleActionComplete"
|
|
TValue="RoleDto">
|
|
</GridEvents>
|
|
<GridPageSettings PageSize="10"></GridPageSettings>
|
|
</SfGrid>
|
|
|
|
<h5 class="text-primary mb-3 mt-4">Funkcje</h5>
|
|
<SfGrid DataSource="@Functions"
|
|
AllowPaging="true"
|
|
ShowColumnMenu="true"
|
|
Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })">
|
|
<GridColumns>
|
|
<GridColumn Field="@nameof(FunctionDto.Id)" IsPrimaryKey="true" HeaderText="ID" Width="70"></GridColumn>
|
|
<GridColumn Field="@nameof(FunctionDto.RoleId)" HeaderText="ID Roli" Width="70"></GridColumn>
|
|
<GridColumn Field="@nameof(FunctionDto.Name)" HeaderText="Nazwa Funkcji" Width="200"></GridColumn>
|
|
</GridColumns>
|
|
<GridEditSettings AllowDeleting="true"
|
|
ShowDeleteConfirmDialog="true"
|
|
AllowAdding="true"
|
|
AllowEditing="true"
|
|
Mode="EditMode.Normal">
|
|
</GridEditSettings>
|
|
<GridEvents OnActionBegin="FunctionActionBegin"
|
|
OnActionComplete="FunctionActionComplete"
|
|
TValue="FunctionDto">
|
|
</GridEvents>
|
|
<GridPageSettings PageSize="10"></GridPageSettings>
|
|
</SfGrid>
|
|
|
|
<SfDialog Width="500px" Title="Dodano użytkownika!" IsModal="true" @bind-Visible="Visibility" AllowPrerender="true">
|
|
<DialogTemplates>
|
|
<Content>
|
|
<p>Użytkownik <strong>@Login</strong> został dodany pomyślnie!</p>
|
|
<p>Hasło tymczasowe: <strong>@TempPassword</strong></p>
|
|
</Content>
|
|
</DialogTemplates>
|
|
<DialogButtons>
|
|
<DialogButton Content="OK" IsPrimary="true" OnClick="@HideModal"/>
|
|
</DialogButtons>
|
|
</SfDialog>
|
|
</CardContent>
|
|
<CardFooter>
|
|
<small class="text-muted">FA Krosno Manager © @(DateTime.Now.Year)</small>
|
|
</CardFooter>
|
|
</SfCard>
|
|
</div>
|
|
|
|
@code {
|
|
private List<UserDto> UserList { get; set; } = new();
|
|
private List<RoleDto> Roles { get; set; } = new();
|
|
private List<FunctionDto> Functions { get; set; } = new();
|
|
|
|
private bool Visibility { get; set; }
|
|
|
|
private string Login { get; set; } = string.Empty;
|
|
private string TempPassword { get; set; } = string.Empty;
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
Visibility = false;
|
|
// ClaimsPrincipal currentUser = CustomAuthenticationStateProvider.GetCurrentUser();
|
|
//
|
|
// if (currentUser.Identity?.IsAuthenticated == false || currentUser.Identity?.Name != "pkus")
|
|
// {
|
|
// NavigationManager.NavigateTo("/Unauthorized");
|
|
// }
|
|
// else
|
|
// {
|
|
await LoadUsers();
|
|
await LoadRoles();
|
|
//await LoadFunctions();
|
|
StateHasChanged();
|
|
// }
|
|
}
|
|
}
|
|
|
|
private async Task LoadUsers()
|
|
{
|
|
UserList = (await UserService.GetUsersAsync() ?? Array.Empty<UserDto>()).ToList();
|
|
}
|
|
|
|
private async Task LoadRoles()
|
|
{
|
|
Roles = (await RoleService.GetRolesAsync() ?? Array.Empty<RoleDto>()).ToList();
|
|
}
|
|
|
|
private async Task LoadFunctions()
|
|
{
|
|
Functions = (await FunctionService.GetFunctionsAsync() ?? Array.Empty<FunctionDto>()).ToList();
|
|
}
|
|
|
|
public async Task ResetPassword(UserDto? user)
|
|
{
|
|
if(user == null) return;
|
|
|
|
TempPassword = Guid.NewGuid().ToString().Substring(0, 8);
|
|
Login = user.Login;
|
|
|
|
string passwordHash = BCrypt.Net.BCrypt.HashPassword(TempPassword);
|
|
|
|
user.PasswordHash = passwordHash;
|
|
user.IsTemporaryPassword = true;
|
|
|
|
await UserService.UpdateUserAsync(user);
|
|
await LoadUsers();
|
|
|
|
Visibility = true;
|
|
}
|
|
|
|
private async Task UserActionBegin(ActionEventArgs<UserDto> args)
|
|
{
|
|
switch (args.RequestType)
|
|
{
|
|
case Action.Delete:
|
|
await UserService.DeleteUserAsync(args.Data.RowPointer);
|
|
break;
|
|
case Action.Add:
|
|
args.Data.RowPointer = Guid.NewGuid();
|
|
args.Data.CreatedDate = DateTime.Now;
|
|
args.Data.IsActive = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
private async Task UserActionComplete(ActionEventArgs<UserDto> args)
|
|
{
|
|
switch (args.RequestType)
|
|
{
|
|
case Action.Delete:
|
|
await LoadUsers();
|
|
break;
|
|
case Action.Save when args.Data.Id == 0:
|
|
UserDto? user = args.Data;
|
|
TempPassword = Guid.NewGuid().ToString().Substring(0, 8);
|
|
Login = user.Login;
|
|
|
|
string? passwordHash = BCrypt.Net.BCrypt.HashPassword(TempPassword);
|
|
|
|
user.PasswordHash = passwordHash;
|
|
user.IsTemporaryPassword = true;
|
|
user.ActiveFrom = DateTime.Now;
|
|
user.CreatedDate = DateTime.Now;
|
|
|
|
await UserService.AddUserAsync(user);
|
|
await LoadUsers();
|
|
|
|
Visibility = true;
|
|
break;
|
|
case Action.Save when args.Data.Id != 0:
|
|
await UserService.UpdateUserAsync(args.Data);
|
|
await LoadUsers();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private async Task RoleActionBegin(ActionEventArgs<RoleDto> args)
|
|
{
|
|
if (args.RequestType.Equals(Action.Delete))
|
|
{
|
|
await RoleService.DeleteRoleAsync(args.Data.RowPointer);
|
|
}
|
|
else if (args.RequestType.Equals(Action.Add))
|
|
{
|
|
args.Data.RowPointer = Guid.NewGuid();
|
|
}
|
|
}
|
|
|
|
private async Task RoleActionComplete(ActionEventArgs<RoleDto> args)
|
|
{
|
|
switch (args.RequestType)
|
|
{
|
|
case Action.Delete:
|
|
await LoadRoles();
|
|
break;
|
|
case Action.Save when args.Data.Id == 0:
|
|
await RoleService.AddRoleAsync(args.Data);
|
|
await LoadUsers();
|
|
break;
|
|
case Action.Save when args.Data.Id != 0:
|
|
await RoleService.UpdateRoleAsync(args.Data);
|
|
await LoadRoles();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private async Task FunctionActionBegin(ActionEventArgs<FunctionDto> args)
|
|
{
|
|
if (args.RequestType.Equals(Action.Delete))
|
|
{
|
|
await FunctionService.DeleteFunctionAsync(args.Data.RowPointer);
|
|
}
|
|
else if (args.RequestType.Equals(Action.Add))
|
|
{
|
|
args.Data.RowPointer = Guid.NewGuid();
|
|
}
|
|
}
|
|
|
|
private async Task FunctionActionComplete(ActionEventArgs<FunctionDto> args)
|
|
{
|
|
switch (args.RequestType)
|
|
{
|
|
case Action.Delete:
|
|
await LoadFunctions();
|
|
break;
|
|
case Action.Save when args.Data.Id == 0:
|
|
await FunctionService.AddFunctionAsync(args.Data);
|
|
await LoadFunctions();
|
|
break;
|
|
case Action.Save when args.Data.Id != 0:
|
|
await FunctionService.UpdateFunctionAsync(args.Data);
|
|
await LoadFunctions();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void HideModal()
|
|
{
|
|
Visibility = false;
|
|
}
|
|
} |