Merge pull request #150 from sbwalker/master

logging improvements
This commit is contained in:
Shaun Walker 2019-10-22 18:17:37 -04:00 committed by GitHub
commit 4c0cf7e8dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 43 deletions

View File

@ -37,11 +37,11 @@ namespace Oqtane.Controllers
// GET api/<controller>/filename // GET api/<controller>/filename
[HttpGet("{filename}")] [HttpGet("{filename}")]
public IActionResult Get(string filename) public IActionResult Get(string assemblyname)
{ {
string binfolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string binfolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
byte[] file = System.IO.File.ReadAllBytes(Path.Combine(binfolder, filename)); byte[] file = System.IO.File.ReadAllBytes(Path.Combine(binfolder, assemblyname));
return File(file, "application/octet-stream", filename); return File(file, "application/octet-stream", assemblyname);
} }
// PUT api/<controller>/5 // PUT api/<controller>/5

View File

@ -51,7 +51,7 @@ namespace Oqtane.Infrastructure
log.Level = Enum.GetName(typeof(LogLevel), Level); log.Level = Enum.GetName(typeof(LogLevel), Level);
if (Exception != null) if (Exception != null)
{ {
log.Exception = JsonSerializer.Serialize(Exception); log.Exception = JsonSerializer.Serialize(Exception.ToString());
} }
log.Message = Message; log.Message = Message;
log.MessageTemplate = ""; log.MessageTemplate = "";

View File

@ -5,6 +5,7 @@ using Oqtane.Modules.HtmlText.Repository;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Shared; using Oqtane.Shared;
using System;
namespace Oqtane.Modules.HtmlText.Controllers namespace Oqtane.Modules.HtmlText.Controllers
{ {
@ -30,12 +31,20 @@ namespace Oqtane.Modules.HtmlText.Controllers
[Authorize(Policy = "ViewModule")] [Authorize(Policy = "ViewModule")]
public HtmlTextInfo Get(int id) public HtmlTextInfo Get(int id)
{ {
HtmlTextInfo HtmlText = null; try
if (EntityId == id)
{ {
HtmlText = htmltext.GetHtmlText(id); HtmlTextInfo HtmlText = null;
if (EntityId == id)
{
HtmlText = htmltext.GetHtmlText(id);
}
return HtmlText;
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Get Error {Error}", ex.Message);
throw;
} }
return HtmlText;
} }
// POST api/<controller> // POST api/<controller>
@ -43,12 +52,20 @@ namespace Oqtane.Modules.HtmlText.Controllers
[Authorize(Policy = "EditModule")] [Authorize(Policy = "EditModule")]
public HtmlTextInfo Post([FromBody] HtmlTextInfo HtmlText) public HtmlTextInfo Post([FromBody] HtmlTextInfo HtmlText)
{ {
if (ModelState.IsValid && HtmlText.ModuleId == EntityId) try
{ {
HtmlText = htmltext.AddHtmlText(HtmlText); if (ModelState.IsValid && HtmlText.ModuleId == EntityId)
logger.AddLog(this.GetType().FullName, LogLevel.Information, "Html/Text Added {HtmlText}", HtmlText); {
HtmlText = htmltext.AddHtmlText(HtmlText);
logger.AddLog(this.GetType().FullName, LogLevel.Information, "Html/Text Added {HtmlText}", HtmlText);
}
return HtmlText;
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Post Error {Error}", ex.Message);
throw;
} }
return HtmlText;
} }
// PUT api/<controller>/5 // PUT api/<controller>/5
@ -56,12 +73,20 @@ namespace Oqtane.Modules.HtmlText.Controllers
[Authorize(Policy = "EditModule")] [Authorize(Policy = "EditModule")]
public HtmlTextInfo Put(int id, [FromBody] HtmlTextInfo HtmlText) public HtmlTextInfo Put(int id, [FromBody] HtmlTextInfo HtmlText)
{ {
if (ModelState.IsValid && HtmlText.ModuleId == EntityId) try
{ {
HtmlText = htmltext.UpdateHtmlText(HtmlText); if (ModelState.IsValid && HtmlText.ModuleId == EntityId)
logger.AddLog(this.GetType().FullName, LogLevel.Information, "Html/Text Updated {HtmlText}", HtmlText); {
HtmlText = htmltext.UpdateHtmlText(HtmlText);
logger.AddLog(this.GetType().FullName, LogLevel.Information, "Html/Text Updated {HtmlText}", HtmlText);
}
return HtmlText;
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Put Error {Error}", ex.Message);
throw;
} }
return HtmlText;
} }
// DELETE api/<controller>/5 // DELETE api/<controller>/5
@ -69,10 +94,18 @@ namespace Oqtane.Modules.HtmlText.Controllers
[Authorize(Policy = "EditModule")] [Authorize(Policy = "EditModule")]
public void Delete(int id) public void Delete(int id)
{ {
if (id == EntityId) try
{ {
htmltext.DeleteHtmlText(id); if (id == EntityId)
logger.AddLog(this.GetType().FullName, LogLevel.Information, "Html/Text Deleted {HtmlTextId}", id); {
htmltext.DeleteHtmlText(id);
logger.AddLog(this.GetType().FullName, LogLevel.Information, "Html/Text Deleted {HtmlTextId}", id);
}
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Delete Error {Error}", ex.Message);
throw;
} }
} }
} }

View File

