Skip to content

Commit

Permalink
Fix site handing after enabling user time zone feature (#17071)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mike Alhayek <[email protected]>
  • Loading branch information
hishamco and MikeAlhayek authored Nov 26, 2024
1 parent 13d647b commit 1061902
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
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>();
}
}

0 comments on commit 1061902

Please sign in to comment.