diff --git a/Oqtane.Client/Modules/HtmlText/Edit.razor b/Oqtane.Client/Modules/HtmlText/Edit.razor index 384a27b4..7f8ed55a 100644 --- a/Oqtane.Client/Modules/HtmlText/Edit.razor +++ b/Oqtane.Client/Modules/HtmlText/Edit.razor @@ -122,13 +122,9 @@ { try { - htmltext = await HtmlTextService.GetHtmlTextAsync(htmltext.HtmlTextId, htmltext.ModuleId); - if (htmltext != null) - { - _view = htmltext.Content; - _view = Utilities.FormatContent(_view, PageState.Alias, "render"); - StateHasChanged(); - } + _view = htmltext.Content; + _view = Utilities.FormatContent(_view, PageState.Alias, "render"); + StateHasChanged(); } catch (Exception ex) { @@ -141,19 +137,15 @@ { try { - htmltext = await HtmlTextService.GetHtmlTextAsync(htmltext.HtmlTextId, ModuleState.ModuleId); - if (htmltext != null) - { - var content = htmltext.Content; - htmltext = new HtmlText(); - htmltext.ModuleId = ModuleState.ModuleId; - htmltext.Content = content; - await HtmlTextService.AddHtmlTextAsync(htmltext); - await logger.LogInformation("Content Restored {HtmlText}", htmltext); - AddModuleMessage(Localizer["Message.Content.Restored"], MessageType.Success); - await LoadContent(); - StateHasChanged(); - } + var content = htmltext.Content; + htmltext = new HtmlText(); + htmltext.ModuleId = ModuleState.ModuleId; + htmltext.Content = content; + await HtmlTextService.AddHtmlTextAsync(htmltext); + await logger.LogInformation("Content Restored {HtmlText}", htmltext); + AddModuleMessage(Localizer["Message.Content.Restored"], MessageType.Success); + await LoadContent(); + StateHasChanged(); } catch (Exception ex) { @@ -166,15 +158,11 @@ { try { - htmltext = await HtmlTextService.GetHtmlTextAsync(htmltext.HtmlTextId, ModuleState.ModuleId); - if (htmltext != null) - { - await HtmlTextService.DeleteHtmlTextAsync(htmltext.HtmlTextId, htmltext.ModuleId); - await logger.LogInformation("Content Deleted {HtmlText}", htmltext); - AddModuleMessage(Localizer["Message.Content.Deleted"], MessageType.Success); - await LoadContent(); - StateHasChanged(); - } + await HtmlTextService.DeleteHtmlTextAsync(htmltext.HtmlTextId, htmltext.ModuleId); + await logger.LogInformation("Content Deleted {HtmlText}", htmltext); + AddModuleMessage(Localizer["Message.Content.Deleted"], MessageType.Success); + await LoadContent(); + StateHasChanged(); } catch (Exception ex) { diff --git a/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs b/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs index 600374cf..324cfedb 100644 --- a/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs +++ b/Oqtane.Client/Modules/HtmlText/Services/HtmlTextService.cs @@ -14,8 +14,6 @@ namespace Oqtane.Modules.HtmlText.Services Task GetHtmlTextAsync(int moduleId); - Task GetHtmlTextAsync(int htmlTextId, int moduleId); - Task AddHtmlTextAsync(Models.HtmlText htmltext); Task DeleteHtmlTextAsync(int htmlTextId, int moduleId); @@ -38,11 +36,6 @@ namespace Oqtane.Modules.HtmlText.Services return await GetJsonAsync(CreateAuthorizationPolicyUrl($"{ApiUrl}/{moduleId}", EntityNames.Module, moduleId)); } - public async Task GetHtmlTextAsync(int htmlTextId, int moduleId) - { - return await GetJsonAsync(CreateAuthorizationPolicyUrl($"{ApiUrl}/{htmlTextId}/{moduleId}", EntityNames.Module, moduleId)); - } - public async Task AddHtmlTextAsync(Models.HtmlText htmlText) { return await PostJsonAsync(CreateAuthorizationPolicyUrl($"{ApiUrl}", EntityNames.Module, htmlText.ModuleId), htmlText); diff --git a/Oqtane.Client/Modules/HtmlText/Settings.razor b/Oqtane.Client/Modules/HtmlText/Settings.razor index 85413e55..eaf30bc0 100644 --- a/Oqtane.Client/Modules/HtmlText/Settings.razor +++ b/Oqtane.Client/Modules/HtmlText/Settings.razor @@ -39,7 +39,7 @@ try { _dynamictokens = SettingService.GetSetting(ModuleState.Settings, "DynamicTokens", "false"); - _versions = SettingService.GetSetting(ModuleState.Settings, "Versions", "3"); + _versions = SettingService.GetSetting(ModuleState.Settings, "Versions", "5"); } catch (Exception ex) { diff --git a/Oqtane.Server/Modules/HtmlText/Controllers/HtmlTextController.cs b/Oqtane.Server/Modules/HtmlText/Controllers/HtmlTextController.cs index fb8ef512..c5b6d851 100644 --- a/Oqtane.Server/Modules/HtmlText/Controllers/HtmlTextController.cs +++ b/Oqtane.Server/Modules/HtmlText/Controllers/HtmlTextController.cs @@ -58,23 +58,6 @@ namespace Oqtane.Modules.HtmlText.Controllers } } - // GET api//5/6 - [HttpGet("{id}/{moduleId}")] - [Authorize(Policy = PolicyNames.ViewModule)] - public async Task Get(int id, int moduleId) - { - if (IsAuthorizedEntityId(EntityNames.Module, moduleId)) - { - return await _htmlTextService.GetHtmlTextAsync(id, moduleId); - } - else - { - _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Get Attempt {HtmlTextId}", id); - HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; - return null; - } - } - // POST api/ [HttpPost] [Authorize(Policy = PolicyNames.EditModule)] diff --git a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs index 93647fe7..cbef97fb 100644 --- a/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs +++ b/Oqtane.Server/Modules/HtmlText/Manager/HtmlTextManager.cs @@ -1,18 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; -using Oqtane.Infrastructure; -using Oqtane.Models; -using Oqtane.Modules.HtmlText.Repository; using System.Net; -using Oqtane.Enums; -using Oqtane.Repository; -using Oqtane.Shared; -using Oqtane.Migrations.Framework; -using Oqtane.Documentation; -using Oqtane.Interfaces; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Memory; +using Oqtane.Documentation; +using Oqtane.Enums; +using Oqtane.Infrastructure; +using Oqtane.Interfaces; +using Oqtane.Migrations.Framework; +using Oqtane.Models; +using Oqtane.Modules.HtmlText.Repository; +using Oqtane.Repository; +using Oqtane.Shared; // ReSharper disable ConvertToUsingDeclaration @@ -21,20 +21,15 @@ namespace Oqtane.Modules.HtmlText.Manager [PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")] public class HtmlTextManager : MigratableModuleBase, IInstallable, IPortable, ISynchronizable, ISearchable { - private readonly IHtmlTextRepository _htmlText; + private readonly IHtmlTextRepository _htmlTextRepository; private readonly IDBContextDependencies _DBContextDependencies; private readonly ISqlRepository _sqlRepository; private readonly ITenantManager _tenantManager; private readonly IMemoryCache _cache; - public HtmlTextManager( - IHtmlTextRepository htmlText, - IDBContextDependencies DBContextDependencies, - ISqlRepository sqlRepository, - ITenantManager tenantManager, - IMemoryCache cache) + public HtmlTextManager(IHtmlTextRepository htmlTextRepository, IDBContextDependencies DBContextDependencies, ISqlRepository sqlRepository, ITenantManager tenantManager, IMemoryCache cache) { - _htmlText = htmlText; + _htmlTextRepository = htmlTextRepository; _DBContextDependencies = DBContextDependencies; _sqlRepository = sqlRepository; _tenantManager = tenantManager; @@ -61,7 +56,7 @@ namespace Oqtane.Modules.HtmlText.Manager public string ExportModule(Module module) { string content = ""; - var htmltext = GetModuleContent(module.ModuleId); + var htmltext = _htmlTextRepository.GetHtmlText(module.ModuleId); if (htmltext != null) { content = WebUtility.HtmlEncode(htmltext.Content); @@ -78,7 +73,7 @@ namespace Oqtane.Modules.HtmlText.Manager public string ExtractModule(Module module, DateTime lastSynchronizedOn) { string content = ""; - var htmltext = GetModuleContent(module.ModuleId); + var htmltext = _htmlTextRepository.GetHtmlText(module.ModuleId); if (htmltext != null && htmltext.CreatedOn > lastSynchronizedOn) { content = WebUtility.HtmlEncode(htmltext.Content); @@ -91,24 +86,13 @@ namespace Oqtane.Modules.HtmlText.Manager SaveModuleContent(module, content); } - private Models.HtmlText GetModuleContent(int moduleId) - { - // get the most recent htmltext record for the module - var htmltexts = _htmlText.GetHtmlTexts(moduleId); - if (htmltexts != null && htmltexts.Any()) - { - return htmltexts.OrderByDescending(item => item.CreatedOn).First(); - } - return null; - } - private void SaveModuleContent(Module module, string content) { content = WebUtility.HtmlDecode(content); var htmlText = new Models.HtmlText(); htmlText.ModuleId = module.ModuleId; htmlText.Content = content; - _htmlText.AddHtmlText(htmlText); + _htmlTextRepository.AddHtmlText(htmlText); //clear the cache for the module var alias = _tenantManager.GetAlias(); @@ -123,7 +107,7 @@ namespace Oqtane.Modules.HtmlText.Manager { var searchContents = new List(); - var htmltext = _htmlText.GetHtmlTexts(pageModule.ModuleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault(); + var htmltext = _htmlTextRepository.GetHtmlText(pageModule.ModuleId); if (htmltext != null && htmltext.CreatedOn >= lastIndexedOn) { searchContents.Add(new SearchContent diff --git a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs index 2f999602..490242a5 100644 --- a/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs +++ b/Oqtane.Server/Modules/HtmlText/Repository/HtmlTextRepository.cs @@ -11,7 +11,7 @@ namespace Oqtane.Modules.HtmlText.Repository public interface IHtmlTextRepository { IEnumerable GetHtmlTexts(int moduleId); - Models.HtmlText GetHtmlText(int htmlTextId); + Models.HtmlText GetHtmlText(int moduleId); Models.HtmlText AddHtmlText(Models.HtmlText htmlText); void DeleteHtmlText(int htmlTextId); } @@ -34,17 +34,18 @@ namespace Oqtane.Modules.HtmlText.Repository return db.HtmlText.Where(item => item.ModuleId == moduleId).ToList(); } - public Models.HtmlText GetHtmlText(int htmlTextId) + public Models.HtmlText GetHtmlText(int moduleId) { using var db = _factory.CreateDbContext(); - return db.HtmlText.Find(htmlTextId); + return db.HtmlText.Where(item => item.ModuleId == moduleId)? + .OrderByDescending(item => item.CreatedOn).FirstOrDefault(); } public Models.HtmlText AddHtmlText(Models.HtmlText htmlText) { using var db = _factory.CreateDbContext(); - var versions = int.Parse(_settingRepository.GetSettingValue(EntityNames.Module, htmlText.ModuleId, "Versions", "3")); + var versions = int.Parse(_settingRepository.GetSettingValue(EntityNames.Module, htmlText.ModuleId, "Versions", "5")); if (versions > 0) { var htmlTexts = db.HtmlText.Where(item => item.ModuleId == htmlText.ModuleId).OrderByDescending(item => item.CreatedOn).ToList(); diff --git a/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs b/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs index 49055c4e..8e9150b3 100644 --- a/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs +++ b/Oqtane.Server/Modules/HtmlText/Services/HtmlTextService.cs @@ -17,16 +17,16 @@ namespace Oqtane.Modules.HtmlText.Services [PrivateApi("Mark HtmlText classes as private, since it's not very useful in the public docs")] public class ServerHtmlTextService : IHtmlTextService, ITransientService { - private readonly IHtmlTextRepository _htmlText; + private readonly IHtmlTextRepository _htmlTextRepository; private readonly IUserPermissions _userPermissions; private readonly IMemoryCache _cache; private readonly ILogManager _logger; private readonly IHttpContextAccessor _accessor; private readonly Alias _alias; - public ServerHtmlTextService(IHtmlTextRepository htmlText, IUserPermissions userPermissions, IMemoryCache cache, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor) + public ServerHtmlTextService(IHtmlTextRepository htmlTextRepository, IUserPermissions userPermissions, IMemoryCache cache, ITenantManager tenantManager, ILogManager logger, IHttpContextAccessor accessor) { - _htmlText = htmlText; + _htmlTextRepository = htmlTextRepository; _userPermissions = userPermissions; _cache = cache; _logger = logger; @@ -38,7 +38,7 @@ namespace Oqtane.Modules.HtmlText.Services { if (_accessor.HttpContext.User.IsInRole(RoleNames.Registered)) { - return Task.FromResult(GetCachedHtmlTexts(moduleId)); + return Task.FromResult(_htmlTextRepository.GetHtmlTexts(moduleId).ToList()); } else { @@ -51,7 +51,11 @@ namespace Oqtane.Modules.HtmlText.Services { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View)) { - return Task.FromResult(GetCachedHtmlTexts(moduleId)?.OrderByDescending(item => item.CreatedOn).FirstOrDefault()); + return Task.FromResult(_cache.GetOrCreate($"HtmlText:{_alias.SiteKey}:{moduleId}", entry => + { + entry.SlidingExpiration = TimeSpan.FromMinutes(30); + return _htmlTextRepository.GetHtmlText(moduleId); + })); } else { @@ -60,24 +64,11 @@ namespace Oqtane.Modules.HtmlText.Services } } - public Task GetHtmlTextAsync(int htmlTextId, int moduleId) - { - if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.View)) - { - return Task.FromResult(GetCachedHtmlTexts(moduleId)?.FirstOrDefault(item => item.HtmlTextId == htmlTextId)); - } - else - { - _logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Html/Text Get Attempt {HtmlTextId} {ModuleId}", htmlTextId, moduleId); - return null; - } - } - public Task AddHtmlTextAsync(Models.HtmlText htmlText) { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, htmlText.ModuleId, PermissionNames.Edit)) { - htmlText = _htmlText.AddHtmlText(htmlText); + htmlText = _htmlTextRepository.AddHtmlText(htmlText); ClearCache(htmlText.ModuleId); _logger.Log(LogLevel.Information, this, LogFunction.Create, "Html/Text Added {HtmlText}", htmlText); } @@ -93,7 +84,7 @@ namespace Oqtane.Modules.HtmlText.Services { if (_userPermissions.IsAuthorized(_accessor.HttpContext.User, _alias.SiteId, EntityNames.Module, moduleId, PermissionNames.Edit)) { - _htmlText.DeleteHtmlText(htmlTextId); + _htmlTextRepository.DeleteHtmlText(htmlTextId); ClearCache(moduleId); _logger.Log(LogLevel.Information, this, LogFunction.Delete, "Html/Text Deleted {HtmlTextId}", htmlTextId); } @@ -104,15 +95,6 @@ namespace Oqtane.Modules.HtmlText.Services return Task.CompletedTask; } - private List GetCachedHtmlTexts(int moduleId) - { - return _cache.GetOrCreate($"HtmlText:{_alias.SiteKey}:{moduleId}", entry => - { - entry.SlidingExpiration = TimeSpan.FromMinutes(30); - return _htmlText.GetHtmlTexts(moduleId).ToList(); - }); - } - private void ClearCache(int moduleId) { _cache.Remove($"HtmlText:{_alias.SiteKey}:{moduleId}");