From 5190e09d404ff554ff3fac8f5c57766a5a17e4b0 Mon Sep 17 00:00:00 2001 From: Ivan Lieckens Date: Tue, 28 Jan 2025 10:45:50 +0100 Subject: [PATCH] Admin session results are no longer cached (#509) Co-authored-by: Ivan Lieckens --- headapps/MvpSite/Directory.Packages.props | 2 +- .../Helpers/MvpSelectionsApiHelper.cs | 24 +++++++++++++++++++ .../ViewComponents/DirectoryViewComponent.cs | 11 +++++++-- .../ViewComponents/ProfileViewComponent.cs | 9 +++++-- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 headapps/MvpSite/MvpSite.Rendering/Helpers/MvpSelectionsApiHelper.cs diff --git a/headapps/MvpSite/Directory.Packages.props b/headapps/MvpSite/Directory.Packages.props index f36d7412..5d727e45 100644 --- a/headapps/MvpSite/Directory.Packages.props +++ b/headapps/MvpSite/Directory.Packages.props @@ -21,7 +21,7 @@ - + diff --git a/headapps/MvpSite/MvpSite.Rendering/Helpers/MvpSelectionsApiHelper.cs b/headapps/MvpSite/MvpSite.Rendering/Helpers/MvpSelectionsApiHelper.cs new file mode 100644 index 00000000..6073e7c9 --- /dev/null +++ b/headapps/MvpSite/MvpSite.Rendering/Helpers/MvpSelectionsApiHelper.cs @@ -0,0 +1,24 @@ +using System.Net; +using Mvp.Selections.Client; +using Mvp.Selections.Client.Models; +using Mvp.Selections.Domain; + +namespace MvpSite.Rendering.Helpers; + +public class MvpSelectionsApiHelper(MvpSelectionsApiClient client) +{ + public async Task IsCurrentUserAnAdmin() + { + bool isAdmin = false; + if (await client.IsAuthenticatedAsync()) + { + Response currentUserResponse = await client.GetCurrentUserAsync(); + if (currentUserResponse is { StatusCode: HttpStatusCode.OK, Result: not null }) + { + isAdmin = currentUserResponse.Result.HasRight(Right.Admin); + } + } + + return isAdmin; + } +} \ No newline at end of file diff --git a/headapps/MvpSite/MvpSite.Rendering/ViewComponents/DirectoryViewComponent.cs b/headapps/MvpSite/MvpSite.Rendering/ViewComponents/DirectoryViewComponent.cs index 35972611..ccfacea3 100644 --- a/headapps/MvpSite/MvpSite.Rendering/ViewComponents/DirectoryViewComponent.cs +++ b/headapps/MvpSite/MvpSite.Rendering/ViewComponents/DirectoryViewComponent.cs @@ -6,8 +6,10 @@ using Mvp.Selections.Api.Model; using Mvp.Selections.Client; using Mvp.Selections.Client.Models; +using Mvp.Selections.Domain; using MvpSite.Rendering.Configuration; using MvpSite.Rendering.Extensions; +using MvpSite.Rendering.Helpers; using MvpSite.Rendering.Models.Directory; using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model; using Sitecore.AspNetCore.SDK.LayoutService.Client.Response.Model.Fields; @@ -119,7 +121,8 @@ private async Task ExecuteSearch(DirectoryViewModel model) List? countryFacetIdentifiers = ExtractSearchIdentifiers(model.ViewFacets, CountryFacetIdentifier); string cacheKey = $"{SearchCacheKeyPrefix}{model.Query}_{mvpTypeFacetIdentifiers.ToCommaSeparatedStringOrNullLiteral()}_{yearFacetIdentifiers.ToCommaSeparatedStringOrNullLiteral()}_{countryFacetIdentifiers.ToCommaSeparatedStringOrNullLiteral()}_p{model.Page}/{model.PageSize}"; - if (!cache.TryGetValue(cacheKey, out SearchResult? profiles)) + bool isAdmin = await new MvpSelectionsApiHelper(client).IsCurrentUserAnAdmin(); + if (isAdmin || !cache.TryGetValue(cacheKey, out SearchResult? profiles)) { Response> response = await client.SearchMvpProfileAsync( model.Query, @@ -131,10 +134,14 @@ private async Task ExecuteSearch(DirectoryViewModel model) if (response is { StatusCode: HttpStatusCode.OK, Result: not null }) { profiles = response.Result; - cache.Set(cacheKey, response.Result, TimeSpan.FromSeconds(_options.SearchCachedSeconds)); + if (!isAdmin) + { + cache.Set(cacheKey, response.Result, TimeSpan.FromSeconds(_options.SearchCachedSeconds)); + } } else { + profiles = null; model.ErrorMessages.Add(response.Message); } } diff --git a/headapps/MvpSite/MvpSite.Rendering/ViewComponents/ProfileViewComponent.cs b/headapps/MvpSite/MvpSite.Rendering/ViewComponents/ProfileViewComponent.cs index 7e856bca..2526b0a0 100644 --- a/headapps/MvpSite/MvpSite.Rendering/ViewComponents/ProfileViewComponent.cs +++ b/headapps/MvpSite/MvpSite.Rendering/ViewComponents/ProfileViewComponent.cs @@ -8,6 +8,7 @@ using Mvp.Selections.Domain; using MvpSite.Rendering.Configuration; using MvpSite.Rendering.Extensions; +using MvpSite.Rendering.Helpers; using MvpSite.Rendering.Models.Profile; using Sitecore.AspNetCore.SDK.RenderingEngine.Binding; @@ -185,7 +186,8 @@ private async Task LoadProfile(ProfileViewModel model) { MvpProfile? result = null; string cacheKey = $"{ProfileCacheKeyPrefix}{id:N}"; - if (cache.TryGetValue(cacheKey, out MvpProfile? profile)) + bool isAdmin = await new MvpSelectionsApiHelper(client).IsCurrentUserAnAdmin(); + if (!isAdmin && cache.TryGetValue(cacheKey, out MvpProfile? profile)) { result = profile; } @@ -195,7 +197,10 @@ private async Task LoadProfile(ProfileViewModel model) if (response is { StatusCode: HttpStatusCode.OK, Result: not null }) { result = response.Result; - cache.Set(cacheKey, response.Result, TimeSpan.FromSeconds(_options.ProfileCachedSeconds)); + if (!isAdmin) + { + cache.Set(cacheKey, response.Result, TimeSpan.FromSeconds(_options.ProfileCachedSeconds)); + } } else {