Skip to content

Commit

Permalink
Admin session results are no longer cached (#509)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Lieckens <[email protected]>
  • Loading branch information
sc-ivanlieckens and IvanLieckens authored Jan 28, 2025
1 parent b7f2feb commit 5190e09
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion headapps/MvpSite/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Session" Version="2.2.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
<PackageVersion Include="Okta.AspNetCore" Version="4.5.0" />
<PackageVersion Include="Mvp.Selections.Client" Version="4.18.0" />
<PackageVersion Include="Mvp.Selections.Client" Version="4.19.0" />
<PackageVersion Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageVersion Include="Markdig" Version="0.37.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<bool> IsCurrentUserAnAdmin()
{
bool isAdmin = false;
if (await client.IsAuthenticatedAsync())
{
Response<User> currentUserResponse = await client.GetCurrentUserAsync();
if (currentUserResponse is { StatusCode: HttpStatusCode.OK, Result: not null })
{
isAdmin = currentUserResponse.Result.HasRight(Right.Admin);
}
}

return isAdmin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,7 +121,8 @@ private async Task ExecuteSearch(DirectoryViewModel model)
List<short>? 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<MvpProfile>? profiles))
bool isAdmin = await new MvpSelectionsApiHelper(client).IsCurrentUserAnAdmin();
if (isAdmin || !cache.TryGetValue(cacheKey, out SearchResult<MvpProfile>? profiles))
{
Response<SearchResult<MvpProfile>> response = await client.SearchMvpProfileAsync(
model.Query,
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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
{
Expand Down

0 comments on commit 5190e09

Please sign in to comment.