Fix #4936: add the cookie consent theme control.
This commit is contained in:
34
Oqtane.Server/Controllers/CookieConsentController.cs
Normal file
34
Oqtane.Server/Controllers/CookieConsentController.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using Oqtane.Infrastructure;
|
||||
using Oqtane.Services;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
[Route(ControllerRoutes.ApiRoute)]
|
||||
public class CookieConsentController : Controller
|
||||
{
|
||||
private readonly ICookieConsentService _cookieConsentService;
|
||||
|
||||
public CookieConsentController(ICookieConsentService cookieConsentService)
|
||||
{
|
||||
_cookieConsentService = cookieConsentService;
|
||||
}
|
||||
|
||||
[HttpGet("CanTrack")]
|
||||
public async Task<bool> CanTrack()
|
||||
{
|
||||
return await _cookieConsentService.CanTrackAsync();
|
||||
}
|
||||
|
||||
[HttpGet("CreateConsentCookie")]
|
||||
public async Task<string> CreateConsentCookie()
|
||||
{
|
||||
return await _cookieConsentService.CreateConsentCookieAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -103,6 +103,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
services.AddScoped<ISearchService, SearchService>();
|
||||
services.AddScoped<ISearchProvider, DatabaseSearchProvider>();
|
||||
services.AddScoped<IImageService, ImageService>();
|
||||
services.AddScoped<ICookieConsentService, ServerCookieConsentService>();
|
||||
|
||||
// providers
|
||||
services.AddScoped<ITextEditor, Oqtane.Modules.Controls.QuillJSTextEditor>();
|
||||
|
||||
38
Oqtane.Server/Services/CookieConsentService.cs
Normal file
38
Oqtane.Server/Services/CookieConsentService.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Oqtane.Documentation;
|
||||
|
||||
namespace Oqtane.Services
|
||||
{
|
||||
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
|
||||
public class ServerCookieConsentService : ICookieConsentService
|
||||
{
|
||||
private readonly IHttpContextAccessor _accessor;
|
||||
|
||||
public ServerCookieConsentService(IHttpContextAccessor accessor)
|
||||
{
|
||||
_accessor = accessor;
|
||||
}
|
||||
|
||||
public Task<bool> CanTrackAsync()
|
||||
{
|
||||
var consentFeature = _accessor.HttpContext?.Features.Get<ITrackingConsentFeature>();
|
||||
var canTrack = consentFeature?.CanTrack ?? true;
|
||||
|
||||
return Task.FromResult(canTrack);
|
||||
}
|
||||
|
||||
public Task<string> CreateConsentCookieAsync()
|
||||
{
|
||||
var consentFeature = _accessor.HttpContext?.Features.Get<ITrackingConsentFeature>();
|
||||
consentFeature?.GrantConsent();
|
||||
var cookie = consentFeature?.CreateConsentCookie() ?? string.Empty;
|
||||
|
||||
return Task.FromResult(cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -169,6 +169,13 @@ namespace Oqtane
|
||||
options.CustomSchemaIds(type => type.ToString()); // Handle SchemaId already used for different type
|
||||
});
|
||||
services.TryAddSwagger(_useSwagger);
|
||||
|
||||
//add cookie consent policy
|
||||
services.Configure<CookiePolicyOptions>(options =>
|
||||
{
|
||||
options.CheckConsentNeeded = context => true;
|
||||
options.ConsentCookieValue = Constants.CookieConsentCookieValue;
|
||||
});
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
@ -225,6 +232,7 @@ namespace Oqtane
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseAntiforgery();
|
||||
app.UseCookiePolicy();
|
||||
|
||||
if (_useSwagger)
|
||||
{
|
||||
|
||||
@ -14,6 +14,9 @@ Oqtane.Interop = {
|
||||
}
|
||||
document.cookie = cookieString;
|
||||
},
|
||||
setCookieString: function (cookieString) {
|
||||
document.cookie = cookieString;
|
||||
},
|
||||
getCookie: function (name) {
|
||||
name = name + "=";
|
||||
var decodedCookie = decodeURIComponent(document.cookie);
|
||||
|
||||
Reference in New Issue
Block a user