diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs index 0dbc315..845600e 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Net.Mail; namespace Elektrifikatsiya.Models; @@ -10,7 +11,7 @@ public class User [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] - public int Id { get; private set; } + public int Id { get; set; } public DateTime LastLoginDate { get; set; } @@ -18,7 +19,7 @@ public class User public string Name { get; set; } [Required] - public string PasswordHash { get; private set; } + public string PasswordHash { get; set; } [Required] public string Email { get; set; } @@ -36,4 +37,8 @@ public class User Email = email; Role = role; } + public User CopyUser() + { + return new User(Name, PasswordHash, Email, Role) { Id = Id }; + } } \ No newline at end of file diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Admin.razor b/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Admin.razor index 05dc402..ea5aea6 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Admin.razor +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Pages/Admin.razor @@ -1,15 +1,18 @@ @page "/Admin" @using Blazorise.Components; @using Elektrifikatsiya.Models; +@using Elektrifikatsiya.Database; @using System.Net @using Elektrifikatsiya.Services; @using Elektrifikatsiya.Services.Implementations; @using FluentResults; +@using BC = BCrypt.Net.BCrypt; @inject IDeviceManagmentService DeviceManagmentService @inject IMessageService MessageService @inject IAuthenticationService AuthenthicationService; @inject NavigationManager NavigationManager; +@inject MainDatabaseContext MainDatabaseContext;
@@ -95,7 +98,7 @@ - + @@ -126,44 +129,49 @@ - - -
- User Name -
- - Here you can change the name of the User - -
- -
+ + + +
+ User Name +
+ + Here you can change the name of the User + +
+ +
+


- - -
- E-Mail -
- - Here you can change the E-Mail of a User - -
- -
+ + + +
+ E-Mail +
+ + Here you can change the E-Mail of a User + +
+ +
+

- - -
- Password -
- - Here you can change the password of a User - -
- -
- -
+
+ + + +
+ Password +
+ + Here you can change the password of a User + +
+ +
+


@@ -179,14 +187,14 @@ Roles - Admin + Admin - User + User - +
} @@ -199,6 +207,7 @@ List users = new List(); User? SelectedUser = null; + User? UserCopy = null; bool hideButtonSettings = true; bool newpage = true; @@ -207,6 +216,12 @@ private TextEdit? modalAddUserName; private TextEdit? modalAddUserEmail; private TextEdit? modalAddUserPW; + private TextEdit? updateName; + private TextEdit? updateEmail; + private TextEdit? updatePW; + bool admin = false; + + Validation? updateValidation; protected override async void OnInitialized() { @@ -232,6 +247,7 @@ } } SelectedUser = user; + UserCopy = user.CopyUser(); newpage = false; } @@ -271,6 +287,31 @@ StateHasChanged(); } + private async Task OnSave() + { + if (UserCopy is not null && SelectedUser is not null) + { + MainDatabaseContext.ChangeTracker.Clear(); + + UserCopy.Name = updateName.Text.ToString(); + UserCopy.Email = updateEmail.Text.ToString(); + UserCopy.PasswordHash = BC.HashPassword(updatePW.Text.ToString()); + if (admin) + { + UserCopy.Role = Role.Admin; + } + else + { + UserCopy.Role = Role.User; + } + + MainDatabaseContext.Update(UserCopy); + + await Result.Try(Task () => MainDatabaseContext.SaveChangesAsync()); + await MessageService.Success("User updated!"); + } + } + private async Task Delete(User contextItem) { bool succ = await MessageService.Confirm($"Do you really want to delete the User \"{contextItem.Name}\"?"); @@ -284,4 +325,57 @@ NavigationManager.NavigateTo(NavigationManager.Uri, true); } + private void SetRoleAdmin() + { + admin = true; + } + private void SetRoleUser() + { + admin = false; + } + public void ValidateName(ValidatorEventArgs e) + { + string? input = Convert.ToString(e.Value); + + if (string.IsNullOrWhiteSpace(input)) + { + e.Status = ValidationStatus.None; + return; + } + else + { + e.Status = ValidationStatus.Success; + return; + } + } + public void ValidateEmail(ValidatorEventArgs e) + { + string? input = Convert.ToString(e.Value); + + if (string.IsNullOrWhiteSpace(input)) + { + e.Status = ValidationStatus.None; + return; + } + else + { + e.Status = ValidationStatus.Success; + return; + } + } + public void ValidatePW(ValidatorEventArgs e) + { + string? input = Convert.ToString(e.Value); + + if (string.IsNullOrWhiteSpace(input)) + { + e.Status = ValidationStatus.None; + return; + } + else + { + e.Status = ValidationStatus.Success; + return; + } + } }