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
[HttpGet("{filename}")]
public IActionResult Get(string filename)
public IActionResult Get(string assemblyname)
{
string binfolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
byte[] file = System.IO.File.ReadAllBytes(Path.Combine(binfolder, filename));
return File(file, "application/octet-stream", filename);
byte[] file = System.IO.File.ReadAllBytes(Path.Combine(binfolder, assemblyname));
return File(file, "application/octet-stream", assemblyname);
}
// PUT api/<controller>/5

View File

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

View File

@ -5,6 +5,7 @@ using Oqtane.Modules.HtmlText.Repository;
using Microsoft.AspNetCore.Http;
using Oqtane.Infrastructure;
using Oqtane.Shared;
using System;
namespace Oqtane.Modules.HtmlText.Controllers
{
@ -29,6 +30,8 @@ namespace Oqtane.Modules.HtmlText.Controllers
[HttpGet("{id}")]
[Authorize(Policy = "ViewModule")]
public HtmlTextInfo Get(int id)
{
try
{
HtmlTextInfo HtmlText = null;
if (EntityId == id)
@ -37,11 +40,19 @@ namespace Oqtane.Modules.HtmlText.Controllers
}
return HtmlText;
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Get Error {Error}", ex.Message);
throw;
}
}
// POST api/<controller>
[HttpPost]
[Authorize(Policy = "EditModule")]
public HtmlTextInfo Post([FromBody] HtmlTextInfo HtmlText)
{
try
{
if (ModelState.IsValid && HtmlText.ModuleId == EntityId)
{
@ -50,11 +61,19 @@ namespace Oqtane.Modules.HtmlText.Controllers
}
return HtmlText;
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Post Error {Error}", ex.Message);
throw;
}
}
// PUT api/<controller>/5
[HttpPut("{id}")]
[Authorize(Policy = "EditModule")]
public HtmlTextInfo Put(int id, [FromBody] HtmlTextInfo HtmlText)
{
try
{
if (ModelState.IsValid && HtmlText.ModuleId == EntityId)
{
@ -63,11 +82,19 @@ namespace Oqtane.Modules.HtmlText.Controllers
}
return HtmlText;
}
catch (Exception ex)
{
logger.AddLog(this.GetType().FullName, LogLevel.Error, ex, "Put Error {Error}", ex.Message);
throw;
}
}
// DELETE api/<controller>/5
[HttpDelete("{id}")]
[Authorize(Policy = "EditModule")]
public void Delete(int id)
{
try
{
if (id == EntityId)
{
@ -75,5 +102,11 @@ namespace Oqtane.Modules.HtmlText.Controllers
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,16 +10,19 @@ namespace Oqtane.Repository
{
private MasterDBContext db;
private readonly string aliasname;
private readonly IAliasRepository _aliasrepository;
private readonly ITenantRepository _tenantrepository;
private readonly IAliasRepository Aliases;
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;
_aliasrepository = aliasrepository;
_tenantrepository = tenantrepository;
this.Aliases = Aliases;
this.Tenants = Tenants;
aliasname = "";
// get alias based on request context
if (accessor.HttpContext != null)
{
aliasname = accessor.HttpContext.Request.Host.Value;
string path = accessor.HttpContext.Request.Path.Value;
string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
@ -32,17 +35,24 @@ namespace Oqtane.Repository
aliasname = aliasname.Substring(0, aliasname.Length - 1);
}
}
}
public Alias GetAlias()
{
IEnumerable<Alias> aliases = _aliasrepository.GetAliases(); // cached
IEnumerable<Alias> aliases = Aliases.GetAliases(); // cached
return aliases.Where(item => item.Name == aliasname).FirstOrDefault();
}
public Tenant GetTenant()
{
IEnumerable<Tenant> tenants = _tenantrepository.GetTenants(); // cached
return tenants.Where(item => item.TenantId == GetAlias().TenantId).FirstOrDefault();
Tenant tenant = null;
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 Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Oqtane.Infrastructure;
using Oqtane.Shared;
namespace Oqtane.Security
{
@ -9,11 +10,13 @@ namespace Oqtane.Security
{
private readonly IHttpContextAccessor HttpContextAccessor;
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.UserPermissions = UserPermissions;
this.logger = logger;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
@ -27,6 +30,10 @@ namespace Oqtane.Security
{
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;
}

View File

@ -152,10 +152,9 @@ namespace Oqtane.Server
services.AddSingleton<IConfigurationRoot>(Configuration);
services.AddSingleton<IInstallationManager, InstallationManager>();
// install any modules or themes
ServiceProvider sp = services.BuildServiceProvider();
var InstallationManager = sp.GetRequiredService<IInstallationManager>();
InstallationManager.InstallPackages("Modules,Themes");
//ServiceProvider sp = services.BuildServiceProvider();
//var InstallationManager = sp.GetRequiredService<IInstallationManager>();
//InstallationManager.InstallPackages("Modules,Themes");
// register transient scoped core services
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.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IInstallationManager InstallationManager)
{
if (env.IsDevelopment())
{
@ -251,6 +250,9 @@ namespace Oqtane.Server
app.UseHsts();
}
// install any modules or themes
InstallationManager.InstallPackages("Modules,Themes");
app.UseHttpsRedirection();
app.UseStaticFiles();
@ -443,6 +445,9 @@ namespace Oqtane.Server
app.UseBlazorDebugging();
}
// install any modules or themes
InstallationManager.InstallPackages("Modules,Themes");
app.UseClientSideBlazorFiles<Client.Startup>();
app.UseStaticFiles();