From de37ea3c6bd3b396965c05c7a3d77f65f5bfc7e3 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sat, 24 Aug 2019 10:21:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E5=81=A5?= =?UTF-8?q?=E5=BA=B7=E6=A3=80=E6=9F=A5=E4=BD=BF=E7=94=A8=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/HealthChecks/DBHealthCheck.cs | 25 ++++++++++++------- Bootstrap.Admin/Startup.cs | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs index 7a08914d..24a1045e 100644 --- a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -1,4 +1,5 @@ using Bootstrap.DataAccess; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Diagnostics.HealthChecks; using System; @@ -15,7 +16,8 @@ namespace Bootstrap.Admin.HealthChecks /// public class DBHealthCheck : IHealthCheck { - private IConfiguration _configuration; + private readonly IConfiguration _configuration; + private readonly IHttpContextAccessor _httpContextAccessor; private static readonly Func ConnectionStringResolve = (c, name) => string.IsNullOrEmpty(name) ? c.GetSection("ConnectionStrings").GetChildren().FirstOrDefault()?.Value : c.GetConnectionString(name); @@ -24,9 +26,11 @@ namespace Bootstrap.Admin.HealthChecks /// 构造函数 /// /// - public DBHealthCheck(IConfiguration configuration) + /// + public DBHealthCheck(IConfiguration configuration, IHttpContextAccessor httpContextAccessor) { _configuration = configuration; + _httpContextAccessor = httpContextAccessor; } /// @@ -46,10 +50,12 @@ namespace Bootstrap.Admin.HealthChecks ConnectionString = ConnectionStringResolve(config.GetSection("ConnectionStrings").Exists() ? config : _configuration, string.Empty) }).FirstOrDefault(i => i.Enabled); - // 检查 Admin 账户权限 - var user = UserHelper.RetrieveUserByUserName("Admin"); - var roles = RoleHelper.RetrievesByUserName("Admin"); - var menus = MenuHelper.RetrieveMenusByUserName("Admin"); + // 检查 当前用户 账户权限 + var loginUser = _httpContextAccessor.HttpContext.User.Identity.Name; + var userName = loginUser ?? "Admin"; + var user = UserHelper.RetrieveUserByUserName(userName); + var roles = RoleHelper.RetrievesByUserName(userName); + var menus = MenuHelper.RetrieveMenusByUserName(userName); var dicts = DictHelper.RetrieveDicts(); var data = new Dictionary() @@ -58,9 +64,10 @@ namespace Bootstrap.Admin.HealthChecks { "Widget", db.Widget }, { "DbType", db.ProviderName }, { "Dicts", dicts.Count() }, - { "User(Admin)", user != null }, - { "Roles(Admin)", string.Join(",", roles) }, - { "Navigations(Admin)", menus.Count() } + { "LoginName", loginUser }, + { "DisplayName", user == null ? null : user.DisplayName }, + { "Roles", string.Join(",", roles) }, + { "Navigations", menus.Count() } }; var v = dicts.Any() && user != null && roles.Any() && menus.Any(); diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 7b0fe510..3a63de54 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -109,8 +109,8 @@ namespace Bootstrap.Admin app.UseHttpsRedirection(); app.UseResponseCompression(); app.UseStaticFiles(); - app.UseBootstrapHealthChecks(); app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); + app.UseBootstrapHealthChecks(); app.UseOnlineUsers(callback: TraceHelper.Save); app.UseCacheManager(); app.UseSignalR(routes =>