@ -10,39 +10,49 @@ namespace Oqtane.Repository
{ {
private MasterDBContext db; private MasterDBContext db;
private readonly string aliasname; private readonly string aliasname;
private readonly IAliasRepository _aliasrepository; private readonly IAliasRepository Aliases;
private readonly ITenantRepository _tenantrepository; private readonly ITenantRepository Tenants;
public TenantResolver(MasterDBContext context, IHttpContextAccessor accessor, IAliasRepository aliasrepository, ITenantRepository tenantrepository) public TenantResolver(MasterDBContext context, IHttpContextAccessor accessor, IAliasRepository Aliases, ITenantRepository Tenants)
{ {
db = context; db = context;
_aliasrepository = aliasrepository; this.Aliases = Aliases;
_tenantrepository = tenantrepository; this.Tenants = Tenants;
aliasname = "";
// get alias based on request context // get alias based on request context
aliasname = accessor.HttpContext.Request.Host.Value; if (accessor.HttpContext != null)
string path = accessor.HttpContext.Request.Path.Value;
string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
{ {
aliasname += "/" + segments[0]; aliasname = accessor.HttpContext.Request.Host.Value;
} string path = accessor.HttpContext.Request.Path.Value;
if (aliasname.EndsWith("/")) string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
{ if (segments.Length > 1 && segments[1] == "api" && segments[0] != "~")
aliasname = aliasname.Substring(0, aliasname.Length - 1); {
aliasname += "/" + segments[0];
}
if (aliasname.EndsWith("/"))
{
aliasname = aliasname.Substring(0, aliasname.Length - 1);
}
} }
} }
public Alias GetAlias() public Alias GetAlias()
{ {
IEnumerable<Alias> aliases = _aliasrepository.GetAliases(); // cached IEnumerable<Alias> aliases = Aliases.GetAliases(); // cached
return aliases.Where(item => item.Name == aliasname).FirstOrDefault(); return aliases.Where(item => item.Name == aliasname).FirstOrDefault();
} }
public Tenant GetTenant() public Tenant GetTenant()
{ {
IEnumerable<Tenant> tenants = _tenantrepository.GetTenants(); // cached Tenant tenant = null;
return tenants.Where(item => item.TenantId == GetAlias().TenantId).FirstOrDefault(); Alias alias = GetAlias();
if (alias != null)
{
IEnumerable<Tenant> tenants = Tenants.GetTenants(); // cached
tenant = tenants.Where(item => item.TenantId == alias.TenantId).FirstOrDefault();
}
return tenant;
} }
} }
} }

View File

@ -1,7 +1,8 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Oqtane.Infrastructure;
using Oqtane.Shared;
namespace Oqtane.Security namespace Oqtane.Security
{ {
@ -9,11 +10,13 @@ namespace Oqtane.Security
{ {
private readonly IHttpContextAccessor HttpContextAccessor; private readonly IHttpContextAccessor HttpContextAccessor;
private readonly IUserPermissions UserPermissions; private readonly IUserPermissions UserPermissions;
private readonly ILogManager logger;
public PermissionHandler(IHttpContextAccessor HttpContextAccessor, IUserPermissions UserPermissions) public PermissionHandler(IHttpContextAccessor HttpContextAccessor, IUserPermissions UserPermissions, ILogManager logger)
{ {
this.HttpContextAccessor = HttpContextAccessor; this.HttpContextAccessor = HttpContextAccessor;
this.UserPermissions = UserPermissions; this.UserPermissions = UserPermissions;
this.logger = logger;
} }
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement) protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
@ -27,6 +30,10 @@ namespace Oqtane.Security
{ {
context.Succeed(requirement); context.Succeed(requirement);
} }
else
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, "User {User} Does Not Have {PermissionName} Permission For {EntityName}:{EntityId}", context.User, requirement.PermissionName, requirement.EntityName, EntityId);
}
} }
return Task.CompletedTask; return Task.CompletedTask;
} }

View File

@ -152,10 +152,9 @@ namespace Oqtane.Server
services.AddSingleton<IConfigurationRoot>(Configuration); services.AddSingleton<IConfigurationRoot>(Configuration);
services.AddSingleton<IInstallationManager, InstallationManager>(); services.AddSingleton<IInstallationManager, InstallationManager>();
// install any modules or themes //ServiceProvider sp = services.BuildServiceProvider();
ServiceProvider sp = services.BuildServiceProvider(); //var InstallationManager = sp.GetRequiredService<IInstallationManager>();
var InstallationManager = sp.GetRequiredService<IInstallationManager>(); //InstallationManager.InstallPackages("Modules,Themes");
InstallationManager.InstallPackages("Modules,Themes");
// register transient scoped core services // register transient scoped core services
services.AddTransient<IModuleDefinitionRepository, ModuleDefinitionRepository>(); services.AddTransient<IModuleDefinitionRepository, ModuleDefinitionRepository>();
@ -239,7 +238,7 @@ namespace Oqtane.Server
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IInstallationManager InstallationManager)
{ {
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
@ -251,6 +250,9 @@ namespace Oqtane.Server
app.UseHsts(); app.UseHsts();
} }
// install any modules or themes
InstallationManager.InstallPackages("Modules,Themes");
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
@ -443,6 +445,9 @@ namespace Oqtane.Server
app.UseBlazorDebugging(); app.UseBlazorDebugging();
} }
// install any modules or themes
InstallationManager.InstallPackages("Modules,Themes");
app.UseClientSideBlazorFiles<Client.Startup>(); app.UseClientSideBlazorFiles<Client.Startup>();
app.UseStaticFiles(); app.UseStaticFiles();