mirror of
https://github.com/Stefan-5422/-T5-Elektrifikatsiya.git
synced 2026-09-04 08:55:59 +02:00
UserSystemUpdate
This commit is contained in:
@@ -3,8 +3,13 @@
|
|||||||
@using Elektrifikatsiya.Models;
|
@using Elektrifikatsiya.Models;
|
||||||
@using System.Net
|
@using System.Net
|
||||||
@using Elektrifikatsiya.Services;
|
@using Elektrifikatsiya.Services;
|
||||||
|
@using Elektrifikatsiya.Services.Implementations;
|
||||||
|
@using FluentResults;
|
||||||
|
|
||||||
|
@inject IDeviceManagmentService DeviceManagmentService
|
||||||
|
@inject IMessageService MessageService
|
||||||
@inject IAuthenticationService AuthenthicationService;
|
@inject IAuthenticationService AuthenthicationService;
|
||||||
|
@inject NavigationManager NavigationManager;
|
||||||
<Div>
|
<Div>
|
||||||
<Row Style="width: 100%; height: 100%" Margin="Margin.Is3.FromTop.OnMobile" Padding="Padding.Is4.FromStart.OnMobile">
|
<Row Style="width: 100%; height: 100%" Margin="Margin.Is3.FromTop.OnMobile" Padding="Padding.Is4.FromStart.OnMobile">
|
||||||
|
|
||||||
@@ -59,7 +64,7 @@
|
|||||||
</Column>
|
</Column>
|
||||||
|
|
||||||
<Column ColumnSize="ColumnSize.Is4.OnDesktop.Is7.OnMobile">
|
<Column ColumnSize="ColumnSize.Is4.OnDesktop.Is7.OnMobile">
|
||||||
<Button Clicked="@ShowModalDelete" class="btn1" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Dark" Outline>
|
<Button @onclick="() => Delete(context.Item)" class="btn1" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Dark" Outline>
|
||||||
<BarIcon IconName="IconName.Delete" />
|
<BarIcon IconName="IconName.Delete" />
|
||||||
</Button>
|
</Button>
|
||||||
<style>
|
<style>
|
||||||
@@ -77,39 +82,20 @@
|
|||||||
<ModalBody>
|
<ModalBody>
|
||||||
<Field>
|
<Field>
|
||||||
<FieldLabel>User Name</FieldLabel>
|
<FieldLabel>User Name</FieldLabel>
|
||||||
<TextEdit Placeholder="Enter user name..." />
|
<TextEdit @ref="modalAddUserName" Placeholder="Enter user name..." />
|
||||||
</Field>
|
</Field>
|
||||||
<Field>
|
<Field>
|
||||||
<FieldLabel>User E-Mail</FieldLabel>
|
<FieldLabel>User E-Mail</FieldLabel>
|
||||||
<TextEdit Placeholder="Enter user E-mail..." />
|
<TextEdit @ref="modalAddUserEmail" Placeholder="Enter user E-mail..." />
|
||||||
</Field>
|
</Field>
|
||||||
<Field>
|
<Field>
|
||||||
<FieldLabel>User Password</FieldLabel>
|
<FieldLabel>User Password</FieldLabel>
|
||||||
<TextEdit Placeholder="Enter user password..." />
|
<TextEdit @ref="modalAddUserPW" Placeholder="Enter user password..." />
|
||||||
</Field>
|
</Field>
|
||||||
</ModalBody>
|
</ModalBody>
|
||||||
<ModalFooter>
|
<ModalFooter>
|
||||||
<Button Color="Color.Secondary" style="border-bottom-left-radius: 3px; border-bottom-right-radius: 3px" Clicked="@HideModalAddUser">Close</Button>
|
<Button Color="Color.Secondary" style="border-bottom-left-radius: 3px; border-bottom-right-radius: 3px" Clicked="@HideModalAddUser">Close</Button>
|
||||||
<Button Color="Color.Primary" style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0" Clicked="@HideModalAddUser">Add</Button>
|
<Button Color="Color.Primary" style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0" Clicked="@AddButtonPressed">Add</Button>
|
||||||
</ModalFooter>
|
|
||||||
</ModalContent>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal @ref="modalRefDelete">
|
|
||||||
<ModalContent Centered>
|
|
||||||
<ModalHeader>
|
|
||||||
<ModalTitle>Delete User</ModalTitle>
|
|
||||||
<CloseButton />
|
|
||||||
</ModalHeader>
|
|
||||||
<ModalBody>
|
|
||||||
<Field>
|
|
||||||
<FieldLabel>Admin Password</FieldLabel>
|
|
||||||
<TextEdit Placeholder="Enter admin password..." />
|
|
||||||
</Field>
|
|
||||||
</ModalBody>
|
|
||||||
<ModalFooter>
|
|
||||||
<Button Color="Color.Secondary" style="border-bottom-left-radius: 3px; border-bottom-right-radius: 3px" Clicked="@HideModalDelete">Close</Button>
|
|
||||||
<Button Color="Color.Primary" style="border-bottom-left-radius: 0px; border-bottom-right-radius: 0px" Clicked="@HideModalDelete">Delete</Button>
|
|
||||||
</ModalFooter>
|
</ModalFooter>
|
||||||
</ModalContent>
|
</ModalContent>
|
||||||
</Modal>
|
</Modal>
|
||||||
@@ -207,7 +193,7 @@
|
|||||||
</Div>
|
</Div>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
List<User> users = new List<User>() { new User("Silas", "aldjlfasd", "[email protected]", Role.Admin), new User("Joe", "qiowruioewjfopa", "[email protected]", Role.Admin), new User("Stefan", "adfadsdf", "[email protected]" , Role.Admin), new User("Manu", "öhöfldaösldf", "[email protected]", Role.User) };
|
List<User> users = new List<User>();
|
||||||
|
|
||||||
User? SelectedUser = null;
|
User? SelectedUser = null;
|
||||||
User? UserCopy = null;
|
User? UserCopy = null;
|
||||||
@@ -215,10 +201,19 @@
|
|||||||
bool hideButtonSettings = true;
|
bool hideButtonSettings = true;
|
||||||
bool newpage = true;
|
bool newpage = true;
|
||||||
|
|
||||||
private Modal? modalRefDelete;
|
private Modal? modalRefAddUser;
|
||||||
|
private TextEdit? modalAddUserName;
|
||||||
|
private TextEdit? modalAddUserEmail;
|
||||||
|
private TextEdit? modalAddUserPW;
|
||||||
|
|
||||||
public Admin()
|
protected override async void OnInitialized()
|
||||||
{
|
{
|
||||||
|
Result<List<User>> getUsersResult = await AuthenthicationService.GetUsers();
|
||||||
|
|
||||||
|
if (getUsersResult.IsSuccess)
|
||||||
|
{
|
||||||
|
users = getUsersResult.Value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Toggle(User user)
|
private void Toggle(User user)
|
||||||
@@ -238,28 +233,53 @@
|
|||||||
newpage = false;
|
newpage = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task ShowModalDelete()
|
|
||||||
{
|
|
||||||
return modalRefDelete!.Show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Task HideModalDelete() => modalRefDelete!.Hide();
|
|
||||||
|
|
||||||
private Modal? modalRefAddUser;
|
|
||||||
|
|
||||||
|
|
||||||
private Task ShowModalAddUser()
|
private Task ShowModalAddUser()
|
||||||
{
|
{
|
||||||
return modalRefAddUser!.Show();
|
return modalRefAddUser!.Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task HideModalAddUser() => modalRefAddUser!.Hide();
|
private Task HideModalAddUser() => modalRefAddUser!.Hide();
|
||||||
private async Task AddModalAddUser(string name, string passwort)
|
|
||||||
|
private async void AddButtonPressed()
|
||||||
{
|
{
|
||||||
await AuthenthicationService.RegisterUserAsync(name, passwort, Role.User);
|
|
||||||
await HideModalAddUser();
|
await HideModalAddUser();
|
||||||
|
await RegisterUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task RegisterUser()
|
||||||
|
{
|
||||||
|
Result<Device> result = await AuthenthicationService.RegisterUserAsync(modalAddUserName.Text.ToString(), modalAddUserEmail.Text.ToString(), modalAddUserPW.Text.ToString(), Role.User);
|
||||||
|
|
||||||
|
if (result.IsSuccess)
|
||||||
|
{
|
||||||
|
await MessageService.Success("User added!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await MessageService.Error(string.Join(',', result.Errors.Select(e => e.Message)), "Adding User failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<List<User>> getUsersResult = await AuthenthicationService.GetUsers();
|
||||||
|
|
||||||
|
if (getUsersResult.IsSuccess)
|
||||||
|
{
|
||||||
|
users = getUsersResult.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
StateHasChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Delete(User contextItem)
|
||||||
|
{
|
||||||
|
bool succ = await MessageService.Confirm($"Do you really want to delete the User \"{contextItem.Name}\"?");
|
||||||
|
|
||||||
|
if (!succ)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await AuthenthicationService.DeleteUser(contextItem);
|
||||||
|
|
||||||
|
NavigationManager.NavigateTo(NavigationManager.Uri, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,8 +52,8 @@
|
|||||||
</Column>
|
</Column>
|
||||||
|
|
||||||
<Column ColumnSize="ColumnSize.Is4.OnDesktop.Is7.OnMobile">
|
<Column ColumnSize="ColumnSize.Is4.OnDesktop.Is7.OnMobile">
|
||||||
<Button class="btn1" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Dark" Outline>
|
<Button @onclick="() => Delete(context.Item)" class="btn1" Position="Position.Absolute.Top.Is50.Start.Is50.Translate.Middle" Color="Color.Dark" Outline>
|
||||||
<BarIcon @onclick="() => Delete(context.Item)" IconName="IconName.Delete" />
|
<BarIcon IconName="IconName.Delete" />
|
||||||
</Button>
|
</Button>
|
||||||
<style>
|
<style>
|
||||||
.btn1:hover {
|
.btn1:hover {
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ IAuthenticationService authenticationService = serviceScope.ServiceProvider.GetR
|
|||||||
|
|
||||||
if (!mainDatabase.Users.Any())
|
if (!mainDatabase.Users.Any())
|
||||||
{
|
{
|
||||||
_ = authenticationService.RegisterUserAsync("admin", "admin", Role.Admin);
|
_ = authenticationService.RegisterUserAsync("admin", "admin", "", Role.Admin);
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ public interface IAuthenticationService
|
|||||||
/// <returns> <see cref="Task"/> to <see langword="await"/> and the currently authenticated user.</returns>
|
/// <returns> <see cref="Task"/> to <see langword="await"/> and the currently authenticated user.</returns>
|
||||||
Task<Result<User>> GetUserAsync();
|
Task<Result<User>> GetUserAsync();
|
||||||
|
|
||||||
|
Task<Result<List<User>>> GetUsers();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a new user.
|
/// Registers a new user.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -20,7 +22,7 @@ public interface IAuthenticationService
|
|||||||
/// <param name="role">The default role of the user.</param>
|
/// <param name="role">The default role of the user.</param>
|
||||||
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
|
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
|
||||||
|
|
||||||
Task<Result> RegisterUserAsync(string name, string password, Role role);
|
Task<Result> RegisterUserAsync(string name, string password, string email, Role role);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Logs a user in.
|
/// Logs a user in.
|
||||||
@@ -42,12 +44,14 @@ public interface IAuthenticationService
|
|||||||
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
|
/// <returns>A <see cref="Task"/> to <see langword="await"/>.</returns>
|
||||||
Task<Result> DeleteUserAsync();
|
Task<Result> DeleteUserAsync();
|
||||||
|
|
||||||
/// <summary>
|
Task<Result> DeleteUser(User user);
|
||||||
/// Checks if a user exists.
|
|
||||||
/// </summary>
|
/// <summary>
|
||||||
/// <param name="name">The name address of the user.</param>
|
/// Checks if a user exists.
|
||||||
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the user exists.</returns>
|
/// </summary>
|
||||||
Task<Result<bool>> UserExistsAsync(string name);
|
/// <param name="name">The name address of the user.</param>
|
||||||
|
/// <returns>A <see cref="Task"/> to <see langword="await"/> and a <see cref="bool"/> that indicates if the user exists.</returns>
|
||||||
|
Task<Result<bool>> UserExistsAsync(string name);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check if the current user is authenticated.
|
/// Check if the current user is authenticated.
|
||||||
|
|||||||
+15
-4
@@ -36,7 +36,13 @@ public class AuthenticationService : IAuthenticationService
|
|||||||
return (await Result.Try(() => mainDatabaseContext.SaveChangesAsync())).ToResult();
|
return (await Result.Try(() => mainDatabaseContext.SaveChangesAsync())).ToResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result<User>> GetUserAsync()
|
public async Task<Result> DeleteUser(User user)
|
||||||
|
{
|
||||||
|
_ = mainDatabaseContext.Users.Remove(user);
|
||||||
|
return (await Result.Try(() => mainDatabaseContext.SaveChangesAsync())).ToResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<User>> GetUserAsync()
|
||||||
{
|
{
|
||||||
string? token = httpContextAccessor.HttpContext?.Request.Cookies["token"]?.ToString();
|
string? token = httpContextAccessor.HttpContext?.Request.Cookies["token"]?.ToString();
|
||||||
|
|
||||||
@@ -55,7 +61,12 @@ public class AuthenticationService : IAuthenticationService
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result<bool>> IsAuthenticated()
|
public async Task<Result<List<User>>> GetUsers()
|
||||||
|
{
|
||||||
|
return await mainDatabaseContext.Users.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Result<bool>> IsAuthenticated()
|
||||||
{
|
{
|
||||||
return (await GetUserAsync()).IsSuccess;
|
return (await GetUserAsync()).IsSuccess;
|
||||||
}
|
}
|
||||||
@@ -111,7 +122,7 @@ public class AuthenticationService : IAuthenticationService
|
|||||||
return Result.Ok();
|
return Result.Ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Result> RegisterUserAsync(string name, string password, Role role)
|
public async Task<Result> RegisterUserAsync(string name, string password, string email, Role role)
|
||||||
{
|
{
|
||||||
Result<bool> userExistsResult = await UserExistsAsync(name);
|
Result<bool> userExistsResult = await UserExistsAsync(name);
|
||||||
|
|
||||||
@@ -120,7 +131,7 @@ public class AuthenticationService : IAuthenticationService
|
|||||||
return Result.Fail("User name already exists!");
|
return Result.Fail("User name already exists!");
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = new User(name, BC.HashPassword(password), "", role);
|
User user = new User(name, BC.HashPassword(password), email, role);
|
||||||
_ = mainDatabaseContext.Users.Add(user);
|
_ = mainDatabaseContext.Users.Add(user);
|
||||||
|
|
||||||
return (await Result.Try(() => mainDatabaseContext.SaveChangesAsync())).ToResult();
|
return (await Result.Try(() => mainDatabaseContext.SaveChangesAsync())).ToResult();
|
||||||
|
|||||||
Reference in New Issue
Block a user