Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/2.1] Fix site handing after enabling user time zone feature #17077

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override async Task<IDisplayResult> UpdateAsync(User user, UpdateEditorCo
var userContext = new UserContext(user);
// TODO This handler should be invoked through the create or update methods.
// otherwise it will not be invoked when a workflow, or other operation, changes this value.
await _userEventHandlers.InvokeAsync((handler, context) => handler.DisabledAsync(userContext), userContext, _logger);
await _userEventHandlers.InvokeAsync((handler, context) => handler.DisabledAsync(context), userContext, _logger);
}
}
else if (!isEditingDisabled && model.IsEnabled && !user.IsEnabled)
Expand All @@ -135,7 +135,7 @@ public override async Task<IDisplayResult> UpdateAsync(User user, UpdateEditorCo
var userContext = new UserContext(user);
// TODO This handler should be invoked through the create or update methods.
// otherwise it will not be invoked when a workflow, or other operation, changes this value.
await _userEventHandlers.InvokeAsync((handler, context) => handler.EnabledAsync(userContext), userContext, _logger);
await _userEventHandlers.InvokeAsync((handler, context) => handler.EnabledAsync(context), userContext, _logger);
}

if (context.Updater.ModelState.IsValid)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.Extensions.Caching.Distributed;
using OrchardCore.Users.Handlers;

namespace OrchardCore.Users.TimeZone.Handlers;

public class UserEventHandler : UserEventHandlerBase
{
private const string CacheKey = "UserTimeZone/";

private readonly IDistributedCache _distributedCache;

public UserEventHandler(IDistributedCache distributedCache)
{
_distributedCache = distributedCache;
}

public override Task DeletedAsync(UserDeleteContext context)
=> ForgetCacheAsync(context.User.UserName);

public override Task UpdatedAsync(UserUpdateContext context)
=> ForgetCacheAsync(context.User.UserName);

public override Task DisabledAsync(UserContext context)
=> ForgetCacheAsync(context.User.UserName);

private Task ForgetCacheAsync(string userName)
{
var key = GetCacheKey(userName);

return _distributedCache.RemoveAsync(key);
}

internal static string GetCacheKey(string userName)
=> CacheKey + userName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
using Microsoft.Extensions.Caching.Distributed;
using OrchardCore.Entities;
using OrchardCore.Modules;
using OrchardCore.Users.Handlers;
using OrchardCore.Users.Models;
using OrchardCore.Users.TimeZone.Handlers;
using OrchardCore.Users.TimeZone.Models;

namespace OrchardCore.Users.TimeZone.Services;

public class UserTimeZoneService : UserEventHandlerBase, IUserTimeZoneService
public class UserTimeZoneService : IUserTimeZoneService
{
private const string CacheKey = "UserTimeZone/";
private const string EmptyTimeZone = "NoTimeZoneFound";

private static readonly DistributedCacheEntryOptions _slidingExpiration = new()
Expand Down Expand Up @@ -47,7 +46,6 @@ public async ValueTask<ITimeZone> GetAsync(string userName)
return _clock.GetTimeZone(currentTimeZoneId);
}


/// <inheritdoc/>
public ValueTask<ITimeZone> GetAsync(IUser user)
=> GetAsync(user?.UserName);
Expand All @@ -59,7 +57,7 @@ public async ValueTask UpdateAsync(IUser user)
/// <inheritdoc/>
private async ValueTask<string> GetTimeZoneIdAsync(string userName)
{
var key = GetCacheKey(userName);
var key = UserEventHandler.GetCacheKey(userName);

var timeZoneId = await _distributedCache.GetStringAsync(key);

Expand Down Expand Up @@ -95,22 +93,10 @@ private async ValueTask<string> GetTimeZoneIdAsync(string userName)
return timeZoneId;
}

public override Task DeletedAsync(UserDeleteContext context)
=> ForgetCacheAsync(context.User.UserName);

public override Task UpdatedAsync(UserUpdateContext context)
=> ForgetCacheAsync(context.User.UserName);

public override Task DisabledAsync(UserContext context)
=> ForgetCacheAsync(context.User.UserName);

private Task ForgetCacheAsync(string userName)
{
var key = GetCacheKey(userName);
var key = UserEventHandler.GetCacheKey(userName);

return _distributedCache.RemoveAsync(key);
}

private static string GetCacheKey(string userName)
=> CacheKey + userName;
}
6 changes: 3 additions & 3 deletions src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OrchardCore.Users.Handlers;
using OrchardCore.Users.Models;
using OrchardCore.Users.TimeZone.Drivers;
using OrchardCore.Users.TimeZone.Handlers;
using OrchardCore.Users.TimeZone.Services;

namespace OrchardCore.Users.TimeZone;
Expand All @@ -13,10 +14,9 @@ public sealed class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IUserTimeZoneService, UserTimeZoneService>();
services.AddScoped<IUserEventHandler, UserEventHandler>();
services.AddScoped<ITimeZoneSelector, UserTimeZoneSelector>();
services.AddScoped<UserTimeZoneService>();
services.AddScoped<IUserTimeZoneService>(sp => sp.GetRequiredService<UserTimeZoneService>());
services.AddScoped<IUserEventHandler>(sp => sp.GetRequiredService<UserTimeZoneService>());
services.AddDisplayDriver<User, UserTimeZoneDisplayDriver>();
}
}