From 6d53645118fe5f2c185a88189966d002b2de63cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Sat, 21 Sep 2024 20:15:44 +0200 Subject: [PATCH 1/3] UserTimeZoneService now returns the correct time zone when the user opted to use the site's --- .../TimeZone/Services/UserTimeZoneService.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs index 9200ae10544..82069ebc794 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Caching.Distributed; using OrchardCore.Entities; using OrchardCore.Modules; +using OrchardCore.Settings; using OrchardCore.Users.Models; using OrchardCore.Users.TimeZone.Models; @@ -15,13 +16,16 @@ public class UserTimeZoneService : IUserTimeZoneService private readonly IClock _clock; private readonly IDistributedCache _distributedCache; + private readonly ISiteService _siteService; public UserTimeZoneService( IClock clock, - IDistributedCache distributedCache) + IDistributedCache distributedCache, + ISiteService siteService) { _clock = clock; _distributedCache = distributedCache; + _siteService = siteService; } /// @@ -67,7 +71,7 @@ private async ValueTask GetTimeZoneIdAsync(IUser user) // The timezone is not cached yet, resolve it and store the value if (string.IsNullOrEmpty(timeZoneId)) { - if (user is User u) + if (user is User u) { timeZoneId = u.As()?.TimeZoneId; } @@ -85,7 +89,8 @@ private async ValueTask GetTimeZoneIdAsync(IUser user) // Do we know this user doesn't have a configured value? if (timeZoneId == EmptyTimeZone) { - return null; + // The user opted to use the site's time zone. + return (await _siteService.GetSiteSettingsAsync())?.TimeZoneId; } return timeZoneId; From 7afe4a75a39d9bc6b0b08a9298da73d7fd3d4d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Tue, 24 Sep 2024 20:15:23 +0200 Subject: [PATCH 2/3] Handing no user time zone configured in UserTimeZoneSelector instead --- .../TimeZone/Services/UserTimeZoneSelector.cs | 9 +++++++-- .../TimeZone/Services/UserTimeZoneService.cs | 11 +++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs index d4c76236039..c4c4215195c 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs @@ -5,7 +5,7 @@ namespace OrchardCore.Users.TimeZone.Services; /// -/// Provides the timezone defined for the currently logged-in user for the current scope (request). +/// Provides the time zone defined for the currently logged-in user for the current scope (request). /// public class UserTimeZoneSelector : ITimeZoneSelector { @@ -27,10 +27,15 @@ public async Task GetTimeZoneAsync() { var currentUser = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); + if (currentUser == null) + { + return null; + } + return new TimeZoneSelectorResult { Priority = 100, - TimeZoneId = async () => (await _userTimeZoneService.GetAsync(currentUser)).TimeZoneId + TimeZoneId = async () => (await _userTimeZoneService.GetAsync(currentUser))?.TimeZoneId }; } } diff --git a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs index 82069ebc794..9200ae10544 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneService.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.Caching.Distributed; using OrchardCore.Entities; using OrchardCore.Modules; -using OrchardCore.Settings; using OrchardCore.Users.Models; using OrchardCore.Users.TimeZone.Models; @@ -16,16 +15,13 @@ public class UserTimeZoneService : IUserTimeZoneService private readonly IClock _clock; private readonly IDistributedCache _distributedCache; - private readonly ISiteService _siteService; public UserTimeZoneService( IClock clock, - IDistributedCache distributedCache, - ISiteService siteService) + IDistributedCache distributedCache) { _clock = clock; _distributedCache = distributedCache; - _siteService = siteService; } /// @@ -71,7 +67,7 @@ private async ValueTask GetTimeZoneIdAsync(IUser user) // The timezone is not cached yet, resolve it and store the value if (string.IsNullOrEmpty(timeZoneId)) { - if (user is User u) + if (user is User u) { timeZoneId = u.As()?.TimeZoneId; } @@ -89,8 +85,7 @@ private async ValueTask GetTimeZoneIdAsync(IUser user) // Do we know this user doesn't have a configured value? if (timeZoneId == EmptyTimeZone) { - // The user opted to use the site's time zone. - return (await _siteService.GetSiteSettingsAsync())?.TimeZoneId; + return null; } return timeZoneId; From 0e9631f64671e29634d4ae0f190b2caa0d64faa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Leh=C3=B3czky?= Date: Tue, 24 Sep 2024 20:17:25 +0200 Subject: [PATCH 3/3] Code styling --- .../TimeZone/Services/UserTimeZoneSelector.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs index c4c4215195c..f2b25c2ef28 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/TimeZone/Services/UserTimeZoneSelector.cs @@ -27,15 +27,12 @@ public async Task GetTimeZoneAsync() { var currentUser = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); - if (currentUser == null) - { - return null; - } - - return new TimeZoneSelectorResult - { - Priority = 100, - TimeZoneId = async () => (await _userTimeZoneService.GetAsync(currentUser))?.TimeZoneId - }; + return currentUser == null + ? null + : new TimeZoneSelectorResult + { + Priority = 100, + TimeZoneId = async () => (await _userTimeZoneService.GetAsync(currentUser))?.TimeZoneId + }; } }