module load error handler, router optimizaton, relative paths, fixed add existing module control panel issue

This commit is contained in:
Shaun Walker
2020-03-04 13:22:58 -05:00
parent 061043bd15
commit 4c2007439d
10 changed files with 242 additions and 199 deletions

View File

@ -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);
}
}

View File

@ -5,76 +5,92 @@
@inject IModuleService ModuleService @inject IModuleService ModuleService
@inject IPageService PageService @inject IPageService PageService
<TabControl> <div class="container-fluid">
<TabPanel Text="Pages"> <div class="form-group">
@if (pages.Count == 0)
{ <ul class="nav nav-tabs" role="tablist">
<br/> <li class="nav-item">
<p>No Deleted Pages</p> <a class="nav-link active" data-toggle="tab" href="#Pages" role="tab">
} Pages
else </a>
{ </li>
<Pager Items="@pages"> <li class="nav-item">
<Header> <a class="nav-link" data-toggle="tab" href="#Modules" role="tab">
<th>Name</th> Modules
<th>Deleted By</th> </a>
<th>Deleted On</th> </li>
<th>&nbsp;</th> </ul>
<th>&nbsp;</th>
</Header> <div class="tab-content">
<Row> <div id="Pages" class="tab-pane fade show active" role="tabpanel">
<td>@context.Name</td> @if (pages == null)
<td>@context.DeletedBy</td> {
<td>@context.DeletedOn</td> <br />
<td><button @onclick="@(() => RestorePage(context))" class="btn btn-info" title="Restore">Restore</button></td> <p>No Deleted Pages</p>
<td><button @onclick="@(() => DeletePage(context.PageId))" class="btn btn-danger">Delete</button></td> }
</Row> else
</Pager> {
} <Pager Items="@pages">
</TabPanel> <Header>
<TabPanel Text="Modules"> <th>&nbsp;</th>
@if (pageModules.Count == 0) <th>&nbsp;</th>
{ <th>Name</th>
<br/> <th>Deleted By</th>
<p>No Deleted Modules</p> <th>Deleted On</th>
} </Header>
else <Row>
{ <td><button @onclick="@(() => RestorePage(context))" class="btn btn-info" title="Restore">Restore</button></td>
<Pager Items="@pageModules"> <td><ActionDialog Header="Delete Page" Message="@("Are You Sure You Wish To Permanently Delete The " + context.Name + " Page?")" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeletePage(context))" /></td>
<Header> <td>@context.Name</td>
<th>Page</th> <td>@context.DeletedBy</td>
<th>Module</th> <td>@context.DeletedOn</td>
<th>Deleted By</th> </Row>
<th>Deleted On</th> </Pager>
<th>&nbsp;</th> }
<th>&nbsp;</th> </div>
</Header> <div id="Modules" class="tab-pane fade" role="tabpanel">
<Row> @if (modules == null)
<td>@PageState.Pages.Find(item => item.PageId == context.PageId).Name</td> {
<td>@context.Title</td> <br />
<td>@context.DeletedBy</td> <p>No Deleted Modules</p>
<td>@context.DeletedOn</td> }
<td><button @onclick="@(() => RestorePageModule(context))" class="btn btn-info" title="Restore">Restore</button></td> else
<td><button @onclick="@(() => DeletePageModule(context.PageModuleId, context.ModuleId))" class="btn btn-danger">Delete</button></td> {
</Row> <Pager Items="@modules">
</Pager> <Header>
} <th>&nbsp;</th>
</TabPanel> <th>&nbsp;</th>
</TabControl> <th>Page</th>
<th>Module</th>
<th>Deleted By</th>
<th>Deleted On</th>
</Header>
<Row>
<td><button @onclick="@(() => RestoreModule(context))" class="btn btn-info" title="Restore">Restore</button></td>
<td><ActionDialog Header="Delete Module" Message="@("Are You Sure You Wish To Permanently Delete The " + context.Title + " Module?")" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteModule(context))" /></td>
<td>@PageState.Pages.Find(item => item.PageId == context.PageId).Name</td>
<td>@context.Title</td>
<td>@context.DeletedBy</td>
<td>@context.DeletedOn</td>
</Row>
</Pager>
}
</div>
</div>
</div>
</div>
@code { @code {
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } } public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } }
List<Page> pages { get; set; } List<Page> pages;
List<PageModule> pageModules { get; set; } List<Module> modules;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
try try
{ {
pages = new List<Page>(); await Load();
pageModules = new List<PageModule>();
await LoadEntities();
} }
catch (Exception ex) 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() modules = await ModuleService.GetModulesAsync(PageState.Site.SiteId);
{ modules = modules.Where(item => item.IsDeleted).ToList();
pageModules.Clear();
foreach (var module in PageState.Modules.Where(item => item.IsDeleted))
{
var pageModule = await PageModuleService.GetPageModuleAsync(module.PageModuleId);
pageModules.Add(pageModule);
}
} }
private async Task RestorePage(Page Page) private async Task RestorePage(Page Page)
@ -105,6 +115,8 @@
Page.IsDeleted = false; Page.IsDeleted = false;
await PageService.UpdatePageAsync(Page); await PageService.UpdatePageAsync(Page);
await logger.LogInformation("Page Restored {Page}", Page); await logger.LogInformation("Page Restored {Page}", Page);
await Load();
StateHasChanged();
NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); NavigationManager.NavigateTo(NavigateUrl(Reload.Site));
} }
catch (Exception ex) catch (Exception ex)
@ -114,61 +126,60 @@
} }
} }
private async Task DeletePage(int PageId) private async Task DeletePage(Page Page)
{ {
try try
{ {
var deletedPageModules = PageState.Modules.Where(item => item.PageId == PageId); await PageService.DeletePageAsync(Page.PageId);
await PageService.DeletePageAsync(PageId); await logger.LogInformation("Page Permanently Deleted {Page}", Page);
foreach (var module in deletedPageModules) await Load();
{ StateHasChanged();
await ModuleService.DeleteModuleAsync(module.ModuleId);
}
await logger.LogInformation("Page Permanently Deleted {PageId}", PageId);
NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); NavigationManager.NavigateTo(NavigateUrl(Reload.Site));
} }
catch (Exception ex) 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); AddModuleMessage(ex.Message, MessageType.Error);
} }
} }
private async Task RestorePageModule(PageModule PageModule) private async Task RestoreModule(Module Module)
{ {
try try
{ {
PageModule.IsDeleted = false; PageModule pagemodule = await PageModuleService.GetPageModuleAsync(Module.PageModuleId);
await PageModuleService.UpdatePageModuleAsync(PageModule); pagemodule.IsDeleted = false;
await LoadEntities(); await PageModuleService.UpdatePageModuleAsync(pagemodule);
await logger.LogInformation("Page Module Restored {PageModule}", PageModule); await logger.LogInformation("Module Restored {Module}", Module);
NavigationManager.NavigateTo(NavigateUrl(Reload.Site)); await Load();
StateHasChanged();
} }
catch (Exception ex) catch (Exception ex)
{ {
await logger.LogError(ex, "Error Restoring Deleted Page Module {PageModule} {Error}", PageModule, ex.Message); await logger.LogError(ex, "Error Restoring Deleted Module {Module} {Error}", Module, ex.Message);
AddModuleMessage("Error Restoring Deleted Page Module", MessageType.Error); AddModuleMessage("Error Restoring Deleted Module", MessageType.Error);
} }
} }
private async Task DeletePageModule(int PageModuleId, int ModuleId) private async Task DeleteModule(Module Module)
{ {
try try
{ {
await PageModuleService.DeletePageModuleAsync(PageModuleId); await PageModuleService.DeletePageModuleAsync(Module.PageModuleId);
if (PageState.Modules.Count(item => item.ModuleId == ModuleId) == 1) // 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 logger.LogInformation("Module Permanently Deleted {Module}", Module);
await LoadEntities(); await Load();
await logger.LogInformation("Page Module Permanently Deleted {PageModuleId}", PageModuleId); StateHasChanged();
NavigationManager.NavigateTo(NavigateUrl(Reload.Site));
} }
catch (Exception ex) catch (Exception ex)
{ {
await logger.LogError(ex, "Error Permanently Deleting Page Module {PageModuleId} {Error}", PageModuleId, ex.Message); await logger.LogError(ex, "Error Permanently Deleting Module {Module} {Error}", Module, ex.Message);
AddModuleMessage("Error Permanently Deleting Page Module", MessageType.Error); AddModuleMessage("Error Permanently Deleting Module", MessageType.Error);
} }
} }
} }

