From 697bbf9a1366e301986e6dfb84655c13b2fb9b6c Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Tue, 4 Apr 2023 15:05:39 +0300 Subject: [PATCH] Cms: Refactoring --- .../CmsKitWebUnifiedModule.cs | 10 +++++-- .../Pages/Index.cshtml | 2 +- .../CmsKit/Comments/CmsKitCommentOptions.cs | 4 +-- .../Public/Comments/CreateCommentInput.cs | 2 -- .../CreateCommentWithParametersInput.cs | 2 -- .../Public/Comments/UpdateCommentInput.cs | 2 -- .../Comments/CommentPublicAppService.cs | 27 +++++++++---------- .../Commenting/CommentingViewComponent.cs | 6 +---- .../Components/Commenting/Default.cshtml | 2 -- .../Shared/Components/Commenting/default.js | 6 ++--- .../CmsKitApplicationTestModule.cs | 11 +++++++- .../Comments/CommentPublicAppService_Tests.cs | 10 +++---- 12 files changed, 42 insertions(+), 42 deletions(-) diff --git a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs index 429d6b010c..f4f746a61d 100644 --- a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs +++ b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/CmsKitWebUnifiedModule.cs @@ -169,9 +169,15 @@ public class CmsKitWebUnifiedModule : AbpModule { options.EntityTypes.Add(new CommentEntityTypeDefinition("quote")); options.IsRecaptchaEnabled = true; - options.AllowedExternalUrls = new List + options.AllowedExternalUrls = new Dictionary> { - "https://abp.io/" + { + "quote", + new List + { + "https://abp.io/" + } + } }; }); diff --git a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml index 74e86b7c76..1b3f3ff94c 100644 --- a/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml +++ b/modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml @@ -90,7 +90,7 @@ @if (GlobalFeatureManager.Instance.IsEnabled()) { - @await Component.InvokeAsync(typeof(CommentingViewComponent), new {entityType = "quote", entityId = "2", allowExternalUrls = false}) + @await Component.InvokeAsync(typeof(CommentingViewComponent), new {entityType = "quote", entityId = "2"}) } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Comments/CmsKitCommentOptions.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Comments/CmsKitCommentOptions.cs index ffa1097aab..1346541e69 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Comments/CmsKitCommentOptions.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Comments/CmsKitCommentOptions.cs @@ -15,7 +15,7 @@ public class CmsKitCommentOptions public bool IsRecaptchaEnabled { get; set; } /// - /// Indicates the allowed external URLs, which can be included in a comment. + /// Indicates the allowed external URLs by entity types, which can be included in a comment. /// - public List AllowedExternalUrls { get; set; } = new(); + public Dictionary> AllowedExternalUrls { get; set; } = new(); } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs index ef78636ea7..c0eae8650e 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentInput.cs @@ -17,6 +17,4 @@ public class CreateCommentInput public Guid? CaptchaToken { get; set; } public int CaptchaAnswer { get; set; } - - public bool AllowExternalUrls { get; set; } = true; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs index d8f946d1a0..bfdbd0a95d 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/CreateCommentWithParametersInput.cs @@ -18,8 +18,6 @@ public class CreateCommentWithParametersInput [Required] public string EntityId { get; set; } - public bool AllowExternalUrls { get; set; } = true; - public Guid? RepliedCommentId { get; set; } public Guid? CaptchaToken { get; set; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/UpdateCommentInput.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/UpdateCommentInput.cs index 0992622603..7ec1279704 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/UpdateCommentInput.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application.Contracts/Volo/CmsKit/Public/Comments/UpdateCommentInput.cs @@ -14,6 +14,4 @@ public class UpdateCommentInput : IHasConcurrencyStamp public string Text { get; set; } public string ConcurrencyStamp { get; set; } - - public bool AllowExternalUrls { get; set; } = true; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs index 8809d8168e..4a7bd8be8c 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Public/Comments/CommentPublicAppService.cs @@ -61,7 +61,7 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli [Authorize] public virtual async Task CreateAsync(string entityType, string entityId, CreateCommentInput input) { - CheckExternalUrls(input.AllowExternalUrls, input.Text); + CheckExternalUrls(entityType, input.Text); var user = await CmsUserLookupService.GetByIdAsync(CurrentUser.GetId()); @@ -93,14 +93,13 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli [Authorize] public virtual async Task UpdateAsync(Guid id, UpdateCommentInput input) { - CheckExternalUrls(input.AllowExternalUrls, input.Text); - var comment = await CommentRepository.GetAsync(id); - if (comment.CreatorId != CurrentUser.GetId()) { throw new AbpAuthorizationException(); } + + CheckExternalUrls(comment.EntityType, input.Text); comment.SetText(input.Text); comment.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp); @@ -158,18 +157,13 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli return ObjectMapper.Map(comments.Single(c => c.Comment.Id == commentId).Author); } - private void CheckExternalUrls(bool allowExternalUrls, string text) + private void CheckExternalUrls(string entityType, string text) { - if (allowExternalUrls) + if (!CmsCommentOptions.AllowedExternalUrls.TryGetValue(entityType, out var allowedExternalUrls)) { return; } - if (!CmsCommentOptions.AllowedExternalUrls.Any()) - { - throw new UserFriendlyException(L["UnAllowedExternalUrlMessage"]); - } - var matches = Regex.Matches(text, RegexMarkdownUrlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -180,14 +174,14 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli continue; } - var url = match.Groups[1].Value; + var url = NormalizeUrl(match.Groups[1].Value); if (!IsExternalUrl(url)) { continue; } - if (!CmsCommentOptions.AllowedExternalUrls.Contains(url.Replace("www.", "").RemovePostFix("/"), - StringComparer.InvariantCultureIgnoreCase)) + if (!allowedExternalUrls.Any(allowedExternalUrl => + url.Contains(NormalizeUrl(allowedExternalUrl), StringComparison.OrdinalIgnoreCase))) { throw new UserFriendlyException(L["UnAllowedExternalUrlMessage"]); } @@ -199,4 +193,9 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli return url.StartsWith("https", StringComparison.InvariantCultureIgnoreCase) || url.StartsWith("http", StringComparison.InvariantCultureIgnoreCase); } + + private static string NormalizeUrl(string url) + { + return url.Replace("www.", "").RemovePostFix("/"); + } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs index fd7949674b..b1ea42397f 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/CommentingViewComponent.cs @@ -60,8 +60,7 @@ public class CommentingViewComponent : AbpViewComponent public virtual async Task InvokeAsync( string entityType, string entityId, - IEnumerable referralLinks = null, - bool allowExternalUrls = true) + IEnumerable referralLinks = null) { referralLinks ??= Enumerable.Empty(); var comments = (await CommentPublicAppService @@ -73,7 +72,6 @@ public class CommentingViewComponent : AbpViewComponent { EntityId = entityId, EntityType = entityType, - AllowExternalUrls = allowExternalUrls, ReferralLinks = referralLinks, LoginUrl = loginUrl, Comments = comments.OrderByDescending(i => i.CreationTime).ToList() @@ -124,8 +122,6 @@ public class CommentingViewComponent : AbpViewComponent public string EntityId { get; set; } - public bool AllowExternalUrls { get; set; } - public IEnumerable ReferralLinks { get; set; } public string LoginUrl { get; set; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/Default.cshtml b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/Default.cshtml index d5b9a064d0..944a818d74 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/Default.cshtml +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/Default.cshtml @@ -32,7 +32,6 @@ style="@(string.IsNullOrEmpty(repliedCommentId?.ToString() ?? "") ? "" : "display:none")">
-
@@ -121,7 +120,6 @@
-
diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js index 1c49e110f3..20b7119344 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/Commenting/default.js @@ -115,8 +115,7 @@ formAsObject.id, { text: formAsObject.commentText, - concurrencyStamp: formAsObject.commentConcurrencyStamp, - allowExternalUrls: formAsObject.allowExternalUrls + concurrencyStamp: formAsObject.commentConcurrencyStamp } ).then(function () { widgetManager.refresh($widget); @@ -152,8 +151,7 @@ repliedCommentId: formAsObject.repliedCommentId, text: formAsObject.commentText, captchaToken: formAsObject.captchaId, - captchaAnswer: formAsObject.input?.captcha, - allowExternalUrls: formAsObject.allowExternalUrls + captchaAnswer: formAsObject.input?.captcha }), success: function () { widgetManager.refresh($widget); diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestModule.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestModule.cs index 041f77d63c..e0dba22140 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestModule.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/CmsKitApplicationTestModule.cs @@ -14,7 +14,16 @@ public class CmsKitApplicationTestModule : AbpModule { Configure(options => { - options.AllowedExternalUrls = new List { "https://abp.io" }; + options.AllowedExternalUrls = new Dictionary> + { + { + "EntityName1", + new List + { + "https://abp.io/" + } + } + }; }); } } diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs index e9e7cc817b..ddd913a93f 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Comments/CommentPublicAppService_Tests.cs @@ -72,10 +72,10 @@ public class CommentPublicAppService_Tests : CmsKitApplicationTestBase await _commentAppService.CreateAsync( _cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, - new CreateCommentInput { + new CreateCommentInput + { RepliedCommentId = null, Text = "[ABP Community](https://community.abp.io/)", //not allowed URL - AllowExternalUrls = false } )); } @@ -102,14 +102,14 @@ public class CommentPublicAppService_Tests : CmsKitApplicationTestBase [Fact] public async Task UpdateAsync_ShouldThrowUserFriendlyException_If_Url_UnAllowed() { - _currentUser.Id.Returns(_cmsKitTestData.User2Id); + _currentUser.Id.Returns(_cmsKitTestData.User1Id); await Should.ThrowAsync(async () => await _commentAppService.UpdateAsync( _cmsKitTestData.CommentWithChildId, - new UpdateCommentInput { + new UpdateCommentInput + { Text = "[ABP Community - Update](https://community.abp.io/)", //not allowed URL - AllowExternalUrls = false } )); }