Merge pull request #4044 from sbwalker/dev

move logging from Html/Text controller to Server Service class so that is captured in all render modes
This commit is contained in:
Shaun Walker
2024-03-21 15:19:47 -04:00
committed by GitHub
2 changed files with 13 additions and 18 deletions

View File

@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Oqtane.Modules.HtmlText.Repository;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Oqtane.Shared; using Oqtane.Shared;
using Oqtane.Enums; using Oqtane.Enums;
@ -9,7 +8,6 @@ using Oqtane.Controllers;
using System.Net; using System.Net;
using Oqtane.Documentation; using Oqtane.Documentation;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Oqtane.Modules.HtmlText.Services; using Oqtane.Modules.HtmlText.Services;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -84,9 +82,7 @@ namespace Oqtane.Modules.HtmlText.Controllers
{ {
if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, htmlText.ModuleId)) if (ModelState.IsValid && IsAuthorizedEntityId(EntityNames.Module, htmlText.ModuleId))
{ {
htmlText = await _htmlTextService.AddHtmlTextAsync(htmlText); return await _htmlTextService.AddHtmlTextAsync(htmlText);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Html/Text Added {HtmlText}", htmlText);
return htmlText;
} }
else else
{ {
@ -104,7 +100,6 @@ namespace Oqtane.Modules.HtmlText.Controllers
if (IsAuthorizedEntityId(EntityNames.Module, moduleId)) if (IsAuthorizedEntityId(EntityNames.Module, moduleId))
{ {
await _htmlTextService.DeleteHtmlTextAsync(id, moduleId); await _htmlTextService.DeleteHtmlTextAsync(id, moduleId);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", id);
} }
else else
{ {

View File

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Http;
using Oqtane.Documentation; using Oqtane.Documentation;
using Oqtane.Enums; using Oqtane.Enums;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Models;
using Oqtane.Modules.HtmlText.Repository; using Oqtane.Modules.HtmlText.Repository;
using Oqtane.Security; using Oqtane.Security;
using Oqtane.Shared; using Oqtane.Shared;
@ -16,17 +17,17 @@ namespace Oqtane.Modules.HtmlText.Services
{ {
private readonly IHtmlTextRepository _htmlText; private readonly IHtmlTextRepository _htmlText;
private readonly IUserPermissions _userPermissions; private readonly IUserPermissions _userPermissions;
private readonly ITenantManager _tenantManager;
private readonly ILogManager _logger; private readonly ILogManager _logger;
private readonly IHttpContextAccessor _accessor; private readonly IHttpContextAccessor _accessor;
private readonly Alias _alias;
public ServerHtmlTextService(IHtmlTextRepository htmlText, IUserPermissions userPermissions, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor) public ServerHtmlTextService(IHtmlTextRepository htmlText, IUserPermissions userPermissions, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor)
{ {
_htmlText = htmlText; _htmlText = htmlText;
_userPermissions = userPermissions; _userPermissions = userPermissions;
_tenantManager = tenantManager;
_logger = logger; _logger = logger;
_accessor = accessor; _accessor = accessor;
_alias = tenantManager.GetAlias();
} }
public async Task<List<Models.HtmlText>> GetHtmlTextsAsync(int moduleId) public async Task<List<Models.HtmlText>> GetHtmlTextsAsync(int moduleId)
@ -44,8 +45,7 @@ namespace Oqtane.Modules.HtmlText.Services
public async Task<Models.HtmlText> GetHtmlTextAsync(int moduleId) public async Task<Models.HtmlText> GetHtmlTextAsync(int moduleId)
{ {
var alias = _tenantManager.GetAlias(); if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View))
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View))
{ {
var htmltexts = await _htmlText.GetHtmlTextsAsync(moduleId); var htmltexts = await _htmlText.GetHtmlTextsAsync(moduleId);
if (htmltexts != null && htmltexts.Any()) if (htmltexts != null && htmltexts.Any())
@ -66,8 +66,7 @@ namespace Oqtane.Modules.HtmlText.Services
public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId) public async Task<Models.HtmlText> GetHtmlTextAsync(int htmlTextId, int moduleId)
{ {
var alias = _tenantManager.GetAlias(); if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View))
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View))
{ {
return await _htmlText.GetHtmlTextAsync(htmlTextId); return await _htmlText.GetHtmlTextAsync(htmlTextId);
} }
@ -80,24 +79,25 @@ namespace Oqtane.Modules.HtmlText.Services
public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText) public async Task<Models.HtmlText> AddHtmlTextAsync(Models.HtmlText htmlText)
{ {
var alias = _tenantManager.GetAlias(); if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, htmlText.ModuleId, PermissionNames.Edit))
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, alias.SiteId, EntityNames.Module, htmlText.ModuleId, PermissionNames.Edit))
{ {
return await _htmlText.AddHtmlTextAsync(htmlText); htmlText = await _htmlText.AddHtmlTextAsync(htmlText);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "Html/Text Added {HtmlText}", htmlText);
} }
else else
{ {
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Add Attempt {HtmlText}", htmlText); _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Add Attempt {HtmlText}", htmlText);
return null; htmlText = null;
} }
return htmlText;
} }
public async Task DeleteHtmlTextAsync(int htmlTextId, int moduleId) public async Task DeleteHtmlTextAsync(int htmlTextId, int moduleId)
{ {
var alias = _tenantManager.GetAlias(); if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.Edit))
if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, alias.SiteId, EntityNames.Module, moduleId, PermissionNames.Edit))
{ {
await _htmlText.DeleteHtmlTextAsync(htmlTextId); await _htmlText.DeleteHtmlTextAsync(htmlTextId);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId);
} }
else else
{ {