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 a85783ee3f..ca273cd94a 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 00851bce15..a2e2dea013 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,7 +1,9 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; 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; @@ -12,7 +14,7 @@ using Volo.CmsKit.Public.Web.Security.Captcha; 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; } @@ -31,12 +33,35 @@ public class CmsKitPublicCommentsController : AbpController [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); } 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) + { + 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 0000000000..f22444036d --- /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 115dfd3fcc..eb4356e8b9 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 @@ using Volo.CmsKit.Public.GlobalResources; 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 c73fba95e7..67a9d64f67 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 @@ using Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.ReactionSelection; namespace Volo.CmsKit.Public.Web.Controllers; -public class CmsKitPublicWidgetsController : AbpController +public class CmsKitPublicWidgetsController : CmsKitPublicControllerBase { public Task ReactionSelection(string entityType, string entityId) { 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 b1ea42397f..4452dc5eec 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 class CommentingViewComponent : AbpViewComponent 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 class CommentingViewComponent : AbpViewComponent 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 34ce468187..5d11f624f3 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,15 +127,32 @@ +
+ @L["MarkdownSupported"] +
+ + @if (CmsKitCommentOptions.Value.IsRecaptchaEnabled) + { + var output = Model.GetCaptcha(); +
+ +
+
+ +
+
+ +
+ +
+
+ }
@L["Update"] @L["Cancel"]
-
- @L["MarkdownSupported"] -
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 c5c26672f1..75f3288982 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); }); }); });