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 IPageService PageService
<TabControl>
<TabPanel Text="Pages">
@if (pages.Count == 0)
{
<br/>
<p>No Deleted Pages</p>
}
else
{
<Pager Items="@pages">
<Header>
<th>Name</th>
<th>Deleted By</th>
<th>Deleted On</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</Header>
<Row>
<td>@context.Name</td>
<td>@context.DeletedBy</td>
<td>@context.DeletedOn</td>
<td><button @onclick="@(() => RestorePage(context))" class="btn btn-info" title="Restore">Restore</button></td>
<td><button @onclick="@(() => DeletePage(context.PageId))" class="btn btn-danger">Delete</button></td>
</Row>
</Pager>
}
</TabPanel>
<TabPanel Text="Modules">
@if (pageModules.Count == 0)
{
<br/>
<p>No Deleted Modules</p>
}
else
{
<Pager Items="@pageModules">
<Header>
<th>Page</th>
<th>Module</th>
<th>Deleted By</th>
<th>Deleted On</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</Header>
<Row>
<td>@PageState.Pages.Find(item => item.PageId == context.PageId).Name</td>
<td>@context.Title</td>
<td>@context.DeletedBy</td>
<td>@context.DeletedOn</td>
<td><button @onclick="@(() => RestorePageModule(context))" class="btn btn-info" title="Restore">Restore</button></td>
<td><button @onclick="@(() => DeletePageModule(context.PageModuleId, context.ModuleId))" class="btn btn-danger">Delete</button></td>
</Row>
</Pager>
}
</TabPanel>
</TabControl>
<div class="container-fluid">
<div class="form-group">
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#Pages" role="tab">
Pages
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#Modules" role="tab">
Modules
</a>
</li>
</ul>
<div class="tab-content">
<div id="Pages" class="tab-pane fade show active" role="tabpanel">
@if (pages == null)
{
<br />
<p>No Deleted Pages</p>
}
else
{
<Pager Items="@pages">
<Header>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>Name</th>
<th>Deleted By</th>
<th>Deleted On</th>
</Header>
<Row>
<td><button @onclick="@(() => RestorePage(context))" class="btn btn-info" title="Restore">Restore</button></td>
<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>
<td>@context.Name</td>
<td>@context.DeletedBy</td>
<td>@context.DeletedOn</td>
</Row>
</Pager>
}
</div>
<div id="Modules" class="tab-pane fade" role="tabpanel">
@if (modules == null)
{
<br />
<p>No Deleted Modules</p>
}
else
{
<Pager Items="@modules">
<Header>
<th>&nbsp;</th>
<th>&nbsp;</th>
<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 {
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } }
List<Page> pages { get; set; }
List<PageModule> pageModules { get; set; }
List<Page> pages;
List<Module> modules;
protected override async Task OnInitializedAsync()
{
try
{
pages = new List<Page>();
pageModules = new List<PageModule>();
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);
}
}
}

View File

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

View File

@ -13,7 +13,7 @@
<br /><br />
@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)

View File

@ -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

View File

@ -355,50 +355,57 @@
Dictionary<string, int> paneindex = new Dictionary<string, int>();
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;
}

View File

@ -202,7 +202,7 @@
List<ModuleDefinition> ModuleDefinitions;
List<ModuleDefinition> moduledefinitions;
List<Page> pages = new List<Page>();
string pageid = "";
string pageid = "-";
string moduleid = "-";
List<Module> modules = new List<Module>();
Dictionary<string, string> containers = new Dictionary<string, string>();
@ -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 = "<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);
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 = "";
moduleid = "-";
NavigationManager.NavigateTo(NavigateUrl(Reload.Page));
}
else
{
message = "<br /><div class=\"alert alert-error\" role=\"alert\">Not Authorized</div>";
}
}

View File

@ -31,7 +31,7 @@
{
actions = new List<ActionViewModel>();
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" });

View File

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

View File

@ -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";