From b07a181db731e4ed80564d626b3d81423a5175e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 7 Jan 2020 15:31:40 +0300 Subject: [PATCH] Fixed #2568: User menu in Module.Web.Host not working. --- .../Menus/MyProjectNameMenuContributor.cs | 9 ++- .../Controllers/AccountController.cs | 9 +++ .../MyProjectNameWebHostMenuContributor.cs | 60 +++++++++++++++++++ .../MyProjectNameWebHostModule.cs | 12 +++- .../Localization/MyProjectName/en.json | 2 +- .../Localization/MyProjectName/tr.json | 6 ++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Controllers/AccountController.cs create mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostMenuContributor.cs create mode 100644 templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/tr.json diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Menus/MyProjectNameMenuContributor.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Menus/MyProjectNameMenuContributor.cs index f7ef2c2093..2a12f2ee32 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Menus/MyProjectNameMenuContributor.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Menus/MyProjectNameMenuContributor.cs @@ -8,6 +8,7 @@ using MyCompanyName.MyProjectName.MultiTenancy; using Volo.Abp.Account.Localization; using Volo.Abp.TenantManagement.Web.Navigation; using Volo.Abp.UI.Navigation; +using Volo.Abp.Users; namespace MyCompanyName.MyProjectName.Web.Menus { @@ -51,11 +52,15 @@ namespace MyCompanyName.MyProjectName.Web.Menus { var l = context.ServiceProvider.GetRequiredService>(); var accountStringLocalizer = context.ServiceProvider.GetRequiredService>(); + var currentUser = context.ServiceProvider.GetRequiredService(); var identityServerUrl = _configuration["AuthServer:Authority"] ?? ""; - context.Menu.AddItem(new ApplicationMenuItem("Account.Manage", accountStringLocalizer["ManageYourProfile"], $"{identityServerUrl.EnsureEndsWith('/')}Account/Manage", icon: "fa fa-cog", order: 1000, null, "_blank")); - context.Menu.AddItem(new ApplicationMenuItem("Account.Logout", l["Logout"], url: "/Account/Logout", icon: "fa fa-power-off", order: int.MaxValue - 1000)); + if (currentUser.IsAuthenticated) + { + context.Menu.AddItem(new ApplicationMenuItem("Account.Manage", accountStringLocalizer["ManageYourProfile"], $"{identityServerUrl.EnsureEndsWith('/')}Account/Manage", icon: "fa fa-cog", order: 1000, null, "_blank")); + context.Menu.AddItem(new ApplicationMenuItem("Account.Logout", l["Logout"], url: "/Account/Logout", icon: "fa fa-power-off", order: int.MaxValue - 1000)); + } return Task.CompletedTask; } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Controllers/AccountController.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Controllers/AccountController.cs new file mode 100644 index 0000000000..318e7191a2 --- /dev/null +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Controllers/AccountController.cs @@ -0,0 +1,9 @@ +using Volo.Abp.AspNetCore.Mvc.Authentication; + +namespace MyCompanyName.MyProjectName.Controllers +{ + public class AccountController : ChallengeAccountController + { + + } +} diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostMenuContributor.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostMenuContributor.cs new file mode 100644 index 0000000000..430a1c237c --- /dev/null +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostMenuContributor.cs @@ -0,0 +1,60 @@ +using System; +using System.Threading.Tasks; +using Localization.Resources.AbpUi; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; +using MyCompanyName.MyProjectName.Localization; +using Volo.Abp.UI.Navigation; +using Volo.Abp.Users; + +namespace MyCompanyName.MyProjectName +{ + public class MyProjectNameWebHostMenuContributor : IMenuContributor + { + private readonly IConfiguration _configuration; + + public MyProjectNameWebHostMenuContributor(IConfiguration configuration) + { + _configuration = configuration; + } + + public Task ConfigureMenuAsync(MenuConfigurationContext context) + { + if (context.Menu.Name == StandardMenus.User) + { + AddLogoutItemToMenu(context); + } + + return Task.CompletedTask; + } + + private void AddLogoutItemToMenu(MenuConfigurationContext context) + { + var currentUser = context.ServiceProvider.GetRequiredService(); + var l = context.ServiceProvider.GetRequiredService>(); + + if (currentUser.IsAuthenticated) + { + context.Menu.Items.Add(new ApplicationMenuItem( + "Account.Manage", + l["ManageYourProfile"], + $"{_configuration["AuthServer:Authority"].EnsureEndsWith('/')}Account/Manage", + icon: "fa fa-cog", + order: int.MaxValue - 1001, + null, + "_blank") + ); + + + context.Menu.Items.Add(new ApplicationMenuItem( + "Account.Logout", + l["Logout"], + "/Account/Logout", + "fas fa-power-off", + order: int.MaxValue - 1000 + )); + } + } + } +} \ No newline at end of file diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs index 6eb3dc9ea3..473bdab2cf 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/MyProjectNameWebHostModule.cs @@ -37,6 +37,7 @@ using Volo.Abp.TenantManagement; using Volo.Abp.TenantManagement.Web; using Volo.Abp.UI.Navigation.Urls; using Volo.Abp.UI; +using Volo.Abp.UI.Navigation; using Volo.Abp.VirtualFileSystem; namespace MyCompanyName.MyProjectName @@ -76,6 +77,7 @@ namespace MyCompanyName.MyProjectName var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); + ConfigureMenu(configuration); ConfigureCache(configuration); ConfigureUrls(configuration); ConfigureAuthentication(context, configuration); @@ -85,7 +87,15 @@ namespace MyCompanyName.MyProjectName ConfigureMultiTenancy(); ConfigureRedis(context, configuration, hostingEnvironment); } - + + private void ConfigureMenu(IConfiguration configuration) + { + Configure(options => + { + options.MenuContributors.Add(new MyProjectNameWebHostMenuContributor(configuration)); + }); + } + private void ConfigureCache(IConfiguration configuration) { Configure(options => diff --git a/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/en.json b/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/en.json index 92e4e9582b..171d9a1220 100644 --- a/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/en.json +++ b/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/en.json @@ -1,6 +1,6 @@ { "culture": "en", "texts": { - + "ManageYourProfile": "Manage your profile" } } \ No newline at end of file diff --git a/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/tr.json b/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/tr.json new file mode 100644 index 0000000000..2b48193353 --- /dev/null +++ b/templates/module/aspnet-core/src/MyCompanyName.MyProjectName.Domain.Shared/Localization/MyProjectName/tr.json @@ -0,0 +1,6 @@ +{ + "culture": "tr", + "texts": { + "ManageYourProfile": "Profil yönetimi" + } +} \ No newline at end of file