Browse Source

Use user-defined menus in Blazor's LoginDisplay.

Fix #5970
pull/5988/head
maliming 6 years ago
parent
commit
8716099880
  1. 20
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor
  2. 21
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs
  3. 2
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/MyProjectNameBlazorModule.cs
  4. 49
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/MyProjectNameMenuContributor.cs

20
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor

@ -1,6 +1,8 @@
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Volo.Abp.Users
@using Volo.Abp.MultiTenancy
@using Volo.Abp.UI.Navigation
@inject IJSRuntime JsRuntime
@inject ICurrentUser CurrentUser
@inject ICurrentTenant CurrentTenant
@inject NavigationManager Navigation
@ -23,12 +25,9 @@
{
@foreach (var menuItem in Menu.Items)
{
<DropdownItem Clicked="@(() => NavigateTo(menuItem.Url))">@menuItem.DisplayName</DropdownItem>
<DropdownItem Clicked="@(() => NavigateToAsync(menuItem.Url, menuItem.Target))">@menuItem.DisplayName</DropdownItem>
}
}
<a class="dropdown-item" href="@ServerAccountUrl">
@UiLocalizer["ManageYourAccount"]
</a>
<DropdownDivider />
<DropdownItem Clicked="BeginSignOut">Logout</DropdownItem>
</DropdownMenu>
@ -40,14 +39,21 @@
</AuthorizeView>
@code{
private void NavigateTo(string uri)
private async Task NavigateToAsync(string uri, string target = "_self")
{
Navigation.NavigateTo(uri);
if (target == "_blank")
{
await JsRuntime.InvokeVoidAsync("open", uri, "_blank");
}
else
{
Navigation.NavigateTo(uri);
}
}
private async Task BeginSignOut()
{
await SignOutManager.SetSignOutState();
NavigateTo("authentication/logout");
await NavigateToAsync("authentication/logout");
}
}

21
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs

@ -12,39 +12,32 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme.Themes.Basic
{
public partial class LoginDisplay : IDisposable
{
[Inject]
[Inject]
protected IMenuManager MenuManager { get; set; }
[Inject]
protected IStringLocalizer<AbpUiResource> UiLocalizer { get; set; }
[Inject]
protected IOptions<AbpRemoteServiceOptions> RemoteServiceOptions { get; set; }
protected ApplicationMenu Menu { get; set; }
protected string ServerUrl { get; set; }
protected string ServerAccountUrl { get; set; }
protected ApplicationMenu Menu { get; set; }
protected override async Task OnInitializedAsync()
{
Menu = await MenuManager.GetAsync(StandardMenus.User);
ServerUrl = RemoteServiceOptions.Value.RemoteServices.Default?.BaseUrl?.TrimEnd('/');
ServerAccountUrl = ServerUrl + "/Account/Manage?returnUrl=" + Navigation.Uri;
Navigation.LocationChanged += OnLocationChanged;
}
protected virtual void OnLocationChanged(object sender, LocationChangedEventArgs e)
{
ServerAccountUrl = ServerUrl + "/Account/Manage?returnUrl=" + Navigation.Uri;
StateHasChanged();
}
public void Dispose()
{
Navigation.LocationChanged -= OnLocationChanged;
}
}
}
}

2
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/MyProjectNameBlazorModule.cs

@ -55,7 +55,7 @@ namespace MyCompanyName.MyProjectName.Blazor
{
Configure<AbpNavigationOptions>(options =>
{
options.MenuContributors.Add(new MyProjectNameMenuContributor());
options.MenuContributors.Add(new MyProjectNameMenuContributor(context.Services.GetConfiguration()));
});
}

49
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/MyProjectNameMenuContributor.cs

@ -1,18 +1,37 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyCompanyName.MyProjectName.Localization;
using Volo.Abp.Account.Localization;
using Volo.Abp.UI.Navigation;
using Volo.Abp.Users;
namespace MyCompanyName.MyProjectName.Blazor
{
public class MyProjectNameMenuContributor : IMenuContributor
{
public Task ConfigureMenuAsync(MenuConfigurationContext context)
private readonly IConfiguration _configuration;
public MyProjectNameMenuContributor(IConfiguration configuration)
{
if(context.Menu.DisplayName != StandardMenus.Main)
_configuration = configuration;
}
public async Task ConfigureMenuAsync(MenuConfigurationContext context)
{
if (context.Menu.Name == StandardMenus.Main)
{
return Task.CompletedTask;
await ConfigureMainMenuAsync(context);
}
else if (context.Menu.Name == StandardMenus.User)
{
await ConfigureUserMenuAsync(context);
}
}
private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
{
var l = context.GetLocalizer<MyProjectNameResource>();
context.Menu.Items.Insert(
@ -27,5 +46,27 @@ namespace MyCompanyName.MyProjectName.Blazor
return Task.CompletedTask;
}
private Task ConfigureUserMenuAsync(MenuConfigurationContext context)
{
var accountStringLocalizer = context.GetLocalizer<AccountResource>();
var currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();
var identityServerUrl = _configuration["AuthServer:Authority"] ?? "";
if (currentUser.IsAuthenticated)
{
context.Menu.AddItem(new ApplicationMenuItem(
"Account.Manage",
accountStringLocalizer["ManageYourProfile"],
$"{identityServerUrl.EnsureEndsWith('/')}Account/Manage",
icon: "fa fa-cog",
order: 1000,
null,
"_blank"));
}
return Task.CompletedTask;
}
}
}

Loading…
Cancel
Save