From 4c2007439d79f735ec38d4525ac8579a05328825 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Wed, 4 Mar 2020 13:22:58 -0500 Subject: [PATCH] module load error handler, router optimizaton, relative paths, fixed add existing module control panel issue --- Oqtane.Client/Modules/Admin/Error/Index.razor | 18 ++ .../Modules/Admin/RecycleBin/Index.razor | 209 +++++++++--------- Oqtane.Client/Modules/HtmlText/Edit.razor | 2 + Oqtane.Client/Modules/HtmlText/Index.razor | 3 +- Oqtane.Client/Modules/ModuleBase.cs | 10 +- Oqtane.Client/Shared/SiteRouter.razor | 90 ++++---- .../Themes/Controls/ControlPanel.razor | 94 ++++---- .../Themes/Controls/ModuleActions.razor | 2 +- Oqtane.Client/Themes/ThemeControlBase.cs | 10 +- Oqtane.Shared/Shared/Constants.cs | 3 + 10 files changed, 242 insertions(+), 199 deletions(-) create mode 100644 Oqtane.Client/Modules/Admin/Error/Index.razor diff --git a/Oqtane.Client/Modules/Admin/Error/Index.razor b/Oqtane.Client/Modules/Admin/Error/Index.razor new file mode 100644 index 00000000..8ecd898c --- /dev/null +++ b/Oqtane.Client/Modules/Admin/Error/Index.razor @@ -0,0 +1,18 @@ +@namespace Oqtane.Modules.Admin.Error +@inherits ModuleBase +@inject IModuleService ModuleService + +@code { + public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Anonymous; } } + + protected override async Task OnInitializedAsync() + { + Module module = await ModuleService.GetModuleAsync(ModuleState.ModuleId); + if (UserSecurity.IsAuthorized(PageState.User, Constants.HostRole)) + { + string message = "A Problem Was Encountered Loading Module " + module.ModuleDefinitionName; + AddModuleMessage(message, MessageType.Error); + } + await logger.LogCritical("Error Loading Module {Module}", module); + } +} diff --git a/Oqtane.Client/Modules/Admin/RecycleBin/Index.razor b/Oqtane.Client/Modules/Admin/RecycleBin/Index.razor index ec7ef28b..9ba7b95b 100644 --- a/Oqtane.Client/Modules/Admin/RecycleBin/Index.razor +++ b/Oqtane.Client/Modules/Admin/RecycleBin/Index.razor @@ -5,76 +5,92 @@ @inject IModuleService ModuleService @inject IPageService PageService - - - @if (pages.Count == 0) - { -
-

No Deleted Pages

- } - else - { - -
- Name - Deleted By - Deleted On -   -   -
- - @context.Name - @context.DeletedBy - @context.DeletedOn - - - -
- } -
- - @if (pageModules.Count == 0) - { -
-

No Deleted Modules

- } - else - { - -
- Page - Module - Deleted By - Deleted On -   -   -
- - @PageState.Pages.Find(item => item.PageId == context.PageId).Name - @context.Title - @context.DeletedBy - @context.DeletedOn - - - -
- } -
-
+
+
+ + + +
+
+ @if (pages == null) + { +
+

No Deleted Pages

+ } + else + { + +
+   +   + Name + Deleted By + Deleted On +
+ + + + @context.Name + @context.DeletedBy + @context.DeletedOn + +
+ } +
+
+ @if (modules == null) + { +
+

No Deleted Modules

+ } + else + { + +
+   +   + Page + Module + Deleted By + Deleted On +
+ + + + @PageState.Pages.Find(item => item.PageId == context.PageId).Name + @context.Title + @context.DeletedBy + @context.DeletedOn + +
+ } +
+
+
+
@code { public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } } - List pages { get; set; } - List pageModules { get; set; } + List pages; + List modules; protected override async Task OnInitializedAsync() { try { - pages = new List(); - pageModules = new List(); - await LoadEntities(); + await Load(); } catch (Exception ex) { @@ -83,19 +99,13 @@ } } - protected override void OnParametersSet() + private async Task Load() { - pages = PageState.Pages.Where(item => item.IsDeleted).ToList(); - } + pages = await PageService.GetPagesAsync(PageState.Site.SiteId); + pages = pages.Where(item => item.IsDeleted).ToList(); - private async Task LoadEntities() - { - pageModules.Clear(); - foreach (var module in PageState.Modules.Where(item => item.IsDeleted)) - { - var pageModule = await PageModuleService.GetPageModuleAsync(module.PageModuleId); - pageModules.Add(pageModule); - } + modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId); + modules = modules.Where(item => item.IsDeleted).ToList(); } private async Task RestorePage(Page Page) @@ -105,6 +115,8 @@ Page.IsDeleted = false; await PageService.UpdatePageAsync(Page); await logger.LogInformation("Page Restored {Page}", Page); + await Load(); + StateHasChanged(); NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); } catch (Exception ex) @@ -114,61 +126,60 @@ } } - private async Task DeletePage(int PageId) + private async Task DeletePage(Page Page) { try { - var deletedPageModules = PageState.Modules.Where(item => item.PageId == PageId); - await PageService.DeletePageAsync(PageId); - foreach (var module in deletedPageModules) - { - await ModuleService.DeleteModuleAsync(module.ModuleId); - } - await logger.LogInformation("Page Permanently Deleted {PageId}", PageId); + await PageService.DeletePageAsync(Page.PageId); + await logger.LogInformation("Page Permanently Deleted {Page}", Page); + await Load(); + StateHasChanged(); NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); } catch (Exception ex) { - await logger.LogError(ex, "Error Permanently Deleting Page {PageId} {Error}", PageId, ex.Message); + await logger.LogError(ex, "Error Permanently Deleting Page {Page} {Error}", Page, ex.Message); AddModuleMessage(ex.Message, MessageType.Error); } } - private async Task RestorePageModule(PageModule PageModule) + private async Task RestoreModule(Module Module) { try { - PageModule.IsDeleted = false; - await PageModuleService.UpdatePageModuleAsync(PageModule); - await LoadEntities(); - await logger.LogInformation("Page Module Restored {PageModule}", PageModule); - NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); + PageModule pagemodule = await PageModuleService.GetPageModuleAsync(Module.PageModuleId); + pagemodule.IsDeleted = false; + await PageModuleService.UpdatePageModuleAsync(pagemodule); + await logger.LogInformation("Module Restored {Module}", Module); + await Load(); + StateHasChanged(); } catch (Exception ex) { - await logger.LogError(ex, "Error Restoring Deleted Page Module {PageModule} {Error}", PageModule, ex.Message); - AddModuleMessage("Error Restoring Deleted Page Module", MessageType.Error); + await logger.LogError(ex, "Error Restoring Deleted Module {Module} {Error}", Module, ex.Message); + AddModuleMessage("Error Restoring Deleted Module", MessageType.Error); } } - private async Task DeletePageModule(int PageModuleId, int ModuleId) + private async Task DeleteModule(Module Module) { try { - await PageModuleService.DeletePageModuleAsync(PageModuleId); - if (PageState.Modules.Count(item => item.ModuleId == ModuleId) == 1) + await PageModuleService.DeletePageModuleAsync(Module.PageModuleId); + // check if there are any remaining module instances in the site + modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId); + if (!modules.Exists(item => item.ModuleId == Module.ModuleId)) { - await ModuleService.DeleteModuleAsync(ModuleId); + await ModuleService.DeleteModuleAsync(Module.ModuleId); } - PageState.Modules.RemoveAt(PageState.Modules.FindIndex(item => item.ModuleId == ModuleId)); - await LoadEntities(); - await logger.LogInformation("Page Module Permanently Deleted {PageModuleId}", PageModuleId); - NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); + await logger.LogInformation("Module Permanently Deleted {Module}", Module); + await Load(); + StateHasChanged(); } catch (Exception ex) { - await logger.LogError(ex, "Error Permanently Deleting Page Module {PageModuleId} {Error}", PageModuleId, ex.Message); - AddModuleMessage("Error Permanently Deleting Page Module", MessageType.Error); + await logger.LogError(ex, "Error Permanently Deleting Module {Module} {Error}", Module, ex.Message); + AddModuleMessage("Error Permanently Deleting Module", MessageType.Error); } } } diff --git a/Oqtane.Client/Modules/HtmlText/Edit.razor b/Oqtane.Client/Modules/HtmlText/Edit.razor index 0fb17a96..d396da3c 100644 --- a/Oqtane.Client/Modules/HtmlText/Edit.razor +++ b/Oqtane.Client/Modules/HtmlText/Edit.razor @@ -133,6 +133,8 @@ else content = await this.RichTextEditorHtml.GetHTML(); } + content = content.Replace(((PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path) + Constants.ContentUrl, Constants.ContentUrl); + try { HtmlTextService htmltextservice = new HtmlTextService(http, sitestate, NavigationManager); diff --git a/Oqtane.Client/Modules/HtmlText/Index.razor b/Oqtane.Client/Modules/HtmlText/Index.razor index b1d679c2..6360d882 100644 --- a/Oqtane.Client/Modules/HtmlText/Index.razor +++ b/Oqtane.Client/Modules/HtmlText/Index.razor @@ -13,7 +13,7 @@

@code { - string content; + string content = ""; protected override async Task OnParametersSetAsync() { @@ -24,6 +24,7 @@ if (htmltext != null) { content = htmltext.Content; + content = content.Replace(Constants.ContentUrl, ((PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path) + Constants.ContentUrl); } } catch (Exception ex) diff --git a/Oqtane.Client/Modules/ModuleBase.cs b/Oqtane.Client/Modules/ModuleBase.cs index 7f2e0872..4e60257a 100644 --- a/Oqtane.Client/Modules/ModuleBase.cs +++ b/Oqtane.Client/Modules/ModuleBase.cs @@ -104,13 +104,9 @@ namespace Oqtane.Modules public string ContentUrl(int fileid) { - string apiurl = PageState.Uri.Scheme + "://" + PageState.Alias.Name + "/"; - if (PageState.Alias.Path == "") - { - apiurl += "~/"; - } - apiurl += "api/File/Download/" + fileid.ToString(); - return apiurl; + string url = (PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path; + url += Constants.ContentUrl + fileid.ToString(); + return url; } // user feedback methods diff --git a/Oqtane.Client/Shared/SiteRouter.razor b/Oqtane.Client/Shared/SiteRouter.razor index 8e4e3e96..f6f84616 100644 --- a/Oqtane.Client/Shared/SiteRouter.razor +++ b/Oqtane.Client/Shared/SiteRouter.razor @@ -355,50 +355,57 @@ Dictionary paneindex = new Dictionary(); foreach (Module module in modules) { - string typename = module.ModuleDefinition.ControlTypeTemplate; - if (module.ModuleId == moduleid && control != "") + if (module.PageId == pageid || module.ModuleId == moduleid) { - // check if the module defines custom routes - if (module.ModuleDefinition.ControlTypeRoutes != "") + string typename = ""; + if (module.ModuleDefinition != null) { - foreach (string route in module.ModuleDefinition.ControlTypeRoutes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) + typename = module.ModuleDefinition.ControlTypeTemplate; + } + else + { + typename = Constants.ErrorModule; + } + if (module.ModuleId == moduleid && control != "") + { + // check if the module defines custom routes + if (module.ModuleDefinition.ControlTypeRoutes != "") { - if (route.StartsWith(control + "=")) + foreach (string route in module.ModuleDefinition.ControlTypeRoutes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { - typename = route.Replace(control + "=", ""); + if (route.StartsWith(control + "=")) + { + typename = route.Replace(control + "=", ""); + } + } + } + module.ModuleType = typename.Replace(Constants.ActionToken, control); + + // admin controls need to load additional metadata from the IModuleControl interface + if (moduleid == module.ModuleId) + { + typename = module.ModuleType; + // check for core module actions component + if (Constants.DefaultModuleActions.Contains(control)) + { + typename = Constants.DefaultModuleActionsTemplate.Replace(Constants.ActionToken, control); + } + Type moduletype = Type.GetType(typename); + if (moduletype != null) + { + var moduleobject = Activator.CreateInstance(moduletype); + module.SecurityAccessLevel = (SecurityAccessLevel)moduletype.GetProperty("SecurityAccessLevel").GetValue(moduleobject, null); + module.ControlTitle = (string)moduletype.GetProperty("Title").GetValue(moduleobject); + module.Actions = (string)moduletype.GetProperty("Actions").GetValue(moduleobject); + module.UseAdminContainer = (bool)moduletype.GetProperty("UseAdminContainer").GetValue(moduleobject); } } } - module.ModuleType = typename.Replace(Constants.ActionToken, control); - - // admin controls need to load additional metadata from the IModuleControl interface - if (moduleid == module.ModuleId) + else { - typename = module.ModuleType; - // check for core module actions component - if (Constants.DefaultModuleActions.Contains(control)) - { - typename = Constants.DefaultModuleActionsTemplate.Replace(Constants.ActionToken, control); - } - Type moduletype = Type.GetType(typename); - if (moduletype != null) - { - var moduleobject = Activator.CreateInstance(moduletype); - module.SecurityAccessLevel = (SecurityAccessLevel)moduletype.GetProperty("SecurityAccessLevel").GetValue(moduleobject, null); - module.ControlTitle = (string)moduletype.GetProperty("Title").GetValue(moduleobject); - module.Actions = (string)moduletype.GetProperty("Actions").GetValue(moduleobject); - module.UseAdminContainer = (bool)moduletype.GetProperty("UseAdminContainer").GetValue(moduleobject); - } + module.ModuleType = typename.Replace(Constants.ActionToken, Constants.DefaultAction); } - } - else - { - module.ModuleType = typename.Replace(Constants.ActionToken, Constants.DefaultAction); - } - - if (module.PageId == pageid) - { // ensure module's pane exists in current page and if not, assign it to the Admin pane if (panes == null || !panes.ToLower().Contains(module.Pane.ToLower())) { @@ -415,20 +422,17 @@ paneindex.Add(module.Pane, 0); } module.PaneModuleIndex = paneindex[module.Pane]; - } - if (string.IsNullOrEmpty(module.ContainerType)) - { - module.ContainerType = defaultcontainertype; + if (string.IsNullOrEmpty(module.ContainerType)) + { + module.ContainerType = defaultcontainertype; + } } } - foreach (Module module in modules) + foreach (Module module in modules.Where(item => item.PageId == pageid)) { - if (module.PageId == pageid) - { - module.PaneModuleCount = paneindex[module.Pane] + 1; - } + module.PaneModuleCount = paneindex[module.Pane] + 1; } return modules; } diff --git a/Oqtane.Client/Themes/Controls/ControlPanel.razor b/Oqtane.Client/Themes/Controls/ControlPanel.razor index e433f30e..ef7f1a60 100644 --- a/Oqtane.Client/Themes/Controls/ControlPanel.razor +++ b/Oqtane.Client/Themes/Controls/ControlPanel.razor @@ -202,7 +202,7 @@ List ModuleDefinitions; List moduledefinitions; List pages = new List(); - string pageid = ""; + string pageid = "-"; string moduleid = "-"; List modules = new List(); Dictionary containers = new Dictionary(); @@ -285,7 +285,7 @@ { pageid = (string)e.Value; modules?.Clear(); - if (pageid != "") + if (pageid != "-") { foreach (Module module in PageState.Modules.Where(item => item.PageId == int.Parse(pageid) && !item.IsDeleted)) { @@ -301,55 +301,67 @@ private async Task AddModule() { - if (UserSecurity.IsAuthorized(PageState.User, "Edit", PageState.Page.Permissions) && !string.IsNullOrWhiteSpace(moduledefinitionname) && moduledefinitionname != "-") + if (UserSecurity.IsAuthorized(PageState.User, "Edit", PageState.Page.Permissions)) { - if (moduletype == "new") - { - Module module = new Module(); - module.SiteId = PageState.Site.SiteId; - module.PageId = PageState.Page.PageId; - module.ModuleDefinitionName = moduledefinitionname; - module.Permissions = PageState.Page.Permissions; - module = await ModuleService.AddModuleAsync(module); - moduleid = module.ModuleId.ToString(); - } - - PageModule pagemodule = new PageModule(); - pagemodule.PageId = string.IsNullOrEmpty(pageid) ? PageState.Page.PageId : int.Parse(pageid); - pagemodule.ModuleId = int.Parse(moduleid); - pagemodule.Title = title; - if (pagemodule.Title == "") + if ((moduletype == "new" && moduledefinitionname != "-") || (moduletype != "new" && moduleid != "-")) { if (moduletype == "new") { - pagemodule.Title = moduledefinitions.Where(item => item.ModuleDefinitionName == moduledefinitionname).FirstOrDefault().Name; + Module module = new Module(); + module.SiteId = PageState.Site.SiteId; + module.PageId = PageState.Page.PageId; + module.ModuleDefinitionName = moduledefinitionname; + module.Permissions = PageState.Page.Permissions; + module = await ModuleService.AddModuleAsync(module); + moduleid = module.ModuleId.ToString(); } - else + + PageModule pagemodule = new PageModule(); + pagemodule.PageId = (pageid != "-") ? PageState.Page.PageId : int.Parse(pageid); + pagemodule.ModuleId = int.Parse(moduleid); + pagemodule.Title = title; + if (pagemodule.Title == "") { - pagemodule.Title = modules.Where(item => item.ModuleId == int.Parse(moduleid)).FirstOrDefault().Title; + if (moduletype == "new") + { + pagemodule.Title = moduledefinitions.Where(item => item.ModuleDefinitionName == moduledefinitionname).FirstOrDefault().Name; + } + else + { + pagemodule.Title = modules.Where(item => item.ModuleId == int.Parse(moduleid)).FirstOrDefault().Title; + } } - } - pagemodule.Pane = pane; - pagemodule.Order = int.MaxValue; - pagemodule.ContainerType = containertype; + pagemodule.Pane = pane; + pagemodule.Order = int.MaxValue; + pagemodule.ContainerType = containertype; - if (pagemodule.ContainerType == PageState.Site.DefaultContainerType) + if (pagemodule.ContainerType == PageState.Site.DefaultContainerType) + { + pagemodule.ContainerType = ""; + } + + await PageModuleService.AddPageModuleAsync(pagemodule); + await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); + + message = "
Module Added To Page
"; + + moduledefinitionname = "-"; + pane = ""; + title = ""; + containertype = ""; + pageid = "-"; + moduleid = "-"; + + NavigationManager.NavigateTo(NavigateUrl(Reload.Page)); + } + else { - pagemodule.ContainerType = ""; + message = "
You Must Select A Module
"; } - - await PageModuleService.AddPageModuleAsync(pagemodule); - await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); - - message = "
Module Added To Page
"; - - moduledefinitionname = "-"; - pane = ""; - title = ""; - containertype = ""; - moduleid = "-"; - - NavigationManager.NavigateTo(NavigateUrl(Reload.Page)); + } + else + { + message = "
Not Authorized
"; } } diff --git a/Oqtane.Client/Themes/Controls/ModuleActions.razor b/Oqtane.Client/Themes/Controls/ModuleActions.razor index d410808f..5ca37b44 100644 --- a/Oqtane.Client/Themes/Controls/ModuleActions.razor +++ b/Oqtane.Client/Themes/Controls/ModuleActions.razor @@ -31,7 +31,7 @@ { actions = new List(); actions.Add(new ActionViewModel { Action = "settings", Name = "Manage Settings" }); - if (ModuleState.ModuleDefinition.ServerAssemblyName != "") + if (ModuleState.ModuleDefinition != null && ModuleState.ModuleDefinition.ServerAssemblyName != "") { actions.Add(new ActionViewModel { Action = "import", Name = "Import Content" }); actions.Add(new ActionViewModel { Action = "export", Name = "Export Content" }); diff --git a/Oqtane.Client/Themes/ThemeControlBase.cs b/Oqtane.Client/Themes/ThemeControlBase.cs index 6e3777dc..baed86cf 100644 --- a/Oqtane.Client/Themes/ThemeControlBase.cs +++ b/Oqtane.Client/Themes/ThemeControlBase.cs @@ -55,13 +55,9 @@ namespace Oqtane.Themes public string ContentUrl(int fileid) { - string apiurl = PageState.Uri.Scheme + "://" + PageState.Alias.Name + "/"; - if (PageState.Alias.Path == "") - { - apiurl += "~/"; - } - apiurl += "api/File/Download/" + fileid.ToString(); - return apiurl; + string url = (PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path; + url += Constants.ContentUrl + fileid.ToString(); + return url; } } } diff --git a/Oqtane.Shared/Shared/Constants.cs b/Oqtane.Shared/Shared/Constants.cs index fe4a12dd..12305ca7 100644 --- a/Oqtane.Shared/Shared/Constants.cs +++ b/Oqtane.Shared/Shared/Constants.cs @@ -23,8 +23,11 @@ public const string AdminDashboardModule = "Oqtane.Modules.Admin.Dashboard, Oqtane.Client"; public const string PageManagementModule = "Oqtane.Modules.Admin.Pages, Oqtane.Client"; + public const string ErrorModule = "Oqtane.Modules.Admin.Error.{Action}, Oqtane.Client"; public const string ModuleMessageComponent = "Oqtane.Modules.Controls.ModuleMessage, Oqtane.Client"; + public const string ContentUrl = "/api/file/download/"; + public const string HostUser = "host"; public const string AllUsersRole = "All Users";