From 7e75a73ddec9123099e006949585b3ef0e24ae57 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 9 Feb 2023 10:37:21 +0800 Subject: [PATCH] Introduce `ApplicationConfigurationChangedService`. --- .../ApplicationConfigurationChangedService.cs | 15 ++++++++++++++ ...blyCachedApplicationConfigurationClient.cs | 20 +++++++++++++++---- .../Themes/Basic/NavMenu.razor.cs | 18 +++++++++-------- .../Themes/Basic/NavToolbar.razor.cs | 11 +++++----- .../Pages/Authentication.razor | 15 ++++++++++++-- .../Themes/Basic/LoginDisplay.razor.cs | 13 ++++++------ 6 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Security/ApplicationConfigurationChangedService.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Security/ApplicationConfigurationChangedService.cs b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Security/ApplicationConfigurationChangedService.cs new file mode 100644 index 0000000000..9803a7991d --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.Web/Volo/Abp/AspNetCore/Components/Web/Security/ApplicationConfigurationChangedService.cs @@ -0,0 +1,15 @@ +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Components.Web.Security; + +public delegate void ApplicationConfigurationChangedHandler(); + +public class ApplicationConfigurationChangedService : IScopedDependency +{ + public event ApplicationConfigurationChangedHandler Changed; + + public void NotifyChanged() + { + Changed?.Invoke(); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCachedApplicationConfigurationClient.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCachedApplicationConfigurationClient.cs index c3ba260a88..79a2dacd9a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCachedApplicationConfigurationClient.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyCachedApplicationConfigurationClient.cs @@ -1,4 +1,6 @@ using System.Threading.Tasks; +using Microsoft.AspNetCore.Components.Authorization; +using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ClientProxies; using Volo.Abp.AspNetCore.Mvc.Client; @@ -10,23 +12,31 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly; public class WebAssemblyCachedApplicationConfigurationClient : ICachedApplicationConfigurationClient, ITransientDependency { protected AbpApplicationConfigurationClientProxy ApplicationConfigurationClientProxy { get; } - + protected AbpApplicationLocalizationClientProxy ApplicationLocalizationClientProxy { get; } protected ApplicationConfigurationCache Cache { get; } protected ICurrentTenantAccessor CurrentTenantAccessor { get; } + protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; } + + protected AuthenticationStateProvider AuthenticationStateProvider { get; } + public WebAssemblyCachedApplicationConfigurationClient( AbpApplicationConfigurationClientProxy applicationConfigurationClientProxy, ApplicationConfigurationCache cache, - ICurrentTenantAccessor currentTenantAccessor, - AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy) + ICurrentTenantAccessor currentTenantAccessor, + AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy, + ApplicationConfigurationChangedService applicationConfigurationChangedService, + AuthenticationStateProvider authenticationStateProvider) { ApplicationConfigurationClientProxy = applicationConfigurationClientProxy; Cache = cache; CurrentTenantAccessor = currentTenantAccessor; ApplicationLocalizationClientProxy = applicationLocalizationClientProxy; + ApplicationConfigurationChangedService = applicationConfigurationChangedService; + AuthenticationStateProvider = authenticationStateProvider; } public virtual async Task InitializeAsync() @@ -45,9 +55,11 @@ public class WebAssemblyCachedApplicationConfigurationClient : ICachedApplicatio ); configurationDto.Localization.Resources = localizationDto.Resources; - + Cache.Set(configurationDto); + ApplicationConfigurationChangedService.NotifyChanged(); + CurrentTenantAccessor.Current = new BasicTenantInfo( configurationDto.CurrentTenant.Id, configurationDto.CurrentTenant.Name diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavMenu.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavMenu.razor.cs index fb117369eb..5b9c07ad44 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavMenu.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavMenu.razor.cs @@ -1,7 +1,9 @@ using System; +using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.UI.Navigation; namespace Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic; @@ -12,24 +14,24 @@ public partial class NavMenu : IDisposable protected IMenuManager MenuManager { get; set; } [Inject] - protected AuthenticationStateProvider AuthenticationStateProvider { get; set; } + protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; set; } protected ApplicationMenu Menu { get; set; } - protected override async Task OnInitializedAsync() + protected async override Task OnInitializedAsync() { Menu = await MenuManager.GetMainMenuAsync(); - AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProviderOnAuthenticationStateChanged; + ApplicationConfigurationChangedService.Changed += ApplicationConfigurationChanged; } - public void Dispose() + private async void ApplicationConfigurationChanged() { - AuthenticationStateProvider.AuthenticationStateChanged -= AuthenticationStateProviderOnAuthenticationStateChanged; + Menu = await MenuManager.GetMainMenuAsync(); + await InvokeAsync(StateHasChanged); } - private async void AuthenticationStateProviderOnAuthenticationStateChanged(Task task) + public void Dispose() { - Menu = await MenuManager.GetMainMenuAsync(); - await InvokeAsync(StateHasChanged); + ApplicationConfigurationChangedService.Changed -= ApplicationConfigurationChanged; } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs index 8e1040ae89..9a0d76afa2 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.Web.BasicTheme/Themes/Basic/NavToolbar.razor.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.AspNetCore.Components.Web.Theming.Toolbars; namespace Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic; @@ -14,14 +15,14 @@ public partial class NavToolbar : IDisposable private IToolbarManager ToolbarManager { get; set; } [Inject] - private AuthenticationStateProvider AuthenticationStateProvider { get; set; } + protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; set; } private List ToolbarItemRenders { get; set; } = new List(); - protected override async Task OnInitializedAsync() + protected async override Task OnInitializedAsync() { await GetToolbarItemRendersAsync(); - AuthenticationStateProvider.AuthenticationStateChanged += AuthenticationStateProviderOnAuthenticationStateChanged; + ApplicationConfigurationChangedService.Changed += ApplicationConfigurationChanged; } private async Task GetToolbarItemRendersAsync() @@ -41,7 +42,7 @@ public partial class NavToolbar : IDisposable } } - private async void AuthenticationStateProviderOnAuthenticationStateChanged(Task task) + private async void ApplicationConfigurationChanged() { await GetToolbarItemRendersAsync(); await InvokeAsync(StateHasChanged); @@ -49,6 +50,6 @@ public partial class NavToolbar : IDisposable public void Dispose() { - AuthenticationStateProvider.AuthenticationStateChanged -= AuthenticationStateProviderOnAuthenticationStateChanged; + ApplicationConfigurationChangedService.Changed -= ApplicationConfigurationChanged; } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Pages/Authentication.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Pages/Authentication.razor index e237cea555..1b9fe309a2 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Pages/Authentication.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Pages/Authentication.razor @@ -1,7 +1,18 @@ @page "/authentication/{action}" @using Microsoft.AspNetCore.Components.WebAssembly.Authentication - - +@using Volo.Abp.AspNetCore.Components.Web.Security + +@inject WebAssemblyCachedApplicationConfigurationClient WebAssemblyCachedApplicationConfigurationClient @code{ [Parameter] public string Action { get; set; } + + private async Task OnLogInSucceeded(RemoteAuthenticationState state) + { + await WebAssemblyCachedApplicationConfigurationClient.InitializeAsync(); + } + + private async Task OnLogOutSucceeded(RemoteAuthenticationState state) + { + await WebAssemblyCachedApplicationConfigurationClient.InitializeAsync(); + } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs index 92577ff937..4312369b2b 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.JSInterop; +using Volo.Abp.AspNetCore.Components.Web.Security; using Volo.Abp.UI.Navigation; namespace Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme.Themes.Basic; @@ -16,18 +17,17 @@ public partial class LoginDisplay : IDisposable protected IMenuManager MenuManager { get; set; } [Inject] - public AuthenticationStateProvider AuthenticationStateProvider { get; set; } + protected ApplicationConfigurationChangedService ApplicationConfigurationChangedService { get; set; } protected ApplicationMenu Menu { get; set; } - protected override async Task OnInitializedAsync() + protected async override Task OnInitializedAsync() { Menu = await MenuManager.GetAsync(StandardMenus.User); Navigation.LocationChanged += OnLocationChanged; - AuthenticationStateProvider.AuthenticationStateChanged += - AuthenticationStateProviderOnAuthenticationStateChanged; + ApplicationConfigurationChangedService.Changed += ApplicationConfigurationChanged; } protected virtual void OnLocationChanged(object sender, LocationChangedEventArgs e) @@ -35,7 +35,7 @@ public partial class LoginDisplay : IDisposable InvokeAsync(StateHasChanged); } - private async void AuthenticationStateProviderOnAuthenticationStateChanged(Task task) + private async void ApplicationConfigurationChanged() { Menu = await MenuManager.GetAsync(StandardMenus.User); await InvokeAsync(StateHasChanged); @@ -44,8 +44,7 @@ public partial class LoginDisplay : IDisposable public void Dispose() { Navigation.LocationChanged -= OnLocationChanged; - AuthenticationStateProvider.AuthenticationStateChanged -= - AuthenticationStateProviderOnAuthenticationStateChanged; + ApplicationConfigurationChangedService.Changed -= ApplicationConfigurationChanged; } private async Task NavigateToAsync(string uri, string target = null)