From 3092daaad4cd5e66c568b49a45c6e1745e131cca Mon Sep 17 00:00:00 2001 From: Baumgartner Marcel Date: Wed, 15 Nov 2023 15:24:27 +0100 Subject: [PATCH] Fixed cookie loading. Started implementing service expire handling --- .../App/Services/Interop/CookieService.cs | 5 +++- .../ServiceManage/ServiceManageService.cs | 13 ++++++++++ Moonlight/App/Services/Utils/JwtService.cs | 2 ++ .../Components/Alerts/NeedsRenewalAlert.razor | 16 ++++++++++++ .../Components/Service/ServiceItem.razor | 24 +++++++++++++++-- Moonlight/Shared/Views/Service/Index.razor | 26 ++++++++++++++----- Moonlight/Shared/Views/Services/Index.razor | 19 +++++++++++++- Moonlight/wwwroot/svg/expired.svg | 1 + 8 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 Moonlight/Shared/Components/Alerts/NeedsRenewalAlert.razor create mode 100644 Moonlight/wwwroot/svg/expired.svg diff --git a/Moonlight/App/Services/Interop/CookieService.cs b/Moonlight/App/Services/Interop/CookieService.cs index d8bf3a3..2aa0ce0 100644 --- a/Moonlight/App/Services/Interop/CookieService.cs +++ b/Moonlight/App/Services/Interop/CookieService.cs @@ -1,4 +1,5 @@ using Microsoft.JSInterop; +using Moonlight.App.Helpers; namespace Moonlight.App.Services.Interop; @@ -30,7 +31,9 @@ public class CookieService if(string.IsNullOrEmpty(cookiePart)) continue; - var cookieKeyValue = cookiePart.Split("="); + var cookieKeyValue = cookiePart.Split("=") + .Select(x => x.Trim()) // There may be spaces e.g. with the "AspNetCore.Culture" key + .ToArray(); if (cookieKeyValue.Length == 2) { diff --git a/Moonlight/App/Services/ServiceManage/ServiceManageService.cs b/Moonlight/App/Services/ServiceManage/ServiceManageService.cs index 140555b..8858fcc 100644 --- a/Moonlight/App/Services/ServiceManage/ServiceManageService.cs +++ b/Moonlight/App/Services/ServiceManage/ServiceManageService.cs @@ -45,4 +45,17 @@ public class ServiceManageService // No match return Task.FromResult(false); } + + public Task NeedsRenewal(Service s) + { + // We fetch the service in a new scope wo ensure that we are not caching + using var scope = ServiceScopeFactory.CreateScope(); + var serviceRepo = scope.ServiceProvider.GetRequiredService>(); + + var service = serviceRepo + .Get() + .First(x => x.Id == s.Id); + + return Task.FromResult(DateTime.UtcNow > service.RenewAt); + } } \ No newline at end of file diff --git a/Moonlight/App/Services/Utils/JwtService.cs b/Moonlight/App/Services/Utils/JwtService.cs index 4112511..3021c1c 100644 --- a/Moonlight/App/Services/Utils/JwtService.cs +++ b/Moonlight/App/Services/Utils/JwtService.cs @@ -1,5 +1,6 @@ using JWT.Algorithms; using JWT.Builder; +using Moonlight.App.Helpers; using Newtonsoft.Json; namespace Moonlight.App.Services.Utils; @@ -47,6 +48,7 @@ public class JwtService } catch (Exception e) { + Logger.Warn(e.Message); return Task.FromResult(false); } } diff --git a/Moonlight/Shared/Components/Alerts/NeedsRenewalAlert.razor b/Moonlight/Shared/Components/Alerts/NeedsRenewalAlert.razor new file mode 100644 index 0000000..a21d6d4 --- /dev/null +++ b/Moonlight/Shared/Components/Alerts/NeedsRenewalAlert.razor @@ -0,0 +1,16 @@ +
+
+
+
+ Expired illustration +
+

+ This service has expired +

