Added IsRecaptchaEnabled to CmsKitCommentOptions

pull/15039/head
malik masis 3 years ago
parent d496d3b62d
commit 111921cf92

@ -167,6 +167,7 @@ public class CmsKitWebUnifiedModule : AbpModule
Configure<CmsKitCommentOptions>(options =>
{
options.EntityTypes.Add(new CommentEntityTypeDefinition("quote"));
options.IsRecaptchaEnabled = true;
});
Configure<CmsKitMediaOptions>(options =>

@ -7,4 +7,10 @@ public class CmsKitCommentOptions
{
[NotNull]
public List<CommentEntityTypeDefinition> EntityTypes { get; } = new List<CommentEntityTypeDefinition>();
/// <summary>
/// Flag to enable/disable ReCaptcha for comment component.
/// Default: false
/// </summary>
public bool IsRecaptchaEnabled { get; set; }
}

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Formats.Gif;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
@ -28,7 +29,7 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli
protected ICmsUserLookupService CmsUserLookupService { get; }
public IDistributedEventBus DistributedEventBus { get; }
protected CommentManager CommentManager { get; }
protected CmsKitCommentOptions CmsKitCommentOptions { get; }
public SimpleMathsCaptchaGenerator SimpleMathsCaptchaGenerator { get; }
public CommentPublicAppService(
@ -36,12 +37,14 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli
ICmsUserLookupService cmsUserLookupService,
IDistributedEventBus distributedEventBus,
CommentManager commentManager,
IOptions<CmsKitCommentOptions> cmsKitCommentOptions,
SimpleMathsCaptchaGenerator simpleMathsCaptchaGenerator)
{
CommentRepository = commentRepository;
CmsUserLookupService = cmsUserLookupService;
DistributedEventBus = distributedEventBus;
CommentManager = commentManager;
CmsKitCommentOptions = cmsKitCommentOptions.Value;
SimpleMathsCaptchaGenerator = simpleMathsCaptchaGenerator;
}
@ -58,7 +61,7 @@ public class CommentPublicAppService : CmsKitPublicAppServiceBase, ICommentPubli
[Authorize]
public virtual async Task<CommentDto> CreateAsync(string entityType, string entityId, CreateCommentInput input)
{
if (input.RepliedCommentId.HasValue)
if (CmsKitCommentOptions.IsRecaptchaEnabled && input.CaptchaToken.HasValue)
{
SimpleMathsCaptchaGenerator.Validate(input.CaptchaToken.Value, input.CaptchaAnswer);
}

@ -8,6 +8,7 @@ using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.UI;
using Volo.Abp.AspNetCore.Mvc.UI.Widgets;
using Volo.CmsKit.Comments;
using Volo.CmsKit.Public.Application.Security.VoloCaptcha;
using Volo.CmsKit.Public.Comments;
using Volo.CmsKit.Web.Renderers;
@ -26,6 +27,7 @@ public class CommentingViewComponent : AbpViewComponent
public ICommentPublicAppService CommentPublicAppService { get; }
public IMarkdownToHtmlRenderer MarkdownToHtmlRenderer { get; }
public AbpMvcUiOptions AbpMvcUiOptions { get; }
public CmsKitCommentOptions CmsKitCommentOptions { get; }
public SimpleMathsCaptchaGenerator SimpleMathsCaptchaGenerator { get; }
[HiddenInput]
@ -45,11 +47,13 @@ public class CommentingViewComponent : AbpViewComponent
ICommentPublicAppService commentPublicAppService,
IOptions<AbpMvcUiOptions> options,
IMarkdownToHtmlRenderer markdownToHtmlRenderer,
IOptions<CmsKitCommentOptions> cmsKitCommentOptions,
SimpleMathsCaptchaGenerator simpleMathsCaptchaGenerator)
{
CommentPublicAppService = commentPublicAppService;
MarkdownToHtmlRenderer = markdownToHtmlRenderer;
AbpMvcUiOptions = options.Value;
CmsKitCommentOptions = cmsKitCommentOptions.Value;
SimpleMathsCaptchaGenerator = simpleMathsCaptchaGenerator;
}
@ -71,14 +75,17 @@ public class CommentingViewComponent : AbpViewComponent
};
await ConvertMarkdownTextsToHtml(viewModel);
CaptchaOutput = SimpleMathsCaptchaGenerator.Generate(new CaptchaOptions(
number1MinValue: 1,
number1MaxValue: 10,
number2MinValue: 5,
number2MaxValue: 15)
);
viewModel.CaptchaImageBase64 = GetCaptchaImageBase64(CaptchaOutput.ImageBytes);
if (CmsKitCommentOptions.IsRecaptchaEnabled)
{
CaptchaOutput = SimpleMathsCaptchaGenerator.Generate(new CaptchaOptions(
number1MinValue: 1,
number1MaxValue: 10,
number2MinValue: 5,
number2MaxValue: 15)
);
viewModel.CaptchaImageBase64 = GetCaptchaImageBase64(CaptchaOutput.ImageBytes);
}
this.Input = viewModel;
return View("~/Pages/CmsKit/Shared/Components/Commenting/Default.cshtml", this);
}

@ -3,6 +3,7 @@
@using Microsoft.Extensions.Options
@using Volo.Abp.GlobalFeatures
@using Volo.Abp.Users
@using Volo.CmsKit.Comments;
@using Volo.CmsKit.GlobalFeatures
@using Volo.CmsKit.Localization
@using Volo.CmsKit.Public.Comments
@ -10,6 +11,7 @@
@using Volo.CmsKit.Web
@inject ICurrentUser CurrentUser
@inject IOptionsSnapshot<CmsKitUiOptions> cmsKitUiOptions;
@inject IOptions<CmsKitCommentOptions> CmsKitCommentOptions
@inject IHtmlLocalizer<CmsKitResource> L
@model Volo.CmsKit.Public.Web.Pages.CmsKit.Shared.Components.Commenting.CommentingViewComponent
@ -37,22 +39,22 @@
</div>
</div>
<div class="volo-captcha">
<label class="form-label" for="Input_Captcha">Captcha code</label>
<div class="d-flex">
<div class="bd-highlight">
<img src="@Model.Input.CaptchaImageBase64" />
</div>
<div class="flex-grow-1 bd-highlight">
<abp-input asp-for="@Model.Input.Captcha" suppress-label="true" class="d-inline-block" autocomplete="off" />
@if (CmsKitCommentOptions.Value.IsRecaptchaEnabled)
{
<div class="volo-captcha">
<label class="form-label" for="Input_Captcha">@L["CaptchaCode"]</label>
<div class="d-flex">
<div class="bd-highlight">
<img src="@Model.Input.CaptchaImageBase64" />
</div>
<div class="flex-grow-1 bd-highlight">
<abp-input asp-for="@Model.Input.Captcha" suppress-label="true" class="d-inline-block" autocomplete="off" />
</div>
<abp-input asp-for="@Model.CaptchaId" value="@Model.CaptchaOutput.Id" />
</div>
<abp-input asp-for="@Model.CaptchaId" value="@Model.CaptchaOutput.Id" />
</div>
</div>
}
<div class="col-auto">
<div class="text-end">
<abp-button id="submit-button" type="submit" button-type="Primary" size="Block">

Loading…
Cancel
Save