From ee54691a3a4dea081fdd528194a7952f61f8dc84 Mon Sep 17 00:00:00 2001 From: Salih Date: Wed, 23 Aug 2023 16:03:36 +0300 Subject: [PATCH 1/3] Add reCaptcha to comment edit section if it's enabled --- .../Public/Comments/UpdateCommentInput.cs | 4 ++++ .../CmsKitPublicCommentsController.cs | 14 ++++++++++++- .../Commenting/CommentingViewComponent.cs | 19 ++++++++++------- .../Components/Commenting/Default.cshtml | 18 ++++++++++++++++ .../Shared/Components/Commenting/default.js | 21 +++++++++++++------ 5 files changed, 62 insertions(+), 14 deletions(-) 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 a85783ee3fc..ca273cd94ad 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 @@ -15,4 +15,8 @@ public class UpdateCommentInput : ExtensibleObject, IHasConcurrencyStamp public string Text { get; set; } public string ConcurrencyStamp { get; set; } + + public Guid? CaptchaToken { get; set; } + + public int CaptchaAnswer { get; set; } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs index 00851bce15d..e5613cb6253 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; @@ -39,4 +40,15 @@ public virtual async Task ValidateAsync([FromBody] CreateCommentWithParametersIn var dto = ObjectMapper.Map (input); await CommentPublicAppService.CreateAsync(input.EntityType, input.EntityId, dto); } + + [HttpPost] + public virtual async Task UpdateAsync(Guid id, [FromBody] UpdateCommentInput input) + { + if (CmsKitCommentOptions.IsRecaptchaEnabled && input.CaptchaToken.HasValue) + { + SimpleMathsCaptchaGenerator.Validate(input.CaptchaToken.Value, input.CaptchaAnswer); + } + + await CommentPublicAppService.UpdateAsync(id, input); + } } 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 b1ea42397fb..4452dc5eec0 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 @@ -80,12 +80,7 @@ public virtual async Task InvokeAsync( if (CmsKitCommentOptions.IsRecaptchaEnabled) { - CaptchaOutput = SimpleMathsCaptchaGenerator.Generate(new CaptchaOptions( - number1MinValue: 1, - number1MaxValue: 10, - number2MinValue: 5, - number2MaxValue: 15) - ); + CaptchaOutput = GetCaptcha(); viewModel.CaptchaImageBase64 = GetCaptchaImageBase64(CaptchaOutput.ImageBytes); } @@ -93,7 +88,17 @@ public virtual async Task InvokeAsync( return View("~/Pages/CmsKit/Shared/Components/Commenting/Default.cshtml", this); } - private string GetCaptchaImageBase64(byte[] bytes) + public CaptchaOutput GetCaptcha() + { + return SimpleMathsCaptchaGenerator.Generate(new CaptchaOptions( + number1MinValue: 1, + number1MaxValue: 10, + number2MinValue: 5, + number2MaxValue: 15) + ); + } + + public string GetCaptchaImageBase64(byte[] bytes) { return $"data:image/jpg;base64,{Convert.ToBase64String(bytes)}"; } 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 34ce4681878..68ad72bc842 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 @@ -136,6 +136,24 @@
@L["MarkdownSupported"]
+ + @if (CmsKitCommentOptions.Value.IsRecaptchaEnabled) + { + var output = Model.GetCaptcha(); +
+ +
+
+ +
+
+ +
+ +
+
+ } + 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 c5c26672f19..75f32889828 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 @@ -111,14 +111,23 @@ $form.submit(function (e) { e.preventDefault(); let formAsObject = $form.serializeFormToObject(); - volo.cmsKit.public.comments.commentPublic.update( - formAsObject.id, - { + $.ajax({ + type: 'POST', + url: '/CmsKitPublicComments/Update/' + formAsObject.id, + contentType: 'application/json; charset=utf-8', + dataType: 'json', + data: JSON.stringify({ text: formAsObject.commentText, - concurrencyStamp: formAsObject.commentConcurrencyStamp + concurrencyStamp: formAsObject.commentConcurrencyStamp, + captchaToken: formAsObject.captchaId, + captchaAnswer: formAsObject.input?.captcha + }), + success: function () { + widgetManager.refresh($widget); + }, + error: function (data) { + abp.message.error(data.responseJSON.error.message); } - ).then(function () { - widgetManager.refresh($widget); }); }); }); From 80036d2c11989c14b6d6104afdf391ae54e91801 Mon Sep 17 00:00:00 2001 From: Salih Date: Wed, 23 Aug 2023 16:12:19 +0300 Subject: [PATCH 2/3] Update Default.cshtml --- .../Shared/Components/Commenting/Default.cshtml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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 68ad72bc842..5d11f624f37 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 @@ -39,7 +39,7 @@
- @L["MarkdownSupported"] + @L["MarkdownSupported"]
@if (CmsKitCommentOptions.Value.IsRecaptchaEnabled) @@ -127,14 +127,8 @@ -
-
- @L["Update"] - @L["Cancel"] -
-
- @L["MarkdownSupported"] + @L["MarkdownSupported"]
@if (CmsKitCommentOptions.Value.IsRecaptchaEnabled) @@ -153,7 +147,12 @@ } - +
+
+ @L["Update"] + @L["Cancel"] +
+
From a84e80cb46d992203b4b8d1790445422b74a750f Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Thu, 24 Aug 2023 14:53:54 +0300 Subject: [PATCH 3/3] CMS: Should provide recaptcha token if it's enabled for newsletter --- .../CmsKitPublicCommentsController.cs | 19 ++++++++++++++++--- .../Controllers/CmsKitPublicControllerBase.cs | 12 ++++++++++++ .../CmsKitPublicGlobalResourcesController.cs | 2 +- .../CmsKitPublicWidgetsController.cs | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicControllerBase.cs diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs index e5613cb6253..a2e2dea0131 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicCommentsController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.ObjectMapping; using Volo.CmsKit.Comments; @@ -13,7 +14,7 @@ namespace Volo.CmsKit.Public.Web.Controllers; //[Route("cms-kit/public-comments")] -public class CmsKitPublicCommentsController : AbpController +public class CmsKitPublicCommentsController : CmsKitPublicControllerBase { public ICommentPublicAppService CommentPublicAppService { get; } protected CmsKitCommentOptions CmsKitCommentOptions { get; } @@ -32,8 +33,10 @@ public CmsKitPublicCommentsController( [HttpPost] public virtual async Task ValidateAsync([FromBody] CreateCommentWithParametersInput input) { - if (CmsKitCommentOptions.IsRecaptchaEnabled && input.CaptchaToken.HasValue) + if (CmsKitCommentOptions.IsRecaptchaEnabled) { + CheckCaptchaTokenNullity(input.CaptchaToken); + SimpleMathsCaptchaGenerator.Validate(input.CaptchaToken.Value, input.CaptchaAnswer); } @@ -44,11 +47,21 @@ public virtual async Task ValidateAsync([FromBody] CreateCommentWithParametersIn [HttpPost] public virtual async Task UpdateAsync(Guid id, [FromBody] UpdateCommentInput input) { - if (CmsKitCommentOptions.IsRecaptchaEnabled && input.CaptchaToken.HasValue) + if (CmsKitCommentOptions.IsRecaptchaEnabled) { + CheckCaptchaTokenNullity(input.CaptchaToken); + SimpleMathsCaptchaGenerator.Validate(input.CaptchaToken.Value, input.CaptchaAnswer); } await CommentPublicAppService.UpdateAsync(id, input); } + + private void CheckCaptchaTokenNullity(Guid? captchaToken) + { + if (!captchaToken.HasValue) + { + throw new UserFriendlyException(L["CaptchaCodeMissingMessage"]); + } + } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicControllerBase.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicControllerBase.cs new file mode 100644 index 00000000000..f22444036d9 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicControllerBase.cs @@ -0,0 +1,12 @@ +using Volo.Abp.AspNetCore.Mvc; +using Volo.CmsKit.Localization; + +namespace Volo.CmsKit.Public.Web.Controllers; + +public abstract class CmsKitPublicControllerBase : AbpController +{ + public CmsKitPublicControllerBase() + { + LocalizationResource = typeof(CmsKitResource); + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicGlobalResourcesController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicGlobalResourcesController.cs index 115dfd3fccd..eb4356e8b91 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicGlobalResourcesController.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicGlobalResourcesController.cs @@ -11,7 +11,7 @@ namespace Volo.CmsKit.Public.Web.Controllers; [Route("cms-kit/global-resources")] -public class CmsKitPublicGlobalResourcesController: AbpController +public class CmsKitPublicGlobalResourcesController : CmsKitPublicControllerBase { private readonly IGlobalResourcePublicAppService _globalResourcePublicAppService; private readonly IDistributedCache _resourceCache; diff --git a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicWidgetsController.cs b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicWidgetsController.cs index c73fba95e79..67a9d64f673 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicWidgetsController.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Public.Web/Controllers/CmsKitPublicWidgetsController.cs @@ -7,7 +7,7 @@ namespace Volo.CmsKit.Public.Web.Controllers; -public class CmsKitPublicWidgetsController : AbpController +public class CmsKitPublicWidgetsController : CmsKitPublicControllerBase { public Task ReactionSelection(string entityType, string entityId) {