From 9cf550e1f08a61e3ac5a63caf3c0976ae6e84a2b Mon Sep 17 00:00:00 2001 From: pavelbannov Date: Thu, 21 Sep 2023 14:38:55 +0300 Subject: [PATCH] Notify: fix culture --- .../Base/LdapNotifySource.cs | 4 +- .../Context/Impl/UserManager.cs | 39 ++++++++++++- common/ASC.Core.Common/Notify/Context.cs | 4 +- .../Notify/Engine/NotifyEngine.cs | 14 ++--- .../Notify/Engine/NotifyRequest.cs | 9 +-- .../Notify/Model/INotifySource.cs | 4 +- common/ASC.Core.Common/Notify/NotifySource.cs | 55 ++++++++++++++----- .../Services/NotifyService/NotifySource.cs | 4 +- .../Notify/NotifyConfiguration.cs | 38 +------------ web/ASC.Web.Core/Notify/StudioNotifySource.cs | 4 +- 10 files changed, 97 insertions(+), 78 deletions(-) diff --git a/common/ASC.ActiveDirectory/Base/LdapNotifySource.cs b/common/ASC.ActiveDirectory/Base/LdapNotifySource.cs index dda1585eaa..c2947d5ea1 100644 --- a/common/ASC.ActiveDirectory/Base/LdapNotifySource.cs +++ b/common/ASC.ActiveDirectory/Base/LdapNotifySource.cs @@ -52,12 +52,12 @@ public class LdapNotifySource : INotifySource await _ldapNotifyHelper.AutoSyncAsync(_tenant); } - public IActionProvider GetActionProvider() + public Task GetActionProvider(NotifyRequest r) { throw new NotImplementedException(); } - public IPatternProvider GetPatternProvider() + public Task GetPatternProvider(NotifyRequest r) { throw new NotImplementedException(); } diff --git a/common/ASC.Core.Common/Context/Impl/UserManager.cs b/common/ASC.Core.Common/Context/Impl/UserManager.cs index 64878f5b07..9e02e93061 100644 --- a/common/ASC.Core.Common/Context/Impl/UserManager.cs +++ b/common/ASC.Core.Common/Context/Impl/UserManager.cs @@ -180,7 +180,7 @@ public class UserManager return await users.ToArrayAsync(); } - + public Task GetUsersCountAsync( bool isDocSpaceAdmin, EmployeeStatus? employeeStatus, @@ -318,6 +318,39 @@ public class UserManager return u != null && !u.Removed ? u : Constants.LostUser; } + public async Task SearchUserAsync(string id) + { + var result = Constants.LostUser; + + if (32 <= id.Length) + { + var guid = default(Guid); + try + { + guid = new Guid(id); + } + catch (FormatException) { } + catch (OverflowException) { } + + if (guid != default) + { + result = await GetUsersAsync(guid); + } + } + + if (Constants.LostUser.Equals(result)) + { + result = await GetUserByEmailAsync(id); + } + + if (Constants.LostUser.Equals(result)) + { + result = await GetUserByUserNameAsync(id); + } + + return result; + } + public async Task SearchAsync(string text, EmployeeStatus status) { return await SearchAsync(text, status, Guid.Empty); @@ -559,8 +592,8 @@ public class UserManager new Uri(_cache.Get("REWRITE_URL" + tenant.Id)).ToString() : tenant.GetTenantDomain(_coreSettings); var davUsersEmails = await GetDavUserEmailsAsync(); var requestUrlBook = _cardDavAddressbook.GetRadicaleUrl(myUri, delUser.Email.ToLower(), true, true); - - if(rootAuthorization != null) + + if (rootAuthorization != null) { var addBookCollection = await _cardDavAddressbook.GetCollection(requestUrlBook, userAuthorization, myUri.ToString()); if (addBookCollection.Completed && addBookCollection.StatusCode != 404) diff --git a/common/ASC.Core.Common/Notify/Context.cs b/common/ASC.Core.Common/Notify/Context.cs index 93e69b9a56..75c36d7ea7 100644 --- a/common/ASC.Core.Common/Notify/Context.cs +++ b/common/ASC.Core.Common/Notify/Context.cs @@ -34,9 +34,7 @@ public sealed class Context : INotifyRegistry internal const string SysRecipientName = "SYS_RECIPIENT_NAME"; internal const string SysRecipientAddress = "SYS_RECIPIENT_ADDRESS"; - private readonly Dictionary _channels = new Dictionary(2); - - public event Action NotifyClientRegistration; + private readonly Dictionary _channels = new Dictionary(2); public void RegisterSender(DispatchEngine dispatchEngine, string senderName, ISink senderSink) { diff --git a/common/ASC.Core.Common/Notify/Engine/NotifyEngine.cs b/common/ASC.Core.Common/Notify/Engine/NotifyEngine.cs index fc22c82b63..3bc6a26190 100644 --- a/common/ASC.Core.Common/Notify/Engine/NotifyEngine.cs +++ b/common/ASC.Core.Common/Notify/Engine/NotifyEngine.cs @@ -207,8 +207,8 @@ public class NotifyEngine try { await PrepareRequestFillSendersAsync(request, serviceScope); - PrepareRequestFillPatterns(request, serviceScope); - PrepareRequestFillTags(request, serviceScope); + await PrepareRequestFillPatterns(request, serviceScope); + await PrepareRequestFillTags(request, serviceScope); } catch (Exception ex) { @@ -310,7 +310,7 @@ public class NotifyEngine noticeMessage.Pattern = pattern; noticeMessage.ContentType = pattern.ContentType; noticeMessage.AddArgument(request.Arguments.ToArray()); - var patternProvider = request.GetPatternProvider(serviceScope); + var patternProvider = await request.GetPatternProvider(serviceScope); var formatter = patternProvider.GetFormatter(pattern); try @@ -375,7 +375,7 @@ public class NotifyEngine } } - private void PrepareRequestFillPatterns(NotifyRequest request, IServiceScope serviceScope) + private async Task PrepareRequestFillPatterns(NotifyRequest request, IServiceScope serviceScope) { if (request._patterns == null) { @@ -385,7 +385,7 @@ public class NotifyEngine return; } - var apProvider = request.GetPatternProvider(serviceScope); + var apProvider = await request.GetPatternProvider(serviceScope); for (var i = 0; i < request._senderNames.Length; i++) { var senderName = request._senderNames[i]; @@ -404,9 +404,9 @@ public class NotifyEngine } } - private void PrepareRequestFillTags(NotifyRequest request, IServiceScope serviceScope) + private async Task PrepareRequestFillTags(NotifyRequest request, IServiceScope serviceScope) { - var patternProvider = request.GetPatternProvider(serviceScope); + var patternProvider = await request.GetPatternProvider(serviceScope); foreach (var pattern in request._patterns) { IPatternFormatter formatter; diff --git a/common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs b/common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs index f657453822..92ed689ec1 100644 --- a/common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs +++ b/common/ASC.Core.Common/Notify/Engine/NotifyRequest.cs @@ -125,14 +125,9 @@ public class NotifyRequest return new NoticeMessage(recipient, NotifyAction, ObjectID); } - public IActionProvider GetActionProvider(IServiceScope scope) + public async Task GetPatternProvider(IServiceScope scope) { - return ((INotifySource)scope.ServiceProvider.GetService(_notifySource.GetType())).GetActionProvider(); - } - - public IPatternProvider GetPatternProvider(IServiceScope scope) - { - return ((INotifySource)scope.ServiceProvider.GetService(_notifySource.GetType())).GetPatternProvider(); + return await ((INotifySource)scope.ServiceProvider.GetService(_notifySource.GetType())).GetPatternProvider(this); } public IRecipientProvider GetRecipientsProvider(IServiceScope scope) diff --git a/common/ASC.Core.Common/Notify/Model/INotifySource.cs b/common/ASC.Core.Common/Notify/Model/INotifySource.cs index 6d4068817d..ab4c90c727 100644 --- a/common/ASC.Core.Common/Notify/Model/INotifySource.cs +++ b/common/ASC.Core.Common/Notify/Model/INotifySource.cs @@ -29,8 +29,8 @@ namespace ASC.Notify.Model; public interface INotifySource { string Id { get; } - IActionProvider GetActionProvider(); - IPatternProvider GetPatternProvider(); + Task GetActionProvider(NotifyRequest r); + Task GetPatternProvider(NotifyRequest r); IRecipientProvider GetRecipientsProvider(); ISubscriptionProvider GetSubscriptionProvider(); } diff --git a/common/ASC.Core.Common/Notify/NotifySource.cs b/common/ASC.Core.Common/Notify/NotifySource.cs index 0130bac3df..6c676bfb9d 100644 --- a/common/ASC.Core.Common/Notify/NotifySource.cs +++ b/common/ASC.Core.Common/Notify/NotifySource.cs @@ -33,14 +33,13 @@ public abstract class NotifySource : INotifySource protected ISubscriptionProvider _subscriprionProvider; protected IRecipientProvider _recipientsProvider; - protected IActionProvider ActionProvider => GetActionProvider(); - protected IPatternProvider PatternProvider => GetPatternProvider(); public string Id { get; private set; } private readonly UserManager _userManager; private readonly SubscriptionManager _subscriptionManager; + private readonly TenantManager _tenantManager; - protected NotifySource(string id, UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager) + protected NotifySource(string id, UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager, TenantManager tenantManager) { ArgumentNullOrEmptyException.ThrowIfNullOrEmpty(id); @@ -48,18 +47,22 @@ public abstract class NotifySource : INotifySource _userManager = userManager; _recipientsProvider = recipientsProvider; _subscriptionManager = subscriptionManager; + _tenantManager = tenantManager; } - protected NotifySource(Guid id, UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager) - : this(id.ToString(), userManager, recipientsProvider, subscriptionManager) + protected NotifySource(Guid id, UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager, TenantManager tenantManager) + : this(id.ToString(), userManager, recipientsProvider, subscriptionManager, tenantManager) { } - public IActionProvider GetActionProvider() + public async Task GetActionProvider(NotifyRequest r) { + var culture = await GetCulture(r); + CultureInfo.CurrentCulture = culture; + CultureInfo.CurrentUICulture = culture; + lock (_actions) { - var culture = CultureInfo.CurrentCulture; if (!_actions.ContainsKey(culture)) { _actions[culture] = CreateActionProvider(); @@ -69,15 +72,14 @@ public abstract class NotifySource : INotifySource } } - public IPatternProvider GetPatternProvider() + public async Task GetPatternProvider(NotifyRequest r) { + var culture = await GetCulture(r); + CultureInfo.CurrentCulture = culture; + CultureInfo.CurrentUICulture = culture; + lock (_patterns) { - var culture = CultureInfo.CurrentCulture; - if (CultureInfo.CurrentUICulture != culture) - { - CultureInfo.CurrentUICulture = culture; - } if (!_patterns.ContainsKey(culture)) { _patterns[culture] = CreatePatternsProvider(); @@ -115,4 +117,31 @@ public abstract class NotifySource : INotifySource return new RecipientProviderImpl(_userManager) ?? throw new NotifyException("Provider IRecipientsProvider not instanced."); } + + private async Task GetCulture(NotifyRequest r) + { + var tagCulture = r.Arguments.FirstOrDefault(a => a.Tag == "Culture"); + if (tagCulture != null) + { + return CultureInfo.GetCultureInfo((string)tagCulture.Value); + } + + CultureInfo culture = null; + + var tenant = await _tenantManager.GetCurrentTenantAsync(false); + + if (tenant != null) + { + culture = tenant.GetCulture(); + } + + var user = await _userManager.SearchUserAsync(r.Recipient.ID); + + if (!Users.Constants.LostUser.Equals(user) && !string.IsNullOrEmpty(user.CultureName)) + { + culture = user.GetCulture(); + } + + return culture; + } } diff --git a/products/ASC.Files/Core/Services/NotifyService/NotifySource.cs b/products/ASC.Files/Core/Services/NotifyService/NotifySource.cs index d35705d63c..a4df6c7eb0 100644 --- a/products/ASC.Files/Core/Services/NotifyService/NotifySource.cs +++ b/products/ASC.Files/Core/Services/NotifyService/NotifySource.cs @@ -32,8 +32,8 @@ namespace ASC.Files.Core.Services.NotifyService; [Scope] public class NotifySource : NotifySourceBase { - public NotifySource(UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager) - : base(new Guid("6FE286A4-479E-4c25-A8D9-0156E332B0C0"), userManager, recipientsProvider, subscriptionManager) + public NotifySource(UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager, TenantManager tenantManager) + : base(new Guid("6FE286A4-479E-4c25-A8D9-0156E332B0C0"), userManager, recipientsProvider, subscriptionManager, tenantManager) { } diff --git a/web/ASC.Web.Core/Notify/NotifyConfiguration.cs b/web/ASC.Web.Core/Notify/NotifyConfiguration.cs index 76a402e734..e3e72a7d0e 100644 --- a/web/ASC.Web.Core/Notify/NotifyConfiguration.cs +++ b/web/ASC.Web.Core/Notify/NotifyConfiguration.cs @@ -182,38 +182,10 @@ public class ProductSecurityInterceptor { var tenant = _tenantManager.GetCurrentTenant(); - if (32 <= r.Recipient.ID.Length) - { - var guid = default(Guid); - try - { - guid = new Guid(r.Recipient.ID); - } - catch (FormatException) { } - catch (OverflowException) { } - - if (guid != default) - { - u = await _userManager.GetUsersAsync(guid); - } - } - - if (Constants.LostUser.Equals(u)) - { - u = await _userManager.GetUserByEmailAsync(r.Recipient.ID); - } - - if (Constants.LostUser.Equals(u)) - { - u = await _userManager.GetUserByUserNameAsync(r.Recipient.ID); - } + u = await _userManager.SearchUserAsync(r.Recipient.ID); if (!Constants.LostUser.Equals(u)) { - var culture = !string.IsNullOrEmpty(u.CultureName) ? u.GetCulture() : tenant.GetCulture(); - CultureInfo.CurrentCulture = culture; - CultureInfo.CurrentUICulture = culture; - // security var tag = r.Arguments.Find(a => a.Tag == CommonTags.ModuleID); var productId = tag != null ? (Guid)tag.Value : Guid.Empty; @@ -232,14 +204,6 @@ public class ProductSecurityInterceptor } } } - - var tagCulture = r.Arguments.FirstOrDefault(a => a.Tag == CommonTags.Culture); - if (tagCulture != null) - { - var culture = CultureInfo.GetCultureInfo((string)tagCulture.Value); - CultureInfo.CurrentCulture = culture; - CultureInfo.CurrentUICulture = culture; - } } catch (Exception error) { diff --git a/web/ASC.Web.Core/Notify/StudioNotifySource.cs b/web/ASC.Web.Core/Notify/StudioNotifySource.cs index 36a0e67ca3..afa71eee74 100644 --- a/web/ASC.Web.Core/Notify/StudioNotifySource.cs +++ b/web/ASC.Web.Core/Notify/StudioNotifySource.cs @@ -29,8 +29,8 @@ namespace ASC.Web.Studio.Core.Notify; [Scope] public class StudioNotifySource : NotifySource { - public StudioNotifySource(UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager) - : base("asc.web.studio", userManager, recipientsProvider, subscriptionManager) + public StudioNotifySource(UserManager userManager, IRecipientProvider recipientsProvider, SubscriptionManager subscriptionManager, TenantManager tenantManager) + : base("asc.web.studio", userManager, recipientsProvider, subscriptionManager, tenantManager) { }