diff --git a/Activities/ValidatePrivacyConsentCheckboxTask.cs b/Activities/ValidatePrivacyConsentCheckboxTask.cs index 992856a..516ec50 100644 --- a/Activities/ValidatePrivacyConsentCheckboxTask.cs +++ b/Activities/ValidatePrivacyConsentCheckboxTask.cs @@ -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 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 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 GetPossibleOutcomes( + WorkflowExecutionContext workflowContext, + ActivityContext activityContext) => + Outcomes(T["Done"], T["Valid"], T["Invalid"]); - public override IEnumerable GetPossibleOutcomes( - WorkflowExecutionContext workflowContext, - ActivityContext activityContext) => - Outcomes(T["Done"], T["Valid"], T["Invalid"]); + public override async Task 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 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); } } diff --git a/Constants/FeatureNames.cs b/Constants/FeatureNames.cs index 8437259..2ea7fd5 100644 --- a/Constants/FeatureNames.cs +++ b/Constants/FeatureNames.cs @@ -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); } diff --git a/Constants/GroupIds.cs b/Constants/GroupIds.cs index 0e91697..29914ca 100644 --- a/Constants/GroupIds.cs +++ b/Constants/GroupIds.cs @@ -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); } diff --git a/Constants/ResourceNames.cs b/Constants/ResourceNames.cs index aa2ccbf..3bb22c4 100644 --- a/Constants/ResourceNames.cs +++ b/Constants/ResourceNames.cs @@ -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); } diff --git a/Constants/TypeNames.cs b/Constants/TypeNames.cs index 742f631..7414953 100644 --- a/Constants/TypeNames.cs +++ b/Constants/TypeNames.cs @@ -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); } diff --git a/Controllers/PrivacyConsentController.cs b/Controllers/PrivacyConsentController.cs index e78a156..7b59b62 100644 --- a/Controllers/PrivacyConsentController.cs +++ b/Controllers/PrivacyConsentController.cs @@ -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 AcceptanceOfConsent() + [HttpPost] + [ValidateAntiForgeryToken] + public async Task 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(); } } diff --git a/Drivers/PrivacyConsentCheckboxPartDisplayDriver.cs b/Drivers/PrivacyConsentCheckboxPartDisplayDriver.cs index dda1c9e..2f14406 100644 --- a/Drivers/PrivacyConsentCheckboxPartDisplayDriver.cs +++ b/Drivers/PrivacyConsentCheckboxPartDisplayDriver.cs @@ -7,28 +7,27 @@ using OrchardCore.DisplayManagement.Views; using System.Threading.Tasks; -namespace Lombiq.Privacy.Drivers +namespace Lombiq.Privacy.Drivers; + +public class PrivacyConsentCheckboxPartDisplayDriver : ContentPartDisplayDriver { - public class PrivacyConsentCheckboxPartDisplayDriver : ContentPartDisplayDriver - { - 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 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( - GetDisplayShapeType(context), - viewModel => viewModel.ConsentCheckbox = part.ConsentCheckbox).Location("Detail", "Content") - : null; + public override async Task 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( + 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); } diff --git a/Drivers/ValidatePrivacyConsentCheckboxTaskDisplayDriver.cs b/Drivers/ValidatePrivacyConsentCheckboxTaskDisplayDriver.cs index eb13323..0bc0bbf 100644 --- a/Drivers/ValidatePrivacyConsentCheckboxTaskDisplayDriver.cs +++ b/Drivers/ValidatePrivacyConsentCheckboxTaskDisplayDriver.cs @@ -1,9 +1,8 @@ using Lombiq.Privacy.Activities; using OrchardCore.Workflows.Display; -namespace Lombiq.Privacy.Drivers +namespace Lombiq.Privacy.Drivers; + +public class ValidatePrivacyConsentCheckboxTaskDisplayDriver : ActivityDisplayDriver { - public class ValidatePrivacyConsentCheckboxTaskDisplayDriver : ActivityDisplayDriver - { - } } diff --git a/Filters/PrivacyConsentBannerInjectionFilter.cs b/Filters/PrivacyConsentBannerInjectionFilter.cs index 7f119c4..c10a58c 100644 --- a/Filters/PrivacyConsentBannerInjectionFilter.cs +++ b/Filters/PrivacyConsentBannerInjectionFilter.cs @@ -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(); } } diff --git a/Filters/RegistrationCheckboxInjectionFilter.cs b/Filters/RegistrationCheckboxInjectionFilter.cs index bd3c04a..032e4bd 100644 --- a/Filters/RegistrationCheckboxInjectionFilter.cs +++ b/Filters/RegistrationCheckboxInjectionFilter.cs @@ -7,42 +7,41 @@ using System; using System.Threading.Tasks; -namespace Lombiq.Privacy.Filters +namespace Lombiq.Privacy.Filters; + +public class RegistrationCheckboxInjectionFilter : IAsyncResultFilter { - public class RegistrationCheckboxInjectionFilter : IAsyncResultFilter + private readonly ILayoutAccessor _layoutAccessor; + private readonly IShapeFactory _shapeFactory; + + public RegistrationCheckboxInjectionFilter( + ILayoutAccessor layoutAccessor, + IShapeFactory shapeFactory) { - private readonly ILayoutAccessor _layoutAccessor; - private readonly IShapeFactory _shapeFactory; + _layoutAccessor = layoutAccessor; + _shapeFactory = shapeFactory; + } - public RegistrationCheckboxInjectionFilter( - ILayoutAccessor layoutAccessor, - IShapeFactory shapeFactory) + public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) + { + var routeValues = context.ActionDescriptor.RouteValues; + if (context.IsNotFullViewRendering() || + !routeValues["Area"].EqualsOrdinalIgnoreCase($"{nameof(OrchardCore)}.{nameof(OrchardCore.Users)}") || + !routeValues["Controller"].EqualsOrdinalIgnoreCase(typeof(RegistrationController).ControllerName()) || + !routeValues["Action"].EqualsOrdinalIgnoreCase(nameof(RegistrationController.Register))) { - _layoutAccessor = layoutAccessor; - _shapeFactory = shapeFactory; + await next(); + return; } - public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) - { - var routeValues = context.ActionDescriptor.RouteValues; - if (context.IsNotFullViewRendering() || - !routeValues["Area"].EqualsOrdinalIgnoreCase($"{nameof(OrchardCore)}.{nameof(OrchardCore.Users)}") || - !routeValues["Controller"].EqualsOrdinalIgnoreCase(typeof(RegistrationController).ControllerName()) || - !routeValues["Action"].EqualsOrdinalIgnoreCase(nameof(RegistrationController.Register))) - { - await next(); - return; - } - - var layout = await _layoutAccessor.GetLayoutAsync(); - var afterRegisterZone = layout.Zones["AfterRegister"]; - var shape = await _shapeFactory.CreateAsync( - "Lombiq_Privacy_RegistrationCheckbox", - viewModel => viewModel.RegistrationCheckbox = false); + var layout = await _layoutAccessor.GetLayoutAsync(); + var afterRegisterZone = layout.Zones["AfterRegister"]; + var shape = await _shapeFactory.CreateAsync( + "Lombiq_Privacy_RegistrationCheckbox", + viewModel => viewModel.RegistrationCheckbox = false); - await afterRegisterZone.AddAsync(shape); + await afterRegisterZone.AddAsync(shape); - await next(); - } + await next(); } } diff --git a/Handlers/RegistrationFormEventHandler.cs b/Handlers/RegistrationFormEventHandler.cs index 0fb1e1c..ffa8812 100644 --- a/Handlers/RegistrationFormEventHandler.cs +++ b/Handlers/RegistrationFormEventHandler.cs @@ -8,39 +8,38 @@ using System.Linq; using System.Threading.Tasks; -namespace Lombiq.Privacy.Handlers +namespace Lombiq.Privacy.Handlers; + +public class RegistrationFormEventHandler : IRegistrationFormEvents { - public class RegistrationFormEventHandler : IRegistrationFormEvents + private readonly IHttpContextAccessor _hca; + private readonly IStringLocalizer T; + private readonly IPrivacyConsentService _consentService; + + public RegistrationFormEventHandler( + IHttpContextAccessor hca, + IStringLocalizer stringLocalizer, + IPrivacyConsentService consentService) { - private readonly IHttpContextAccessor _hca; - private readonly IStringLocalizer T; - private readonly IPrivacyConsentService _consentService; + _hca = hca; + T = stringLocalizer; + _consentService = consentService; + } - public RegistrationFormEventHandler( - IHttpContextAccessor hca, - IStringLocalizer stringLocalizer, - IPrivacyConsentService consentService) - { - _hca = hca; - T = stringLocalizer; - _consentService = consentService; - } + public Task RegisteredAsync(IUser user) => _consentService.StoreUserConsentAsync(user); - public Task RegisteredAsync(IUser user) => _consentService.StoreUserConsentAsync(user); + public Task RegistrationValidationAsync(Action reportError) + { + var registrationCheckbox = _hca.HttpContext?.Request?.Form?[nameof(PrivacyRegistrationConsentCheckboxViewModel.RegistrationCheckbox)] + .Select(value => bool.Parse(value)); - public Task RegistrationValidationAsync(Action reportError) + if (registrationCheckbox == null || !registrationCheckbox.Contains(value: true)) { - var registrationCheckbox = _hca.HttpContext?.Request?.Form?[nameof(PrivacyRegistrationConsentCheckboxViewModel.RegistrationCheckbox)] - .Select(value => bool.Parse(value)); - - if (registrationCheckbox == null || !registrationCheckbox.Contains(value: true)) - { - reportError( - nameof(PrivacyRegistrationConsentCheckboxViewModel.RegistrationCheckbox), - T["You have to accept the privacy policy."]); - } - - return Task.CompletedTask; + reportError( + nameof(PrivacyRegistrationConsentCheckboxViewModel.RegistrationCheckbox), + T["You have to accept the privacy policy."]); } + + return Task.CompletedTask; } } diff --git a/Lombiq.Privacy.csproj b/Lombiq.Privacy.csproj index 0243aea..c27e051 100644 --- a/Lombiq.Privacy.csproj +++ b/Lombiq.Privacy.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net6.0 true $(DefaultItemExcludes);.git*;node_modules\**;Tests\** @@ -29,15 +29,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/Migrations/PrivacyConsentBannerSettingsMigrations.cs b/Migrations/PrivacyConsentBannerSettingsMigrations.cs index 5d1548d..835ab73 100644 --- a/Migrations/PrivacyConsentBannerSettingsMigrations.cs +++ b/Migrations/PrivacyConsentBannerSettingsMigrations.cs @@ -6,30 +6,29 @@ using System.Threading.Tasks; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Migrations +namespace Lombiq.Privacy.Migrations; + +public class PrivacyConsentBannerSettingsMigrations : DataMigration { - public class PrivacyConsentBannerSettingsMigrations : DataMigration - { - private readonly IContentDefinitionManager _contentDefinitionManager; - private readonly IRecipeMigrator _recipeMigrator; + private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly IRecipeMigrator _recipeMigrator; - public PrivacyConsentBannerSettingsMigrations( - IContentDefinitionManager contentDefinitionManager, - IRecipeMigrator recipeMigrator) - { - _contentDefinitionManager = contentDefinitionManager; - _recipeMigrator = recipeMigrator; - } + public PrivacyConsentBannerSettingsMigrations( + IContentDefinitionManager contentDefinitionManager, + IRecipeMigrator recipeMigrator) + { + _contentDefinitionManager = contentDefinitionManager; + _recipeMigrator = recipeMigrator; + } - public async Task CreateAsync() - { - _contentDefinitionManager.AlterTypeDefinition(PrivacyConsentBannerSettings, type => type - .WithPart(nameof(LiquidPart)) - .Stereotype("CustomSettings")); + public async Task CreateAsync() + { + _contentDefinitionManager.AlterTypeDefinition(PrivacyConsentBannerSettings, type => type + .WithPart(nameof(LiquidPart)) + .Stereotype("CustomSettings")); - await _recipeMigrator.ExecuteAsync("Recipes/PrivacyConsentBannerSettings.recipe.json", this); + await _recipeMigrator.ExecuteAsync("Recipes/PrivacyConsentBannerSettings.recipe.json", this); - return 1; - } + return 1; } } diff --git a/Migrations/PrivacyConsentCheckboxMigrations.cs b/Migrations/PrivacyConsentCheckboxMigrations.cs index f5f596f..1f7dd1e 100644 --- a/Migrations/PrivacyConsentCheckboxMigrations.cs +++ b/Migrations/PrivacyConsentCheckboxMigrations.cs @@ -4,25 +4,24 @@ using OrchardCore.Data.Migration; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Migrations +namespace Lombiq.Privacy.Migrations; + +public class PrivacyConsentCheckboxMigrations : DataMigration { - public class PrivacyConsentCheckboxMigrations : DataMigration - { - private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly IContentDefinitionManager _contentDefinitionManager; - public PrivacyConsentCheckboxMigrations(IContentDefinitionManager contentDefinitionManager) => - _contentDefinitionManager = contentDefinitionManager; + public PrivacyConsentCheckboxMigrations(IContentDefinitionManager contentDefinitionManager) => + _contentDefinitionManager = contentDefinitionManager; - public int Create() - { - _contentDefinitionManager.AlterPartDefinition(nameof(PrivacyConsentCheckboxPart), part => part - .WithDescription("Provides privacy consent checkbox properties.")); + public int Create() + { + _contentDefinitionManager.AlterPartDefinition(nameof(PrivacyConsentCheckboxPart), part => part + .WithDescription("Provides privacy consent checkbox properties.")); - _contentDefinitionManager.AlterTypeDefinition(PrivacyConsentCheckbox, type => type - .WithPart(nameof(PrivacyConsentCheckboxPart)) - .Stereotype("Widget")); + _contentDefinitionManager.AlterTypeDefinition(PrivacyConsentCheckbox, type => type + .WithPart(nameof(PrivacyConsentCheckboxPart)) + .Stereotype("Widget")); - return 1; - } + return 1; } } diff --git a/Migrations/PrivacyConsentCheckboxSettingsMigrations.cs b/Migrations/PrivacyConsentCheckboxSettingsMigrations.cs index 64b3d07..0d14646 100644 --- a/Migrations/PrivacyConsentCheckboxSettingsMigrations.cs +++ b/Migrations/PrivacyConsentCheckboxSettingsMigrations.cs @@ -6,30 +6,29 @@ using System.Threading.Tasks; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Migrations +namespace Lombiq.Privacy.Migrations; + +public class PrivacyConsentCheckboxSettingsMigrations : DataMigration { - public class PrivacyConsentCheckboxSettingsMigrations : DataMigration - { - private readonly IContentDefinitionManager _contentDefinitionManager; - private readonly IRecipeMigrator _recipeMigrator; + private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly IRecipeMigrator _recipeMigrator; - public PrivacyConsentCheckboxSettingsMigrations( - IContentDefinitionManager contentDefinitionManager, - IRecipeMigrator recipeMigrator) - { - _contentDefinitionManager = contentDefinitionManager; - _recipeMigrator = recipeMigrator; - } + public PrivacyConsentCheckboxSettingsMigrations( + IContentDefinitionManager contentDefinitionManager, + IRecipeMigrator recipeMigrator) + { + _contentDefinitionManager = contentDefinitionManager; + _recipeMigrator = recipeMigrator; + } - public async Task CreateAsync() - { - _contentDefinitionManager.AlterTypeDefinition(PrivacyConsentCheckboxSettings, type => type - .WithPart(nameof(LiquidPart)) - .Stereotype("CustomSettings")); + public async Task CreateAsync() + { + _contentDefinitionManager.AlterTypeDefinition(PrivacyConsentCheckboxSettings, type => type + .WithPart(nameof(LiquidPart)) + .Stereotype("CustomSettings")); - await _recipeMigrator.ExecuteAsync("Recipes/PrivacyConsentCheckboxSettings.recipe.json", this); + await _recipeMigrator.ExecuteAsync("Recipes/PrivacyConsentCheckboxSettings.recipe.json", this); - return 1; - } + return 1; } } diff --git a/Migrations/PrivacyRegistrationConsentSettingsMigrations.cs b/Migrations/PrivacyRegistrationConsentSettingsMigrations.cs index 4fb2fcf..747aceb 100644 --- a/Migrations/PrivacyRegistrationConsentSettingsMigrations.cs +++ b/Migrations/PrivacyRegistrationConsentSettingsMigrations.cs @@ -6,30 +6,29 @@ using System.Threading.Tasks; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Migrations +namespace Lombiq.Privacy.Migrations; + +public class PrivacyRegistrationConsentSettingsMigrations : DataMigration { - public class PrivacyRegistrationConsentSettingsMigrations : DataMigration - { - private readonly IContentDefinitionManager _contentDefinitionManager; - private readonly IRecipeMigrator _recipeMigrator; + private readonly IContentDefinitionManager _contentDefinitionManager; + private readonly IRecipeMigrator _recipeMigrator; - public PrivacyRegistrationConsentSettingsMigrations( - IContentDefinitionManager contentDefinitionManager, - IRecipeMigrator recipeMigrator) - { - _contentDefinitionManager = contentDefinitionManager; - _recipeMigrator = recipeMigrator; - } + public PrivacyRegistrationConsentSettingsMigrations( + IContentDefinitionManager contentDefinitionManager, + IRecipeMigrator recipeMigrator) + { + _contentDefinitionManager = contentDefinitionManager; + _recipeMigrator = recipeMigrator; + } - public async Task CreateAsync() - { - _contentDefinitionManager.AlterTypeDefinition(PrivacyRegistrationConsentSettings, type => type - .WithPart(nameof(LiquidPart)) - .Stereotype("CustomSettings")); + public async Task CreateAsync() + { + _contentDefinitionManager.AlterTypeDefinition(PrivacyRegistrationConsentSettings, type => type + .WithPart(nameof(LiquidPart)) + .Stereotype("CustomSettings")); - await _recipeMigrator.ExecuteAsync("Recipes/PrivacyRegistrationConsentSettings.recipe.json", this); + await _recipeMigrator.ExecuteAsync("Recipes/PrivacyRegistrationConsentSettings.recipe.json", this); - return 1; - } + return 1; } } diff --git a/Models/PrivacyConsent.cs b/Models/PrivacyConsent.cs index 682dc04..7e41c65 100644 --- a/Models/PrivacyConsent.cs +++ b/Models/PrivacyConsent.cs @@ -1,7 +1,6 @@ -namespace Lombiq.Privacy.Models +namespace Lombiq.Privacy.Models; + +public class PrivacyConsent { - public class PrivacyConsent - { - public bool Accepted { get; set; } - } + public bool Accepted { get; set; } } diff --git a/Models/PrivacyConsentCheckboxPart.cs b/Models/PrivacyConsentCheckboxPart.cs index 2619133..561655c 100644 --- a/Models/PrivacyConsentCheckboxPart.cs +++ b/Models/PrivacyConsentCheckboxPart.cs @@ -1,9 +1,8 @@ using OrchardCore.ContentManagement; -namespace Lombiq.Privacy.Models +namespace Lombiq.Privacy.Models; + +public class PrivacyConsentCheckboxPart : ContentPart { - public class PrivacyConsentCheckboxPart : ContentPart - { - public bool ConsentCheckbox { get; set; } - } + public bool ConsentCheckbox { get; set; } } diff --git a/Navigation/PrivacyConsentBannerSettingsMenu.cs b/Navigation/PrivacyConsentBannerSettingsMenu.cs index 9367cb1..c716b69 100644 --- a/Navigation/PrivacyConsentBannerSettingsMenu.cs +++ b/Navigation/PrivacyConsentBannerSettingsMenu.cs @@ -6,45 +6,44 @@ using System.Threading.Tasks; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Navigation +namespace Lombiq.Privacy.Navigation; + +public class PrivacyConsentBannerSettingsMenu : INavigationProvider { - public class PrivacyConsentBannerSettingsMenu : INavigationProvider + private readonly CustomSettingsService _customSettingsService; + private readonly IStringLocalizer T; + + public PrivacyConsentBannerSettingsMenu( + IStringLocalizer localizer, + CustomSettingsService customSettingsService) { - private readonly CustomSettingsService _customSettingsService; - private readonly IStringLocalizer T; + T = localizer; + _customSettingsService = customSettingsService; + } - public PrivacyConsentBannerSettingsMenu( - IStringLocalizer localizer, - CustomSettingsService customSettingsService) + public Task BuildNavigationAsync(string name, NavigationBuilder builder) + { + if (!name.EqualsOrdinalIgnoreCase("admin")) { - T = localizer; - _customSettingsService = customSettingsService; + return Task.CompletedTask; } - public Task BuildNavigationAsync(string name, NavigationBuilder builder) - { - if (!name.EqualsOrdinalIgnoreCase("admin")) - { - return Task.CompletedTask; - } + var type = _customSettingsService.GetSettingsType(PrivacyConsentBannerSettings); - var type = _customSettingsService.GetSettingsType(PrivacyConsentBannerSettings); - - if (type != null) - { - builder - .Add(T["Configuration"], configuration => configuration - .Add(T["Settings"], settings => settings - .Add(new LocalizedString(type.DisplayName, type.DisplayName), type.DisplayName.PrefixPosition(), layers => layers - .Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = type.Name }) - .Permission(Permissions.CreatePermissionForType(type)) - .Resource(type.Name) - .AddClass(type.Name) - .Id(type.Name) - .LocalNav()))); - } - - return Task.CompletedTask; + if (type != null) + { + builder + .Add(T["Configuration"], configuration => configuration + .Add(T["Settings"], settings => settings + .Add(new LocalizedString(type.DisplayName, type.DisplayName), type.DisplayName.PrefixPosition(), layers => layers + .Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = type.Name }) + .Permission(Permissions.CreatePermissionForType(type)) + .Resource(type.Name) + .AddClass(type.Name) + .Id(type.Name) + .LocalNav()))); } + + return Task.CompletedTask; } } diff --git a/Navigation/PrivacyConsentCheckboxSettingsMenu.cs b/Navigation/PrivacyConsentCheckboxSettingsMenu.cs index e5f59a1..3c9fdae 100644 --- a/Navigation/PrivacyConsentCheckboxSettingsMenu.cs +++ b/Navigation/PrivacyConsentCheckboxSettingsMenu.cs @@ -6,45 +6,44 @@ using System.Threading.Tasks; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Navigation +namespace Lombiq.Privacy.Navigation; + +public class PrivacyConsentCheckboxSettingsMenu : INavigationProvider { - public class PrivacyConsentCheckboxSettingsMenu : INavigationProvider + private readonly CustomSettingsService _customSettingsService; + private readonly IStringLocalizer T; + + public PrivacyConsentCheckboxSettingsMenu( + IStringLocalizer localizer, + CustomSettingsService customSettingsService) { - private readonly CustomSettingsService _customSettingsService; - private readonly IStringLocalizer T; + T = localizer; + _customSettingsService = customSettingsService; + } - public PrivacyConsentCheckboxSettingsMenu( - IStringLocalizer localizer, - CustomSettingsService customSettingsService) + public Task BuildNavigationAsync(string name, NavigationBuilder builder) + { + if (!name.EqualsOrdinalIgnoreCase("admin")) { - T = localizer; - _customSettingsService = customSettingsService; + return Task.CompletedTask; } - public Task BuildNavigationAsync(string name, NavigationBuilder builder) - { - if (!name.EqualsOrdinalIgnoreCase("admin")) - { - return Task.CompletedTask; - } + var type = _customSettingsService.GetSettingsType(PrivacyConsentCheckboxSettings); - var type = _customSettingsService.GetSettingsType(PrivacyConsentCheckboxSettings); - - if (type != null) - { - builder - .Add(T["Configuration"], configuration => configuration - .Add(T["Settings"], settings => settings - .Add(new LocalizedString(type.DisplayName, type.DisplayName), type.DisplayName.PrefixPosition(), layers => layers - .Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = type.Name }) - .Permission(Permissions.CreatePermissionForType(type)) - .Resource(type.Name) - .AddClass(type.Name) - .Id(type.Name) - .LocalNav()))); - } - - return Task.CompletedTask; + if (type != null) + { + builder + .Add(T["Configuration"], configuration => configuration + .Add(T["Settings"], settings => settings + .Add(new LocalizedString(type.DisplayName, type.DisplayName), type.DisplayName.PrefixPosition(), layers => layers + .Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = type.Name }) + .Permission(Permissions.CreatePermissionForType(type)) + .Resource(type.Name) + .AddClass(type.Name) + .Id(type.Name) + .LocalNav()))); } + + return Task.CompletedTask; } } diff --git a/Navigation/PrivacyRegistrationConsentSettingsMenu.cs b/Navigation/PrivacyRegistrationConsentSettingsMenu.cs index 098d64e..f8d80d7 100644 --- a/Navigation/PrivacyRegistrationConsentSettingsMenu.cs +++ b/Navigation/PrivacyRegistrationConsentSettingsMenu.cs @@ -6,45 +6,44 @@ using System.Threading.Tasks; using static Lombiq.Privacy.Constants.TypeNames; -namespace Lombiq.Privacy.Navigation +namespace Lombiq.Privacy.Navigation; + +public class PrivacyRegistrationConsentSettingsMenu : INavigationProvider { - public class PrivacyRegistrationConsentSettingsMenu : INavigationProvider + private readonly CustomSettingsService _customSettingsService; + private readonly IStringLocalizer T; + + public PrivacyRegistrationConsentSettingsMenu( + IStringLocalizer localizer, + CustomSettingsService customSettingsService) { - private readonly CustomSettingsService _customSettingsService; - private readonly IStringLocalizer T; + T = localizer; + _customSettingsService = customSettingsService; + } - public PrivacyRegistrationConsentSettingsMenu( - IStringLocalizer localizer, - CustomSettingsService customSettingsService) + public Task BuildNavigationAsync(string name, NavigationBuilder builder) + { + if (!name.EqualsOrdinalIgnoreCase("admin")) { - T = localizer; - _customSettingsService = customSettingsService; + return Task.CompletedTask; } - public Task BuildNavigationAsync(string name, NavigationBuilder builder) - { - if (!name.EqualsOrdinalIgnoreCase("admin")) - { - return Task.CompletedTask; - } + var type = _customSettingsService.GetSettingsType(PrivacyRegistrationConsentSettings); - var type = _customSettingsService.GetSettingsType(PrivacyRegistrationConsentSettings); - - if (type != null) - { - builder - .Add(T["Configuration"], configuration => configuration - .Add(T["Settings"], settings => settings - .Add(new LocalizedString(type.DisplayName, type.DisplayName), type.DisplayName.PrefixPosition(), layers => layers - .Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = type.Name }) - .Permission(Permissions.CreatePermissionForType(type)) - .Resource(type.Name) - .AddClass(type.Name) - .Id(type.Name) - .LocalNav()))); - } - - return Task.CompletedTask; + if (type != null) + { + builder + .Add(T["Configuration"], configuration => configuration + .Add(T["Settings"], settings => settings + .Add(new LocalizedString(type.DisplayName, type.DisplayName), type.DisplayName.PrefixPosition(), layers => layers + .Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = type.Name }) + .Permission(Permissions.CreatePermissionForType(type)) + .Resource(type.Name) + .AddClass(type.Name) + .Id(type.Name) + .LocalNav()))); } + + return Task.CompletedTask; } } diff --git a/ResourceManagementOptionsConfiguration.cs b/ResourceManagementOptionsConfiguration.cs index ea725b0..40918f1 100644 --- a/ResourceManagementOptionsConfiguration.cs +++ b/ResourceManagementOptionsConfiguration.cs @@ -2,19 +2,18 @@ using OrchardCore.ResourceManagement; using static Lombiq.Privacy.Constants.ResourceNames; -namespace Lombiq.Privacy +namespace Lombiq.Privacy; + +public class ResourceManagementOptionsConfiguration : IConfigureOptions { - public class ResourceManagementOptionsConfiguration : IConfigureOptions - { - private static readonly ResourceManifest _manifest = new(); + private static readonly ResourceManifest _manifest = new(); - static ResourceManagementOptionsConfiguration() => - _manifest - .DefineStyle(ConsentBanner) - .SetUrl( - "~/Lombiq.Privacy/css/lombiq-privacy-consent-banner.min.css", - "~/Lombiq.Privacy/css/lombiq-privacy-consent-banner.css"); + static ResourceManagementOptionsConfiguration() => + _manifest + .DefineStyle(ConsentBanner) + .SetUrl( + "~/Lombiq.Privacy/css/lombiq-privacy-consent-banner.min.css", + "~/Lombiq.Privacy/css/lombiq-privacy-consent-banner.css"); - public void Configure(ResourceManagementOptions options) => options.ResourceManifests.Add(_manifest); - } + public void Configure(ResourceManagementOptions options) => options.ResourceManifests.Add(_manifest); } diff --git a/Services/IPrivacyConsentService.cs b/Services/IPrivacyConsentService.cs index 46be799..1ac3d27 100644 --- a/Services/IPrivacyConsentService.cs +++ b/Services/IPrivacyConsentService.cs @@ -4,55 +4,51 @@ using System.Security.Claims; using System.Threading.Tasks; -namespace Lombiq.Privacy.Services +namespace Lombiq.Privacy.Services; + +/// +/// Service to manage user privacy consent. +/// +public interface IPrivacyConsentService { /// - /// Service to manage user privacy consent. + /// Decides whether the consent banner should be displayed based on the configuration of the and whether the user has already accepted the privacy statement. /// - public interface IPrivacyConsentService - { - /// - /// Decides whether the consent banner should be displayed based on the configuration of the - /// and whether the user has already accepted the privacy statement. - /// - /// The current HTTP context. - /// - /// if it needs to display the banner, otherwise. - /// - Task IsConsentBannerNeededAsync(HttpContext httpContext); + /// The current HTTP context. + /// if it needs to display the banner, otherwise. + Task IsConsentBannerNeededAsync(HttpContext httpContext); - /// - /// Decides whether in the current HTTP context it is required to accept this privacy statement. - /// - /// The current HTTP context. - /// - /// if the consent must be accepted in the current context, - /// otherwise. - /// - Task IsConsentNeededAsync(HttpContext httpContext); + /// + /// Decides whether in the current HTTP context it is required to accept this privacy statement. + /// + /// The current HTTP context. + /// + /// if the consent must be accepted in the current context, + /// otherwise. + /// + Task IsConsentNeededAsync(HttpContext httpContext); - /// - /// Decides whether the user already accepts the privacy policy in the given context. - /// - /// The current HTTP context. - /// - /// if the user already accepted the privacy consent, otherwise. - /// - Task IsUserAcceptedConsentAsync(HttpContext httpContext); + /// + /// Decides whether the user already accepts the privacy policy in the given context. + /// + /// The current HTTP context. + /// + /// if the user already accepted the privacy consent, otherwise. + /// + Task IsUserAcceptedConsentAsync(HttpContext httpContext); - /// - /// Stores the user's acceptance of a privacy statement. - /// - /// The current user. - /// A task that represents the asynchronous operation. - Task StoreUserConsentAsync(ClaimsPrincipal user); + /// + /// Stores the user's acceptance of a privacy statement. + /// + /// The current user. + /// A task that represents the asynchronous operation. + Task StoreUserConsentAsync(ClaimsPrincipal user); - /// - /// Stores the user's acceptance of a privacy statement. - /// - /// The current user. - /// A task that represents the asynchronous - /// operation. - Task StoreUserConsentAsync(IUser user); - } + /// + /// Stores the user's acceptance of a privacy statement. + /// + /// The current user. + /// A task that represents the asynchronous operation. + Task StoreUserConsentAsync(IUser user); } diff --git a/Services/PrivacyConsentService.cs b/Services/PrivacyConsentService.cs index 544dd9f..55d801f 100644 --- a/Services/PrivacyConsentService.cs +++ b/Services/PrivacyConsentService.cs @@ -12,79 +12,78 @@ using System.Security.Claims; using System.Threading.Tasks; -namespace Lombiq.Privacy.Services +namespace Lombiq.Privacy.Services; + +public class PrivacyConsentService : IPrivacyConsentService { - public class PrivacyConsentService : IPrivacyConsentService - { - private readonly UserManager _userManager; - private readonly IUserService _userService; + private readonly UserManager _userManager; + private readonly IUserService _userService; + + private readonly IOptions _cookiePolicyOptions; - private readonly IOptions _cookiePolicyOptions; + public PrivacyConsentService(UserManager userManager, IOptions cookiePolicyOptions, IUserService userService) + { + _userManager = userManager; + _cookiePolicyOptions = cookiePolicyOptions; + _userService = userService; + } - public PrivacyConsentService(UserManager userManager, IOptions cookiePolicyOptions, IUserService userService) + public async Task IsConsentBannerNeededAsync(HttpContext httpContext) + { + var consentFeature = httpContext.Features.Get(); + if (!consentFeature.IsConsentNeeded) { - _userManager = userManager; - _cookiePolicyOptions = cookiePolicyOptions; - _userService = userService; + return false; } - public async Task IsConsentBannerNeededAsync(HttpContext httpContext) + if (httpContext.User.Identity.IsAuthenticated) { - var consentFeature = httpContext.Features.Get(); - if (!consentFeature.IsConsentNeeded) - { - return false; - } - - if (httpContext.User.Identity.IsAuthenticated) - { - var user = await _userService.GetAuthenticatedUserAsync(httpContext.User); - return user is not User orchardUser || !orchardUser.Has(); - } - - var cookieConsent = httpContext.Request.Cookies[_cookiePolicyOptions.Value.ConsentCookie.Name]; - return cookieConsent == null; + var user = await _userService.GetAuthenticatedUserAsync(httpContext.User); + return user is not User orchardUser || !orchardUser.Has(); } - public async Task IsConsentNeededAsync(HttpContext httpContext) - { - if (httpContext.User.Identity.IsAuthenticated) - { - var user = await _userService.GetAuthenticatedUserAsync(httpContext.User); + var cookieConsent = httpContext.Request.Cookies[_cookiePolicyOptions.Value.ConsentCookie.Name]; + return cookieConsent == null; + } - return - user is not User orchardUser || - !(orchardUser.Has() && orchardUser.As().Accepted); - } + public async Task IsConsentNeededAsync(HttpContext httpContext) + { + if (httpContext.User.Identity.IsAuthenticated) + { + var user = await _userService.GetAuthenticatedUserAsync(httpContext.User); - return true; + return + user is not User orchardUser || + !(orchardUser.Has() && orchardUser.As().Accepted); } - public async Task IsUserAcceptedConsentAsync(HttpContext httpContext) - { - if (httpContext.User.Identity.IsAuthenticated) - { - var user = await _userService.GetAuthenticatedUserAsync(httpContext.User); + return true; + } - return - user is User orchardUser && - orchardUser.Has() && orchardUser.As().Accepted; - } + public async Task IsUserAcceptedConsentAsync(HttpContext httpContext) + { + if (httpContext.User.Identity.IsAuthenticated) + { + var user = await _userService.GetAuthenticatedUserAsync(httpContext.User); - var cookieConsent = httpContext.Request.Cookies[_cookiePolicyOptions.Value.ConsentCookie.Name]; - return !string.IsNullOrEmpty(cookieConsent) && cookieConsent.EqualsOrdinalIgnoreCase("yes"); + return + user is User orchardUser && + orchardUser.Has() && orchardUser.As().Accepted; } - public async Task StoreUserConsentAsync(ClaimsPrincipal user) => - await StoreUserConsentAsync(await _userService.GetAuthenticatedUserAsync(user)); + var cookieConsent = httpContext.Request.Cookies[_cookiePolicyOptions.Value.ConsentCookie.Name]; + return !string.IsNullOrEmpty(cookieConsent) && cookieConsent.EqualsOrdinalIgnoreCase("yes"); + } + + public async Task StoreUserConsentAsync(ClaimsPrincipal user) => + await StoreUserConsentAsync(await _userService.GetAuthenticatedUserAsync(user)); - public async Task StoreUserConsentAsync(IUser user) + public async Task StoreUserConsentAsync(IUser user) + { + if (user is User orchardUser) { - if (user is User orchardUser) - { - orchardUser.Put(new PrivacyConsent { Accepted = true }); - await _userManager.UpdateAsync(orchardUser); - } + orchardUser.Put(new PrivacyConsent { Accepted = true }); + await _userManager.UpdateAsync(orchardUser); } } } diff --git a/Startup.cs b/Startup.cs index 8a41972..9cf73a9 100644 --- a/Startup.cs +++ b/Startup.cs @@ -24,82 +24,81 @@ using System; using System.Threading.Tasks; -namespace Lombiq.Privacy -{ - public class Startup : StartupBase - { - public override void Configure( - IApplicationBuilder app, - IEndpointRouteBuilder routes, - IServiceProvider serviceProvider) => - app.UseCookiePolicy(); +namespace Lombiq.Privacy; - public override void ConfigureServices(IServiceCollection services) - { - services.AddScoped(); - services.Configure(options => - { - options.CheckConsentNeeded = context => IsConsentNeededAsync(context).GetAwaiter().GetResult(); - options.MinimumSameSitePolicy = SameSiteMode.Strict; - options.Secure = CookieSecurePolicy.Always; - options.ConsentCookie.Expiration = new TimeSpan(365, 0, 0, 0); - }); - } +public class Startup : StartupBase +{ + public override void Configure( + IApplicationBuilder app, + IEndpointRouteBuilder routes, + IServiceProvider serviceProvider) => + app.UseCookiePolicy(); - // This is necessary because IConsentService is not yet available with Dependency Injection at this point. - private static Task IsConsentNeededAsync(HttpContext httpContext) + public override void ConfigureServices(IServiceCollection services) + { + services.AddScoped(); + services.Configure(options => { - var consentService = httpContext.RequestServices.GetService(); - return consentService.IsConsentNeededAsync(httpContext); - } + options.CheckConsentNeeded = context => IsConsentNeededAsync(context).GetAwaiter().GetResult(); + options.MinimumSameSitePolicy = SameSiteMode.Strict; + options.Secure = CookieSecurePolicy.Always; + options.ConsentCookie.Expiration = new TimeSpan(365, 0, 0, 0); + }); } - [Feature(FeatureNames.ConsentBanner)] - public class ConsentBannerStartup : StartupBase + // This is necessary because IConsentService is not yet available with Dependency Injection at this point. + private static Task IsConsentNeededAsync(HttpContext httpContext) { - // This is important because the custom settings menu item override only runs correctly this way. - public override int Order => -1; - - public override void ConfigureServices(IServiceCollection services) - { - services.AddTransient, ResourceManagementOptionsConfiguration>(); - services.Configure((options) => - options.Filters.Add(typeof(PrivacyConsentBannerInjectionFilter))); - services.AddScoped(); - services.AddScoped(); - } + var consentService = httpContext.RequestServices.GetService(); + return consentService.IsConsentNeededAsync(httpContext); } +} - [Feature(FeatureNames.RegistrationConsent)] - public class RegistrationConsentStartup : StartupBase - { - // This is important because the custom settings menu item override only runs correctly this way. - public override int Order => -1; +[Feature(FeatureNames.ConsentBanner)] +public class ConsentBannerStartup : StartupBase +{ + // This is important because the custom settings menu item override only runs correctly this way. + public override int Order => -1; - public override void ConfigureServices(IServiceCollection services) - { - services.Configure((options) => - options.Filters.Add(typeof(RegistrationCheckboxInjectionFilter))); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - } + public override void ConfigureServices(IServiceCollection services) + { + services.AddTransient, ResourceManagementOptionsConfiguration>(); + services.Configure((options) => + options.Filters.Add(typeof(PrivacyConsentBannerInjectionFilter))); + services.AddScoped(); + services.AddScoped(); } +} - [Feature(FeatureNames.FormConsent)] - public class FormConsentStartup : StartupBase +[Feature(FeatureNames.RegistrationConsent)] +public class RegistrationConsentStartup : StartupBase +{ + // This is important because the custom settings menu item override only runs correctly this way. + public override int Order => -1; + + public override void ConfigureServices(IServiceCollection services) { - // This is important because the custom settings menu item override only runs correctly this way. - public override int Order => -1; + services.Configure((options) => + options.Filters.Add(typeof(RegistrationCheckboxInjectionFilter))); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + } +} - public override void ConfigureServices(IServiceCollection services) - { - services.AddContentPart() - .UseDisplayDriver(); - services.AddScoped(); - services.AddScoped(); - services.AddActivity(); - services.AddScoped(); - } +[Feature(FeatureNames.FormConsent)] +public class FormConsentStartup : StartupBase +{ + // This is important because the custom settings menu item override only runs correctly this way. + public override int Order => -1; + + public override void ConfigureServices(IServiceCollection services) + { + services.AddContentPart() + .UseDisplayDriver(); + services.AddScoped(); + services.AddScoped(); + services.AddActivity(); + services.AddScoped(); } } diff --git a/ViewModels/PrivacyConsentCheckboxPartViewModel.cs b/ViewModels/PrivacyConsentCheckboxPartViewModel.cs index 29d0152..697a1f5 100644 --- a/ViewModels/PrivacyConsentCheckboxPartViewModel.cs +++ b/ViewModels/PrivacyConsentCheckboxPartViewModel.cs @@ -1,7 +1,6 @@ -namespace Lombiq.Privacy.ViewModels +namespace Lombiq.Privacy.ViewModels; + +public class PrivacyConsentCheckboxPartViewModel { - public class PrivacyConsentCheckboxPartViewModel - { - public bool ConsentCheckbox { get; set; } - } + public bool ConsentCheckbox { get; set; } } diff --git a/ViewModels/PrivacyRegistrationConsentCheckboxViewModel.cs b/ViewModels/PrivacyRegistrationConsentCheckboxViewModel.cs index 4fa40c6..de880bf 100644 --- a/ViewModels/PrivacyRegistrationConsentCheckboxViewModel.cs +++ b/ViewModels/PrivacyRegistrationConsentCheckboxViewModel.cs @@ -1,7 +1,6 @@ -namespace Lombiq.Privacy.ViewModels +namespace Lombiq.Privacy.ViewModels; + +public class PrivacyRegistrationConsentCheckboxViewModel { - public class PrivacyRegistrationConsentCheckboxViewModel - { - public bool RegistrationCheckbox { get; set; } - } + public bool RegistrationCheckbox { get; set; } }