From 07a41aeaf5bb1a14883c532f06efbe625de7df44 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 17 Sep 2023 17:26:48 -0500 Subject: [PATCH] Add ability for admins to change user password Ref #28 --- .../Pages/Settings/Users/ChangePassword.razor | 61 +++++++++++++++++++ .../{Users.razor => Users/Index.razor} | 12 ++++ 2 files changed, 73 insertions(+) create mode 100644 LANCommander/Pages/Settings/Users/ChangePassword.razor rename LANCommander/Pages/Settings/{Users.razor => Users/Index.razor} (91%) diff --git a/LANCommander/Pages/Settings/Users/ChangePassword.razor b/LANCommander/Pages/Settings/Users/ChangePassword.razor new file mode 100644 index 0000000..c660396 --- /dev/null +++ b/LANCommander/Pages/Settings/Users/ChangePassword.razor @@ -0,0 +1,61 @@ +@page "/Settings/Users/{id:guid}/ChangePassword" +@layout SettingsLayout +@inject UserManager UserManager +@inject IMessageService MessageService +@attribute [Authorize(Roles = "Administrator")] + + + +
+
+ + + + + + + + + + + +
+
+ +@code { + [Parameter] public Guid Id { get; set; } + + User User; + ChangePasswordModel Model = new ChangePasswordModel(); + + protected override async Task OnInitializedAsync() + { + if (Id != Guid.Empty) + { + User = await UserManager.FindByIdAsync(Id.ToString()); + } + } + + async void Change() + { + try + { + var token = await UserManager.GeneratePasswordResetTokenAsync(User); + + if (Model.NewPassword == Model.NewPasswordConfirm) + await UserManager.ResetPasswordAsync(User, token, Model.NewPassword); + + await MessageService.Success("Password changed!"); + } + catch + { + await MessageService.Error("The user's password could not be changed"); + } + } + + public class ChangePasswordModel + { + public string NewPassword { get; set; } + public string NewPasswordConfirm { get; set; } + } +} diff --git a/LANCommander/Pages/Settings/Users.razor b/LANCommander/Pages/Settings/Users/Index.razor similarity index 91% rename from LANCommander/Pages/Settings/Users.razor rename to LANCommander/Pages/Settings/Users/Index.razor index 77d5d8d..4c76037 100644 --- a/LANCommander/Pages/Settings/Users.razor +++ b/LANCommander/Pages/Settings/Users/Index.razor @@ -4,6 +4,7 @@ @inject UserManager UserManager @inject RoleManager RoleManager @inject IMessageService MessageService +@inject NavigationManager NavigationManager @attribute [Authorize(Roles = "Administrator")] @@ -37,6 +38,12 @@ } + + +