From 20fdd3e891fcace696a42a524ba0518fec207ac7 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Mon, 23 Oct 2023 11:24:22 -0400 Subject: [PATCH] fix #3427 - profile validation for User Management (Add/Edit) --- Oqtane.Client/Modules/Admin/Users/Add.razor | 156 +++++++++---------- Oqtane.Client/Modules/Admin/Users/Edit.razor | 97 +++++------- 2 files changed, 110 insertions(+), 143 deletions(-) diff --git a/Oqtane.Client/Modules/Admin/Users/Add.razor b/Oqtane.Client/Modules/Admin/Users/Add.razor index 10f24ff1..805155cb 100644 --- a/Oqtane.Client/Modules/Admin/Users/Add.razor +++ b/Oqtane.Client/Modules/Admin/Users/Add.razor @@ -8,63 +8,63 @@ @inject IStringLocalizer Localizer @inject IStringLocalizer SharedLocalizer - - - @if (profiles != null) - { - -
-
- -
- +@if (_initialized) +{ + + + @if (profiles != null) + { + +
+
+ +
+ +
+
+
+ +
+
+ + +
+
+
+
+ +
+
+ + +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
-
- -
-
- - -
-
-
-
- -
-
- - -
-
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- } - - - @if (profiles != null) - { + } + +
@foreach (Profile profile in profiles) @@ -79,8 +79,8 @@ }
-
- @if (!string.IsNullOrEmpty(p.Options)) +
+ @if (!string.IsNullOrEmpty(p.Options)) { - } - else - { - - } + } else { - @if (p.IsRequired) - { - - } - else - { - - } + } }
@@ -126,15 +112,17 @@ }
- } - - -
-
- -@SharedLocalizer["Cancel"] + + +
+
+ + @SharedLocalizer["Cancel"] +} + @code { + private bool _initialized = false; private string _passwordrequirements; private string _username = string.Empty; private string _password = string.Empty; @@ -158,6 +146,7 @@ _togglepassword = SharedLocalizer["ShowPassword"]; profiles = await ProfileService.GetProfilesAsync(ModuleState.SiteId); settings = new Dictionary(); + _initialized = true; } catch (Exception ex) { @@ -231,13 +220,14 @@ { foreach (Profile profile in profiles) { - if (string.IsNullOrEmpty(SettingService.GetSetting(settings, profile.Name, string.Empty)) && !string.IsNullOrEmpty(profile.DefaultValue)) + var value = GetProfileValue(profile.Name, string.Empty); + if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(profile.DefaultValue)) { settings = SettingService.SetSetting(settings, profile.Name, profile.DefaultValue); } if (!profile.IsPrivate || UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin)) { - if (profile.IsRequired && string.IsNullOrEmpty(SettingService.GetSetting(settings, profile.Name, string.Empty))) + if (profile.IsRequired && string.IsNullOrEmpty(value)) { AddModuleMessage(string.Format(SharedLocalizer["ProfileRequired"], profile.Title), MessageType.Warning); return false; @@ -245,7 +235,7 @@ if (!string.IsNullOrEmpty(profile.Validation)) { Regex regex = new Regex(profile.Validation); - bool valid = regex.Match(SettingService.GetSetting(settings, profile.Name, string.Empty)).Success; + bool valid = regex.Match(value).Success; if (!valid) { AddModuleMessage(string.Format(SharedLocalizer["ProfileInvalid"], profile.Title), MessageType.Warning); diff --git a/Oqtane.Client/Modules/Admin/Users/Edit.razor b/Oqtane.Client/Modules/Admin/Users/Edit.razor index ea0198c9..8d43a4d6 100644 --- a/Oqtane.Client/Modules/Admin/Users/Edit.razor +++ b/Oqtane.Client/Modules/Admin/Users/Edit.razor @@ -9,18 +9,10 @@ @inject IStringLocalizer Localizer @inject IStringLocalizer SharedLocalizer -@if (PageState.User != null && photo != null) +@if (_initialized) { - @displayname -} -else -{ -
-} - - - @if (profiles != null) - { + +
@@ -31,20 +23,20 @@ else
-
+
- + -
+
- + -
+
@@ -87,11 +79,8 @@ else
- } -
- - @if (profiles != null) - { + +
@foreach (Profile profile in profiles) @@ -106,8 +95,8 @@ else }
-
- @if (!string.IsNullOrEmpty(p.Options)) +
+ @if (!string.IsNullOrEmpty(p.Options)) { - } - else - { - - } + } else { - @if (p.IsRequired) - { - - } - else - { - - } + } }
@@ -153,17 +128,18 @@ else }
- } - - + + - -@SharedLocalizer["Cancel"] -
-
- + + @SharedLocalizer["Cancel"] +
+
+ +} -@code { + @code { + private bool _initialized = false; private string _passwordrequirements; private int userid; private string username = string.Empty; @@ -175,7 +151,6 @@ else private string displayname = string.Empty; private FileManager filemanager; private int photofileid = -1; - private File photo = null; private string isdeleted; private string lastlogin; private string lastipaddress; @@ -193,16 +168,17 @@ else public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit; - protected override async Task OnParametersSetAsync() + protected override async Task OnInitializedAsync() { try { - if (PageState.QueryString.ContainsKey("id")) + _passwordrequirements = await UserService.GetPasswordRequirementsAsync(PageState.Site.SiteId); + _togglepassword = SharedLocalizer["ShowPassword"]; + profiles = await ProfileService.GetProfilesAsync(PageState.Site.SiteId); + + if (PageState.QueryString.ContainsKey("id") && int.TryParse(PageState.QueryString["id"], out int UserId)) { - _passwordrequirements = await UserService.GetPasswordRequirementsAsync(PageState.Site.SiteId); - _togglepassword = SharedLocalizer["ShowPassword"]; - profiles = await ProfileService.GetProfilesAsync(PageState.Site.SiteId); - userid = Int32.Parse(PageState.QueryString["id"]); + userid = UserId; var user = await UserService.GetUserAsync(userid, PageState.Site.SiteId); if (user != null) { @@ -212,12 +188,10 @@ else if (user.PhotoFileId != null) { photofileid = user.PhotoFileId.Value; - photo = await FileService.GetFileAsync(photofileid); } else { photofileid = -1; - photo = null; } isdeleted = user.IsDeleted.ToString(); lastlogin = string.Format("{0:MMM dd yyyy HH:mm:ss}", user.LastLoginOn); @@ -232,6 +206,8 @@ else deletedon = user.DeletedOn; } } + + _initialized = true; } catch (Exception ex) { @@ -309,13 +285,14 @@ else { foreach (Profile profile in profiles) { - if (string.IsNullOrEmpty(SettingService.GetSetting(settings, profile.Name, string.Empty)) && !string.IsNullOrEmpty(profile.DefaultValue)) + var value = GetProfileValue(profile.Name, string.Empty); + if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(profile.DefaultValue)) { settings = SettingService.SetSetting(settings, profile.Name, profile.DefaultValue); } if (!profile.IsPrivate || UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin)) { - if (profile.IsRequired && string.IsNullOrEmpty(SettingService.GetSetting(settings, profile.Name, string.Empty))) + if (profile.IsRequired && string.IsNullOrEmpty(value)) { AddModuleMessage(string.Format(SharedLocalizer["ProfileRequired"], profile.Title), MessageType.Warning); return false; @@ -323,7 +300,7 @@ else if (!string.IsNullOrEmpty(profile.Validation)) { Regex regex = new Regex(profile.Validation); - bool valid = regex.Match(SettingService.GetSetting(settings, profile.Name, string.Empty)).Success; + bool valid = regex.Match(value).Success; if (!valid) { AddModuleMessage(string.Format(SharedLocalizer["ProfileInvalid"], profile.Title), MessageType.Warning);