+
+ This service has expired and has to be renewed in order to manage it +
+ Go back to services +
+
+
\ No newline at end of file diff --git a/Moonlight/Shared/Components/Service/ServiceItem.razor b/Moonlight/Shared/Components/Service/ServiceItem.razor index a240971..109cd1e 100644 --- a/Moonlight/Shared/Components/Service/ServiceItem.razor +++ b/Moonlight/Shared/Components/Service/ServiceItem.razor @@ -128,7 +128,13 @@ else

- @(Service.Nickname ?? $"Service {Service.Id}") + + @(Service.Nickname ?? $"Service {Service.Id}") + @if (NeedsRenewal) + { + (Expired) + } + @(Service.Product.Name)

@@ -177,14 +183,28 @@ else [Parameter] public Func OnChange { get; set; } + // Renew access state + private bool NeedsRenewal = false; + + // States private bool ShowDeletionScreen = false; - private bool ShowRenewScreen = false; private ManageServiceShareModal ShareModal; + // Renewing private int DurationMultiplier = 1; private bool CanBeRenewed = false; private bool IsValidating = false; private string ErrorMessage = ""; + private bool ShowRenewScreen = false; + + protected override async Task OnAfterRenderAsync(bool firstRender) + { + if (firstRender) + { + NeedsRenewal = await ServiceService.Manage.NeedsRenewal(Service); + await InvokeAsync(StateHasChanged); + } + } private Task Revalidate() { diff --git a/Moonlight/Shared/Views/Service/Index.razor b/Moonlight/Shared/Views/Service/Index.razor index d28a269..47e0d4c 100644 --- a/Moonlight/Shared/Views/Service/Index.razor +++ b/Moonlight/Shared/Views/Service/Index.razor @@ -19,15 +19,22 @@ } else { - - - - - @ViewContext.Layout + if (NeedsRenewal) + { + + } + else + { + + + + + @ViewContext.Layout + - + } } @@ -43,6 +50,8 @@ private ServiceDefinition Definition; private ServiceViewContext ViewContext; + private bool NeedsRenewal = false; + private async Task Load(LazyLoader lazyLoader) { await lazyLoader.SetText("Requesting service"); @@ -63,6 +72,11 @@ if (Service == null) return; + + NeedsRenewal = await ServiceService.Manage.NeedsRenewal(Service); + + if(NeedsRenewal) // Stop loading more data + return; // Load implementation await lazyLoader.SetText("Loading implementation"); diff --git a/Moonlight/Shared/Views/Services/Index.razor b/Moonlight/Shared/Views/Services/Index.razor index 80b36e3..6c5b8d1 100644 --- a/Moonlight/Shared/Views/Services/Index.razor +++ b/Moonlight/Shared/Views/Services/Index.razor @@ -19,11 +19,19 @@ @foreach (var service in SharedServices) { + var needsRenewal = SharedRenewalStates[service]; +

- @(service.Nickname ?? $"Service {service.Id}") + + @(service.Nickname ?? $"Service {service.Id}") + @if (needsRenewal) + { + (Expired) + } + @(service.Product.Name)

@@ -57,10 +65,19 @@ private Service[] MyServices; private Service[] SharedServices; + private Dictionary SharedRenewalStates = new(); private async Task Load(LazyLoader _) { + // Load all services MyServices = await ServiceService.Get(IdentityService.CurrentUser); SharedServices = await ServiceService.GetShared(IdentityService.CurrentUser); + + // Load all services renewal states + foreach (var service in SharedServices) + { + if(!SharedRenewalStates.ContainsKey(service)) + SharedRenewalStates.Add(service, await ServiceService.Manage.NeedsRenewal(service)); + } } } \ No newline at end of file diff --git a/Moonlight/wwwroot/svg/expired.svg b/Moonlight/wwwroot/svg/expired.svg new file mode 100644 index 0000000..9ac0f34 --- /dev/null +++ b/Moonlight/wwwroot/svg/expired.svg @@ -0,0 +1 @@ + \ No newline at end of file