From 8882e19ec5c11d73d56a39c201392b9ce1e925b0 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Sat, 18 Jun 2022 09:18:23 -0400 Subject: [PATCH] allow multiple aliases to be defined as default --- Oqtane.Client/Modules/Admin/Site/Index.razor | 384 ++++++++++-------- .../Resources/Modules/Admin/Site/Index.resx | 11 +- 2 files changed, 219 insertions(+), 176 deletions(-) diff --git a/Oqtane.Client/Modules/Admin/Site/Index.razor b/Oqtane.Client/Modules/Admin/Site/Index.razor index 3d0ce023..d033a259 100644 --- a/Oqtane.Client/Modules/Admin/Site/Index.razor +++ b/Oqtane.Client/Modules/Admin/Site/Index.razor @@ -170,24 +170,54 @@ @if (_aliases != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) {
-
-
- -
- +
+
+ +
+ + +
+   +   + @Localizer["AliasName"] + @Localizer["AliasDefault"] +
+ + @if (context.AliasId != _aliasid) + { + + @if (_aliasid == -1) + { + + } + + + @if (_aliasid == -1) + { + + } + + @context.Name + @context.IsDefault + } + else + { + + + + + + + + + } + +
+
-
-
- -
- -
-
@@ -253,8 +283,9 @@ private List _containers = new List(); private string _name = string.Empty; private List _aliases; - private string _defaultalias = string.Empty; - private string _urls = string.Empty; + private int _aliasid = -1; + private string _aliasname; + private string _defaultalias; private string _runtime = ""; private string _prerender = ""; private int _logofileid = -1; @@ -304,10 +335,7 @@ _prerender = site.RenderMode.Replace(_runtime, ""); _isdeleted = site.IsDeleted.ToString(); - if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) - { - await GetAliases(); - } + await GetAliases(); if (site.LogoFileId != null) { @@ -409,152 +437,92 @@ { if (_name != string.Empty && _themetype != "-" && _containertype != "-") { - var unique = true; - if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) + var site = await SiteService.GetSiteAsync(PageState.Site.SiteId); + if (site != null) { - _urls = Regex.Replace(_urls, @"\r\n?|\n", ","); // convert line breaks to commas - var aliases = await AliasService.GetAliasesAsync(); - foreach (string name in _urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(sValue => sValue.Trim()).ToArray()) + bool refresh = false; + bool reload = false; + + site.Name = _name; + if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) { - var alias = aliases.Where(item => item.Name == name).FirstOrDefault(); - if (alias != null && unique) + if (site.Runtime != _runtime || site.RenderMode != _runtime + _prerender) { - unique = (alias.TenantId == PageState.Site.TenantId && alias.SiteId == PageState.Site.SiteId); + site.Runtime = _runtime; + site.RenderMode = _runtime + _prerender; + reload = true; // needs to be reloaded on server } } - if (unique && string.IsNullOrEmpty(_defaultalias)) unique = false; - } + site.IsDeleted = (_isdeleted == null ? true : Boolean.Parse(_isdeleted)); - if (unique) - { - var site = await SiteService.GetSiteAsync(PageState.Site.SiteId); - if (site != null) + site.LogoFileId = null; + var logofileid = _logofilemanager.GetFileId(); + if (logofileid != -1) { - bool refresh = false; - bool reload = false; - - site.Name = _name; - if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) - { - if (site.Runtime != _runtime || site.RenderMode != _runtime + _prerender) - { - site.Runtime = _runtime; - site.RenderMode = _runtime + _prerender; - reload = true; // needs to be reloaded on server - } - } - site.IsDeleted = (_isdeleted == null ? true : Boolean.Parse(_isdeleted)); - - site.LogoFileId = null; - var logofileid = _logofilemanager.GetFileId(); - if (logofileid != -1) - { - site.LogoFileId = logofileid; - } - int? faviconFieldId = _faviconfilemanager.GetFileId(); - if (faviconFieldId == -1) faviconFieldId = null; - if (site.FaviconFileId != faviconFieldId) - { - site.FaviconFileId = faviconFieldId; - reload = true; // needs to be reloaded on server - } - if (site.DefaultThemeType != _themetype) - { - site.DefaultThemeType = _themetype; - refresh = true; // needs to be refreshed on client - } - if (site.DefaultContainerType != _containertype) - { - site.DefaultContainerType = _containertype; - refresh = true; // needs to be refreshed on client - } - site.AdminContainerType = _admincontainertype; - - if (site.PwaIsEnabled.ToString() != _pwaisenabled) - { - site.PwaIsEnabled = Boolean.Parse(_pwaisenabled); - reload = true; // needs to be reloaded on server - } - int? pwaappiconfileid = _pwaappiconfilemanager.GetFileId(); - if (pwaappiconfileid == -1) pwaappiconfileid = null; - if (site.PwaAppIconFileId != pwaappiconfileid) - { - site.PwaAppIconFileId = pwaappiconfileid; - reload = true; // needs to be reloaded on server - } - int? pwasplashiconfileid = _pwasplashiconfilemanager.GetFileId(); - if (pwasplashiconfileid == -1) pwasplashiconfileid = null; - if (site.PwaSplashIconFileId != pwasplashiconfileid) - { - site.PwaSplashIconFileId = pwasplashiconfileid; - reload = true; // needs to be reloaded on server - } - - site = await SiteService.UpdateSiteAsync(site); - - var settings = await SettingService.GetSiteSettingsAsync(site.SiteId); - settings = SettingService.SetSetting(settings, "SMTPHost", _smtphost, true); - settings = SettingService.SetSetting(settings, "SMTPPort", _smtpport, true); - settings = SettingService.SetSetting(settings, "SMTPSSL", _smtpssl, true); - settings = SettingService.SetSetting(settings, "SMTPUsername", _smtpusername, true); - settings = SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true); - settings = SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true); - settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true); - await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId); - - if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) - { - var names = _urls.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) - .Select(sValue => sValue.Trim()).ToArray(); - foreach (Alias alias in _aliases) - { - if (!names.Contains(alias.Name.Trim())) - { - await AliasService.DeleteAliasAsync(alias.AliasId); - } - } - - foreach (string name in names) - { - var alias = _aliases.Find(item => item.Name.Trim() == name); - if (alias == null) - { - alias = new Alias(); - alias.Name = name; - alias.TenantId = site.TenantId; - alias.SiteId = site.SiteId; - alias.IsDefault = (name == _defaultalias); - await AliasService.AddAliasAsync(alias); - } - else - { - if (alias.Name != name || alias.IsDefault != (alias.Name.Trim() == _defaultalias)) - { - alias.Name = name; - alias.IsDefault = (name == _defaultalias); - await AliasService.UpdateAliasAsync(alias); - } - } - } - await GetAliases(); - } - - await logger.LogInformation("Site Settings Saved {Site}", site); - - if (refresh || reload) - { - NavigationManager.NavigateTo(NavigateUrl(true), reload); // refresh/reload - } - else - { - AddModuleMessage(Localizer["Success.Settings.SaveSite"], MessageType.Success); - await interop.ScrollTo(0, 0, "smooth"); - } + site.LogoFileId = logofileid; + } + int? faviconFieldId = _faviconfilemanager.GetFileId(); + if (faviconFieldId == -1) faviconFieldId = null; + if (site.FaviconFileId != faviconFieldId) + { + site.FaviconFileId = faviconFieldId; + reload = true; // needs to be reloaded on server + } + if (site.DefaultThemeType != _themetype) + { + site.DefaultThemeType = _themetype; + refresh = true; // needs to be refreshed on client + } + if (site.DefaultContainerType != _containertype) + { + site.DefaultContainerType = _containertype; + refresh = true; // needs to be refreshed on client + } + site.AdminContainerType = _admincontainertype; + + if (site.PwaIsEnabled.ToString() != _pwaisenabled) + { + site.PwaIsEnabled = Boolean.Parse(_pwaisenabled); + reload = true; // needs to be reloaded on server + } + int? pwaappiconfileid = _pwaappiconfilemanager.GetFileId(); + if (pwaappiconfileid == -1) pwaappiconfileid = null; + if (site.PwaAppIconFileId != pwaappiconfileid) + { + site.PwaAppIconFileId = pwaappiconfileid; + reload = true; // needs to be reloaded on server + } + int? pwasplashiconfileid = _pwasplashiconfilemanager.GetFileId(); + if (pwasplashiconfileid == -1) pwasplashiconfileid = null; + if (site.PwaSplashIconFileId != pwasplashiconfileid) + { + site.PwaSplashIconFileId = pwasplashiconfileid; + reload = true; // needs to be reloaded on server + } + + site = await SiteService.UpdateSiteAsync(site); + + var settings = await SettingService.GetSiteSettingsAsync(site.SiteId); + settings = SettingService.SetSetting(settings, "SMTPHost", _smtphost, true); + settings = SettingService.SetSetting(settings, "SMTPPort", _smtpport, true); + settings = SettingService.SetSetting(settings, "SMTPSSL", _smtpssl, true); + settings = SettingService.SetSetting(settings, "SMTPUsername", _smtpusername, true); + settings = SettingService.SetSetting(settings, "SMTPPassword", _smtppassword, true); + settings = SettingService.SetSetting(settings, "SMTPSender", _smtpsender, true); + settings = SettingService.SetSetting(settings, "NotificationRetention", _retention, true); + await SettingService.UpdateSiteSettingsAsync(settings, site.SiteId); + + await logger.LogInformation("Site Settings Saved {Site}", site); + + if (refresh || reload) + { + NavigationManager.NavigateTo(NavigateUrl(true), reload); // refresh/reload + } + else + { + AddModuleMessage(Localizer["Success.Settings.SaveSite"], MessageType.Success); + await interop.ScrollTo(0, 0, "smooth"); } - } - else // deuplicate alias or default alias not specified - { - AddModuleMessage(Localizer["Message.Aliases.Taken"], MessageType.Warning); } } else @@ -637,20 +605,6 @@ } } - private async Task GetAliases() - { - _urls = string.Empty; - _defaultalias = string.Empty; - _aliases = await AliasService.GetAliasesAsync(); - _aliases = _aliases.Where(item => item.SiteId == PageState.Site.SiteId && item.TenantId == PageState.Site.TenantId).OrderBy(item => item.AliasId).ToList(); - foreach (Alias alias in _aliases) - { - _urls += (_urls == string.Empty) ? alias.Name.Trim() : ", " + alias.Name.Trim(); - if (alias.IsDefault && string.IsNullOrEmpty(_defaultalias)) _defaultalias = alias.Name.Trim(); - } - if (string.IsNullOrEmpty(_defaultalias)) _defaultalias = _aliases.First().Name.Trim(); - } - private void ToggleSMTPPassword() { if (_smtppasswordtype == "password") @@ -664,4 +618,84 @@ _togglesmtppassword = SharedLocalizer["ShowPassword"]; } } + + private async Task GetAliases() + { + if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) + { + _aliases = await AliasService.GetAliasesAsync(); + _aliases = _aliases.Where(item => item.SiteId == PageState.Site.SiteId && item.TenantId == PageState.Site.TenantId).OrderBy(item => item.AliasId).ToList(); + } + } + + private void AddAlias() + { + _aliases.Add(new Alias { AliasId = 0, Name = "", IsDefault = false }); + _aliasid = 0; + _aliasname = ""; + _defaultalias = "False"; + StateHasChanged(); + } + + private void EditAlias(Alias alias) + { + _aliasid = alias.AliasId; + _aliasname = alias.Name; + _defaultalias = alias.IsDefault.ToString(); + StateHasChanged(); + } + + private async Task DeleteAlias(Alias alias) + { + if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) + { + await AliasService.DeleteAliasAsync(alias.AliasId); + await GetAliases(); + StateHasChanged(); + } + } + + private async Task SaveAlias() + { + if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) + { + if (!string.IsNullOrEmpty(_aliasname)) + { + var aliases = await AliasService.GetAliasesAsync(); + var alias = aliases.Where(item => item.Name == _aliasname).FirstOrDefault(); + bool unique = (alias == null || alias.AliasId == _aliasid); + if (unique) + { + if (_aliasid == 0) + { + alias = new Alias { SiteId = PageState.Site.SiteId, TenantId = PageState.Site.TenantId, Name = _aliasname, IsDefault = bool.Parse(_defaultalias) }; + await AliasService.AddAliasAsync(alias); + } + else + { + alias = _aliases.Single(item => item.AliasId == _aliasid); + alias.Name = _aliasname; + alias.IsDefault = bool.Parse(_defaultalias); + await AliasService.UpdateAliasAsync(alias); + } + } + else // duplicate alias + { + AddModuleMessage(Localizer["Message.Aliases.Taken"], MessageType.Warning); + } + } + await GetAliases(); + _aliasid = -1; + _aliasname = ""; + StateHasChanged(); + } + } + + private async Task CancelAlias() + { + await GetAliases(); + _aliasid = -1; + _aliasname = ""; + StateHasChanged(); + } } diff --git a/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx b/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx index 1007685f..66df7731 100644 --- a/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx +++ b/Oqtane.Client/Resources/Modules/Admin/Site/Index.resx @@ -142,7 +142,7 @@ Site Settings Saved - The Default Alias Has Not Been Specified Or An Alias Was Specified That Has Already Been Used For Another Site + An Alias Was Specified That Has Already Been Used For Another Site You Must Provide A Site Name, Alias, And Default Theme/Container @@ -324,4 +324,13 @@ Aliases + + Name + + + Default? + + + Are You Sure You Wish To Delete {0}? + \ No newline at end of file