Skip to content

Commit

Permalink
Merge pull request #9 from Lombiq/issue/OSOE-60
Browse files Browse the repository at this point in the history
OSOE-60: .NET 6 and Orchard Core 1.3 upgrade
  • Loading branch information
sarahelsaig authored Mar 17, 2022
2 parents 1006c77 + 2f26ad4 commit babbc27
Show file tree
Hide file tree
Showing 27 changed files with 543 additions and 572 deletions.
95 changes: 47 additions & 48 deletions Activities/ValidatePrivacyConsentCheckboxTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,66 @@
using System.Linq;
using System.Threading.Tasks;

namespace Lombiq.Privacy.Activities
namespace Lombiq.Privacy.Activities;

public class ValidatePrivacyConsentCheckboxTask : TaskActivity
{
public class ValidatePrivacyConsentCheckboxTask : TaskActivity
private readonly IUpdateModelAccessor _updateModelAccessor;
private readonly IStringLocalizer T;
private readonly IHttpContextAccessor _hca;
private readonly IPrivacyConsentService _consentService;

public ValidatePrivacyConsentCheckboxTask(
IUpdateModelAccessor updateModelAccessor,
IStringLocalizer<ValidatePrivacyConsentCheckboxTask> stringLocalizer,
IHttpContextAccessor hca,
IPrivacyConsentService consentService)
{
private readonly IUpdateModelAccessor _updateModelAccessor;
private readonly IStringLocalizer T;
private readonly IHttpContextAccessor _hca;
private readonly IPrivacyConsentService _consentService;
_updateModelAccessor = updateModelAccessor;
_hca = hca;
_consentService = consentService;
T = stringLocalizer;
}

public ValidatePrivacyConsentCheckboxTask(
IUpdateModelAccessor updateModelAccessor,
IStringLocalizer<ValidatePrivacyConsentCheckboxTask> stringLocalizer,
IHttpContextAccessor hca,
IPrivacyConsentService consentService)
{
_updateModelAccessor = updateModelAccessor;
_hca = hca;
_consentService = consentService;
T = stringLocalizer;
}
public override string Name => nameof(ValidatePrivacyConsentCheckboxTask);

public override string Name => nameof(ValidatePrivacyConsentCheckboxTask);
public override LocalizedString DisplayText => T["Validate Consent Checkbox Task"];

public override LocalizedString DisplayText => T["Validate Consent Checkbox Task"];
public override LocalizedString Category => T["Validation"];

public override LocalizedString Category => T["Validation"];
public override bool HasEditor => false;

public override bool HasEditor => false;
public override IEnumerable<Outcome> GetPossibleOutcomes(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext) =>
Outcomes(T["Done"], T["Valid"], T["Invalid"]);

public override IEnumerable<Outcome> GetPossibleOutcomes(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext) =>
Outcomes(T["Done"], T["Valid"], T["Invalid"]);
public override async Task<ActivityExecutionResult> ExecuteAsync(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext)
{
// If the user has already accepted the privacy statement, it doesn't need to validate that form again.
if (await _consentService.IsUserAcceptedConsentAsync(_hca.HttpContext))
return Outcomes("Done", "Valid");

public override async Task<ActivityExecutionResult> ExecuteAsync(
WorkflowExecutionContext workflowContext,
ActivityContext activityContext)
{
// If the user has already accepted the privacy statement, it doesn't need to validate that form again.
if (await _consentService.IsUserAcceptedConsentAsync(_hca.HttpContext))
return Outcomes("Done", "Valid");
var consentCheckboxName = $"{nameof(PrivacyConsentCheckboxPart)}.{nameof(PrivacyConsentCheckboxPart.ConsentCheckbox)}";
var form = _hca.HttpContext.Request.Form;
var consentCheckboxValue = form[consentCheckboxName].Select(value => bool.Parse(value));
var isValid = consentCheckboxValue != null && consentCheckboxValue.Contains(value: true);
var outcome = isValid ? "Valid" : "Invalid";

var consentCheckboxName = $"{nameof(PrivacyConsentCheckboxPart)}.{nameof(PrivacyConsentCheckboxPart.ConsentCheckbox)}";
var form = _hca.HttpContext.Request.Form;
var consentCheckboxValue = form[consentCheckboxName].Select(value => bool.Parse(value));
var isValid = consentCheckboxValue != null && consentCheckboxValue.Contains(value: true);
var outcome = isValid ? "Valid" : "Invalid";
if (!isValid)
{
var updater = _updateModelAccessor.ModelUpdater;

if (!isValid)
if (updater != null)
{
var updater = _updateModelAccessor.ModelUpdater;

if (updater != null)
{
updater.ModelState.TryAddModelError(
consentCheckboxName,
T["You have to accept the privacy policy."]);
}
updater.ModelState.TryAddModelError(
consentCheckboxName,
T["You have to accept the privacy policy."]);
}

return Outcomes("Done", outcome);
}

return Outcomes("Done", outcome);
}
}
15 changes: 7 additions & 8 deletions Constants/FeatureNames.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Lombiq.Privacy.Constants
namespace Lombiq.Privacy.Constants;

public static class FeatureNames
{
public static class FeatureNames
{
public const string Module = "Lombiq.Privacy";
public const string Module = "Lombiq.Privacy";

public const string ConsentBanner = Module + "." + nameof(ConsentBanner);
public const string RegistrationConsent = Module + "." + nameof(RegistrationConsent);
public const string FormConsent = Module + "." + nameof(FormConsent);
}
public const string ConsentBanner = Module + "." + nameof(ConsentBanner);
public const string RegistrationConsent = Module + "." + nameof(RegistrationConsent);
public const string FormConsent = Module + "." + nameof(FormConsent);
}
9 changes: 4 additions & 5 deletions Constants/GroupIds.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Lombiq.Privacy.Constants
namespace Lombiq.Privacy.Constants;

public static class GroupIds
{
public static class GroupIds
{
public const string PrivacySettings = nameof(PrivacySettings);
}
public const string PrivacySettings = nameof(PrivacySettings);
}
11 changes: 5 additions & 6 deletions Constants/ResourceNames.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Lombiq.Privacy.Constants
namespace Lombiq.Privacy.Constants;

public static class ResourceNames
{
public static class ResourceNames
{
private const string Prefix = "Lombiq.Privacy";
private const string Prefix = "Lombiq.Privacy";

public const string ConsentBanner = Prefix + "." + nameof(ConsentBanner);
}
public const string ConsentBanner = Prefix + "." + nameof(ConsentBanner);
}
15 changes: 7 additions & 8 deletions Constants/TypeNames.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace Lombiq.Privacy.Constants
namespace Lombiq.Privacy.Constants;

public static class TypeNames
{
public static class TypeNames
{
public const string PrivacyConsentCheckbox = nameof(PrivacyConsentCheckbox);
public const string PrivacyConsentBannerSettings = nameof(PrivacyConsentBannerSettings);
public const string PrivacyConsentCheckboxSettings = nameof(PrivacyConsentCheckboxSettings);
public const string PrivacyRegistrationConsentSettings = nameof(PrivacyRegistrationConsentSettings);
}
public const string PrivacyConsentCheckbox = nameof(PrivacyConsentCheckbox);
public const string PrivacyConsentBannerSettings = nameof(PrivacyConsentBannerSettings);
public const string PrivacyConsentCheckboxSettings = nameof(PrivacyConsentCheckboxSettings);
public const string PrivacyRegistrationConsentSettings = nameof(PrivacyRegistrationConsentSettings);
}
27 changes: 13 additions & 14 deletions Controllers/PrivacyConsentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Lombiq.Privacy.Controllers
namespace Lombiq.Privacy.Controllers;

public class PrivacyConsentController : Controller
{
public class PrivacyConsentController : Controller
{
private readonly IPrivacyConsentService _consentService;
private readonly IPrivacyConsentService _consentService;

public PrivacyConsentController(IPrivacyConsentService consentService) => _consentService = consentService;
public PrivacyConsentController(IPrivacyConsentService consentService) => _consentService = consentService;

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AcceptanceOfConsent()
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AcceptanceOfConsent()
{
if (!await _consentService.IsUserAcceptedConsentAsync(ControllerContext.HttpContext))
{
if (!await _consentService.IsUserAcceptedConsentAsync(ControllerContext.HttpContext))
{
await _consentService.StoreUserConsentAsync(User);
}

return Ok();
await _consentService.StoreUserConsentAsync(User);
}

return Ok();
}
}
39 changes: 19 additions & 20 deletions Drivers/PrivacyConsentCheckboxPartDisplayDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@
using OrchardCore.DisplayManagement.Views;
using System.Threading.Tasks;

namespace Lombiq.Privacy.Drivers
namespace Lombiq.Privacy.Drivers;

public class PrivacyConsentCheckboxPartDisplayDriver : ContentPartDisplayDriver<PrivacyConsentCheckboxPart>
{
public class PrivacyConsentCheckboxPartDisplayDriver : ContentPartDisplayDriver<PrivacyConsentCheckboxPart>
{
private readonly IPrivacyConsentService _consentService;
private readonly IHttpContextAccessor _hca;
private readonly IPrivacyConsentService _consentService;
private readonly IHttpContextAccessor _hca;

public PrivacyConsentCheckboxPartDisplayDriver(IPrivacyConsentService consentService, IHttpContextAccessor hca)
{
_consentService = consentService;
_hca = hca;
}
public PrivacyConsentCheckboxPartDisplayDriver(IPrivacyConsentService consentService, IHttpContextAccessor hca)
{
_consentService = consentService;
_hca = hca;
}

public override async Task<IDisplayResult> DisplayAsync(PrivacyConsentCheckboxPart part, BuildPartDisplayContext context) =>
// If the user has already accepted the privacy statement, it doesn't need to display the checkbox.
!await _consentService.IsUserAcceptedConsentAsync(_hca.HttpContext)
? Initialize<PrivacyConsentCheckboxPartViewModel>(
GetDisplayShapeType(context),
viewModel => viewModel.ConsentCheckbox = part.ConsentCheckbox).Location("Detail", "Content")
: null;
public override async Task<IDisplayResult> DisplayAsync(PrivacyConsentCheckboxPart part, BuildPartDisplayContext context) =>
// If the user has already accepted the privacy statement, it doesn't need to display the checkbox.
!await _consentService.IsUserAcceptedConsentAsync(_hca.HttpContext)
? Initialize<PrivacyConsentCheckboxPartViewModel>(
GetDisplayShapeType(context),
viewModel => viewModel.ConsentCheckbox = part.ConsentCheckbox).Location("Detail", "Content")
: null;

public override IDisplayResult Edit(PrivacyConsentCheckboxPart part, BuildPartEditorContext context) =>
View(GetEditorShapeType(context), part);
}
public override IDisplayResult Edit(PrivacyConsentCheckboxPart part, BuildPartEditorContext context) =>
View(GetEditorShapeType(context), part);
}
7 changes: 3 additions & 4 deletions Drivers/ValidatePrivacyConsentCheckboxTaskDisplayDriver.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Lombiq.Privacy.Activities;
using OrchardCore.Workflows.Display;

namespace Lombiq.Privacy.Drivers
namespace Lombiq.Privacy.Drivers;

public class ValidatePrivacyConsentCheckboxTaskDisplayDriver : ActivityDisplayDriver<ValidatePrivacyConsentCheckboxTask>
{
public class ValidatePrivacyConsentCheckboxTaskDisplayDriver : ActivityDisplayDriver<ValidatePrivacyConsentCheckboxTask>
{
}
}
55 changes: 27 additions & 28 deletions Filters/PrivacyConsentBannerInjectionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,39 @@
using OrchardCore.DisplayManagement.Layout;
using System.Threading.Tasks;

namespace Lombiq.Privacy.Filters
namespace Lombiq.Privacy.Filters;

public class PrivacyConsentBannerInjectionFilter : IAsyncResultFilter
{
public class PrivacyConsentBannerInjectionFilter : IAsyncResultFilter
private readonly ILayoutAccessor _layoutAccessor;
private readonly IShapeFactory _shapeFactory;
private readonly IPrivacyConsentService _consentService;
private readonly IHttpContextAccessor _hca;

public PrivacyConsentBannerInjectionFilter(
ILayoutAccessor layoutAccessor,
IShapeFactory shapeFactory,
IPrivacyConsentService consentService,
IHttpContextAccessor hca)
{
private readonly ILayoutAccessor _layoutAccessor;
private readonly IShapeFactory _shapeFactory;
private readonly IPrivacyConsentService _consentService;
private readonly IHttpContextAccessor _hca;
_layoutAccessor = layoutAccessor;
_shapeFactory = shapeFactory;
_consentService = consentService;
_hca = hca;
}

public PrivacyConsentBannerInjectionFilter(
ILayoutAccessor layoutAccessor,
IShapeFactory shapeFactory,
IPrivacyConsentService consentService,
IHttpContextAccessor hca)
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (context.IsNotFullViewRendering() || !await _consentService.IsConsentBannerNeededAsync(_hca.HttpContext))
{
_layoutAccessor = layoutAccessor;
_shapeFactory = shapeFactory;
_consentService = consentService;
_hca = hca;
await next();
return;
}

public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (context.IsNotFullViewRendering() || !await _consentService.IsConsentBannerNeededAsync(_hca.HttpContext))
{
await next();
return;
}

var layout = await _layoutAccessor.GetLayoutAsync();
var contentZone = layout.Zones["Content"];
await contentZone.AddAsync(await _shapeFactory.CreateAsync("Lombiq_Privacy_ConsentBanner"));
var layout = await _layoutAccessor.GetLayoutAsync();
var contentZone = layout.Zones["Content"];
await contentZone.AddAsync(await _shapeFactory.CreateAsync("Lombiq_Privacy_ConsentBanner"));

await next();
}
await next();
}
}
Loading

0 comments on commit babbc27

Please sign in to comment.