View File

@ -133,6 +133,8 @@ else
content = await this.RichTextEditorHtml.GetHTML(); content = await this.RichTextEditorHtml.GetHTML();
} }
content = content.Replace(((PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path) + Constants.ContentUrl, Constants.ContentUrl);
try try
{ {
HtmlTextService htmltextservice = new HtmlTextService(http, sitestate, NavigationManager); HtmlTextService htmltextservice = new HtmlTextService(http, sitestate, NavigationManager);

View File

@ -13,7 +13,7 @@
<br /><br /> <br /><br />
@code { @code {
string content; string content = "";
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
{ {
@ -24,6 +24,7 @@
if (htmltext != null) if (htmltext != null)
{ {
content = htmltext.Content; content = htmltext.Content;
content = content.Replace(Constants.ContentUrl, ((PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path) + Constants.ContentUrl);
} }
} }
catch (Exception ex) catch (Exception ex)

View File

@ -104,13 +104,9 @@ namespace Oqtane.Modules
public string ContentUrl(int fileid) public string ContentUrl(int fileid)
{ {
string apiurl = PageState.Uri.Scheme + "://" + PageState.Alias.Name + "/"; string url = (PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path;
if (PageState.Alias.Path == "") url += Constants.ContentUrl + fileid.ToString();
{ return url;
apiurl += "~/";
}
apiurl += "api/File/Download/" + fileid.ToString();
return apiurl;
} }
// user feedback methods // user feedback methods

View File

@ -355,50 +355,57 @@
Dictionary<string, int> paneindex = new Dictionary<string, int>(); Dictionary<string, int> paneindex = new Dictionary<string, int>();
foreach (Module module in modules) foreach (Module module in modules)
{ {
string typename = module.ModuleDefinition.ControlTypeTemplate; if (module.PageId == pageid || module.ModuleId == moduleid)
if (module.ModuleId == moduleid && control != "")
{ {
// check if the module defines custom routes string typename = "";
if (module.ModuleDefinition.ControlTypeRoutes != "") 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); else
// admin controls need to load additional metadata from the IModuleControl interface
if (moduleid == module.ModuleId)
{ {
typename = module.ModuleType; module.ModuleType = typename.Replace(Constants.ActionToken, Constants.DefaultAction);
// 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);
}
} }
}
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 // 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())) if (panes == null || !panes.ToLower().Contains(module.Pane.ToLower()))
{ {
@ -415,20 +422,17 @@
paneindex.Add(module.Pane, 0); paneindex.Add(module.Pane, 0);
} }
module.PaneModuleIndex = paneindex[module.Pane]; module.PaneModuleIndex = paneindex[module.Pane];
}
if (string.IsNullOrEmpty(module.ContainerType)) if (string.IsNullOrEmpty(module.ContainerType))
{ {
module.ContainerType = defaultcontainertype; 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; return modules;
} }

View File

@ -202,7 +202,7 @@
List<ModuleDefinition> ModuleDefinitions; List<ModuleDefinition> ModuleDefinitions;
List<ModuleDefinition> moduledefinitions; List<ModuleDefinition> moduledefinitions;
List<Page> pages = new List<Page>(); List<Page> pages = new List<Page>();
string pageid = ""; string pageid = "-";
string moduleid = "-"; string moduleid = "-";
List<Module> modules = new List<Module>(); List<Module> modules = new List<Module>();
Dictionary<string, string> containers = new Dictionary<string, string>(); Dictionary<string, string> containers = new Dictionary<string, string>();
@ -285,7 +285,7 @@
{ {
pageid = (string)e.Value; pageid = (string)e.Value;
modules?.Clear(); modules?.Clear();
if (pageid != "") if (pageid != "-")
{ {
foreach (Module module in PageState.Modules.Where(item => item.PageId == int.Parse(pageid) && !item.IsDeleted)) foreach (Module module in PageState.Modules.Where(item => item.PageId == int.Parse(pageid) && !item.IsDeleted))
{ {
@ -301,55 +301,67 @@
private async Task AddModule() 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") if ((moduletype == "new" && moduledefinitionname != "-") || (moduletype != "new" && moduleid != "-"))
{
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") 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.Pane = pane; pagemodule.Order = int.MaxValue;
pagemodule.Order = int.MaxValue; pagemodule.ContainerType = containertype;
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 = "<br /><div class=\"alert alert-success\" role=\"alert\">Module Added To Page</div>";
moduledefinitionname = "-";
pane = "";
title = "";
containertype = "";
pageid = "-";
moduleid = "-";
NavigationManager.NavigateTo(NavigateUrl(Reload.Page));
}
else
{ {
pagemodule.ContainerType = ""; message = "<br /><div class=\"alert alert-warning\" role=\"alert\">You Must Select A Module</div>";
} }
}
await PageModuleService.AddPageModuleAsync(pagemodule); else
await PageModuleService.UpdatePageModuleOrderAsync(pagemodule.PageId, pagemodule.Pane); {
message = "<br /><div class=\"alert alert-error\" role=\"alert\">Not Authorized</div>";
message = "<br /><div class=\"alert alert-success\" role=\"alert\">Module Added To Page</div>";
moduledefinitionname = "-";
pane = "";
title = "";
containertype = "";
moduleid = "-";
NavigationManager.NavigateTo(NavigateUrl(Reload.Page));
} }
} }

View File

@ -31,7 +31,7 @@
{ {
actions = new List<ActionViewModel>(); actions = new List<ActionViewModel>();
actions.Add(new ActionViewModel { Action = "settings", Name = "Manage Settings" }); 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 = "import", Name = "Import Content" });
actions.Add(new ActionViewModel { Action = "export", Name = "Export Content" }); actions.Add(new ActionViewModel { Action = "export", Name = "Export Content" });

View File

@ -55,13 +55,9 @@ namespace Oqtane.Themes
public string ContentUrl(int fileid) public string ContentUrl(int fileid)
{ {
string apiurl = PageState.Uri.Scheme + "://" + PageState.Alias.Name + "/"; string url = (PageState.Alias.Path == "") ? "/~" : PageState.Alias.Path;
if (PageState.Alias.Path == "") url += Constants.ContentUrl + fileid.ToString();
{ return url;
apiurl += "~/";
}
apiurl += "api/File/Download/" + fileid.ToString();
return apiurl;
} }
} }
} }

View File

@ -23,8 +23,11 @@
public const string AdminDashboardModule = "Oqtane.Modules.Admin.Dashboard, Oqtane.Client"; public const string AdminDashboardModule = "Oqtane.Modules.Admin.Dashboard, Oqtane.Client";
public const string PageManagementModule = "Oqtane.Modules.Admin.Pages, 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 ModuleMessageComponent = "Oqtane.Modules.Controls.ModuleMessage, Oqtane.Client";
public const string ContentUrl = "/api/file/download/";
public const string HostUser = "host"; public const string HostUser = "host";
public const string AllUsersRole = "All Users"; public const string AllUsersRole = "All Users";