diff --git a/Moonlight/Core/Configuration/CoreConfiguration.cs b/Moonlight/Core/Configuration/CoreConfiguration.cs index 6af7336c..38930921 100644 --- a/Moonlight/Core/Configuration/CoreConfiguration.cs +++ b/Moonlight/Core/Configuration/CoreConfiguration.cs @@ -116,6 +116,7 @@ public class CoreConfiguration public FileManagerData FileManager { get; set; } = new(); [JsonProperty("Footer")] public FooterData Footer { get; set; } = new(); + [JsonProperty("CookieConsentBanner")] public CookieData CookieConsentBanner{ get; set; } = new(); } public class FooterData @@ -147,4 +148,27 @@ public class CoreConfiguration [Description("This specifies the general timeout for file manager operations. This can but has not to be used by file accesses")] public int OperationTimeout { get; set; } = 5; } + + public class CookieData + { + [JsonProperty("Enabled")] + [Description("This specifies if the cookie consent banner is shown to users.")] + public bool Enabled { get; set; } = false; + + [JsonProperty("BannerTitle")] + [Description("The title for the cookie consent banner.")] + public string BannerTitle { get; set; } = "\ud83c\udf6a Cookies"; + + [JsonProperty("BannerText")] + [Description("The description for the cookie consent banner.")] + public string BannerText { get; set; } = "Moonlight is using cookies \ud83c\udf6a, to personalize your experience."; + + [JsonProperty("ConsentText")] + [Description("The text for the consent option.")] + public string ConsentText { get; set; } = "Consent"; + + [JsonProperty("DeclineText")] + [Description("The text for the decline option.")] + public string DeclineText { get; set; } = "Decline"; + } } \ No newline at end of file diff --git a/Moonlight/Core/Models/Forms/ChangeCookiesForm.cs b/Moonlight/Core/Models/Forms/ChangeCookiesForm.cs new file mode 100644 index 00000000..0090240e --- /dev/null +++ b/Moonlight/Core/Models/Forms/ChangeCookiesForm.cs @@ -0,0 +1,9 @@ +using System.ComponentModel; + +namespace Moonlight.Core.Models.Forms; + +public class ChangeCookiesForm +{ + [Description("This specifies if you would like to personalize your experience with optional cookies.")] + public bool UseOptionalCookies { get; set; } = false; +} \ No newline at end of file diff --git a/Moonlight/Core/UI/Components/Partials/CookieConsentBanner.razor b/Moonlight/Core/UI/Components/Partials/CookieConsentBanner.razor new file mode 100644 index 00000000..f6b4b00e --- /dev/null +++ b/Moonlight/Core/UI/Components/Partials/CookieConsentBanner.razor @@ -0,0 +1,71 @@ +@using ApexCharts +@using MoonCore.Services +@using Moonlight.Core.Configuration +@using Moonlight.Core.Services + +@inject ConfigService ConfigService +@inject IdentityService IdentityService + +@if (ShowBanner) +{ +
+
+
+
+

@ConfigService.Get().Customisation.CookieConsentBanner.BannerTitle

+

+ @ConfigService.Get().Customisation.CookieConsentBanner.BannerText +

+ + + @ConfigService.Get().Customisation.CookieConsentBanner.ConsentText + + + @ConfigService.Get().Customisation.CookieConsentBanner.DeclineText + + +
+
+
+
+} + +@code { + + private bool ShowBanner; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + var userWasAsked = await IdentityService.HasFlag("CookieAsked"); + + if (ConfigService.Get().Customisation.CookieConsentBanner.Enabled && !userWasAsked) + ShowBanner = true; + + await InvokeAsync(StateHasChanged); + } + } + + private async Task Consent() + { + if (!IdentityService.IsLoggedIn) + return; + + await IdentityService.SetFlag("CookieAsked", true); + await IdentityService.SetFlag("CookieConsent", true); + + await InvokeAsync(StateHasChanged); + } + + private async Task Decline() + { + if (!IdentityService.IsLoggedIn) + return; + + await IdentityService.SetFlag("CookieAsked", true); + + await InvokeAsync(StateHasChanged); + } + +} \ No newline at end of file diff --git a/Moonlight/Core/UI/Layouts/MainLayout.razor b/Moonlight/Core/UI/Layouts/MainLayout.razor index 964cc2ec..f0007150 100644 --- a/Moonlight/Core/UI/Layouts/MainLayout.razor +++ b/Moonlight/Core/UI/Layouts/MainLayout.razor @@ -56,6 +56,7 @@ { @Body + } else diff --git a/Moonlight/Core/UI/Views/Account/Security.razor b/Moonlight/Core/UI/Views/Account/Security.razor index eb69af0a..7c2cb050 100644 --- a/Moonlight/Core/UI/Views/Account/Security.razor +++ b/Moonlight/Core/UI/Views/Account/Security.razor @@ -28,7 +28,7 @@
- +
@@ -47,19 +47,37 @@
+
+
+ +
+

Cookies

+
+ + +
+
+ +
+
+
@code { private readonly ChangePasswordForm PasswordForm = new(); + + private ChangeCookiesForm CookiesForm = new(); - private Task Load(LazyLoader lazyLoader) + private async Task Load(LazyLoader lazyLoader) { - return Task.CompletedTask; + CookiesForm.UseOptionalCookies = await IdentityService.HasFlag("CookieConsent"); } - private async Task OnValidSubmit() + private async Task OnValidSubmitPassword() { if (PasswordForm.Password != PasswordForm.RepeatedPassword) throw new DisplayException("The passwords do not match"); @@ -69,4 +87,11 @@ await ToastService.Success("Successfully changed password"); await IdentityService.Authenticate(true); } + + private async Task OnValidSubmitCookie() + { + await IdentityService.SetFlag("CookieConsent", CookiesForm.UseOptionalCookies); + + await InvokeAsync(StateHasChanged); + } } \ No newline at end of file