From 3f5ca7c7d60ea0a40bb375faad8b3e81bc49c76f Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 30 Jun 2019 23:18:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=8D=87=E7=BA=A7=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E7=BB=84=E4=BB=B6DbHelper=E5=A2=9E=E5=8A=A0=E5=A4=A7?= =?UTF-8?q?=E9=87=8F=E9=9D=99=E6=80=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Comment Bootstrap.Client 工程使用 DbHelper 静态方法 --- Bootstrap.Admin/Bootstrap.Admin.csproj | 4 +- Bootstrap.Admin/appsettings.json | 9 +- .../Bootstrap.Client.DataAccess.csproj | 2 +- Bootstrap.Client.DataAccess/DictHelper.cs | 88 +------------------ Bootstrap.Client.DataAccess/MenuHelper.cs | 36 -------- Bootstrap.Client.DataAccess/RoleHelper.cs | 27 ------ Bootstrap.Client.DataAccess/UserHelper.cs | 20 ----- Bootstrap.Client/Bootstrap.Client.csproj | 4 +- .../Controllers/HomeController.cs | 5 +- Bootstrap.Client/Models/HeaderBarModel.cs | 12 +-- Bootstrap.Client/Models/ModelBase.cs | 9 +- Bootstrap.Client/Models/NavigatorBarModel.cs | 5 +- Bootstrap.Client/Startup.cs | 18 +++- Bootstrap.Client/appsettings.Development.json | 1 + Bootstrap.Client/appsettings.json | 7 ++ Bootstrap.DataAccess/Helper/RoleHelper.cs | 16 +--- Bootstrap.DataAccess/Role.cs | 2 +- 17 files changed, 63 insertions(+), 202 deletions(-) delete mode 100644 Bootstrap.Client.DataAccess/MenuHelper.cs delete mode 100644 Bootstrap.Client.DataAccess/RoleHelper.cs delete mode 100644 Bootstrap.Client.DataAccess/UserHelper.cs diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj index e3036fc4..aa65fb3f 100644 --- a/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -12,8 +12,8 @@ - - + + diff --git a/Bootstrap.Admin/appsettings.json b/Bootstrap.Admin/appsettings.json index 98b3bca8..76b328ab 100644 --- a/Bootstrap.Admin/appsettings.json +++ b/Bootstrap.Admin/appsettings.json @@ -51,7 +51,7 @@ ], "SwaggerPathBase": "/BA", "AllowOrigins": "http://localhost,http://argo.zylweb.cn", - "Sentry" : { + "Sentry": { "Dsn": "https://70bdfff562e84fa7b9a43d65924ab9ad@sentry.io/1469396" }, "LongbowCache": { @@ -87,6 +87,13 @@ "SlidingExpiration": true, "Desc": "通过菜单获得角色数据" }, + { + "Enabled": true, + "Key": "AppHelper-RetrieveAppsByUserName", + "Interval": 600000, + "SlidingExpiration": true, + "Desc": "指定用户授权应用数据缓存" + }, { "Enabled": true, "Key": "BootstrapUser-RetrieveUsersByName", diff --git a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index 796ea5ae..4e1b388a 100644 --- a/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -5,7 +5,7 @@ - + diff --git a/Bootstrap.Client.DataAccess/DictHelper.cs b/Bootstrap.Client.DataAccess/DictHelper.cs index 9e654e7a..54a3a2d9 100644 --- a/Bootstrap.Client.DataAccess/DictHelper.cs +++ b/Bootstrap.Client.DataAccess/DictHelper.cs @@ -1,9 +1,4 @@ -using Bootstrap.Security; -using Bootstrap.Security.DataAccess; -using Longbow.Cache; -using Longbow.Configuration; -using System; -using System.Collections.Generic; +using Bootstrap.Security.DataAccess; using System.Linq; namespace Bootstrap.Client.DataAccess @@ -13,89 +8,10 @@ namespace Bootstrap.Client.DataAccess /// public static class DictHelper { - /// - /// 缓存索引,BootstrapAdmin后台清理缓存时使用 - /// - private const string RetrieveDictsDataKey = "BootstrapDict-RetrieveDicts"; - - /// - /// - /// - /// - public static string RetrieveProfilesUrl() - { - return RetrieveAppName("个人中心地址"); - } - - /// - /// - /// - /// - public static string RetrieveSettingsUrl() - { - return RetrieveAppName("系统设置地址"); - } - - /// - /// - /// - /// - public static string RetrieveNotisUrl() - { - return RetrieveAppName("系统通知地址"); - } - - /// - /// - /// - /// - public static string RetrieveTitle() - { - return RetrieveAppName("网站标题"); - } - - /// - /// - /// - /// - public static string RetrieveFooter() - { - return RetrieveAppName("网站页脚"); - } - - /// - /// - /// - /// - private static IEnumerable RetrieveDicts() => CacheManager.GetOrAdd(RetrieveDictsDataKey, key => DbHelper.RetrieveDicts()); - - private static string RetrieveAppName(string name, string defaultValue = "未设置") - { - var dicts = RetrieveDicts(); - var platName = dicts.FirstOrDefault(d => d.Category == "应用程序" && d.Code == ConfigurationManager.AppSettings["AppId"])?.Name; - return dicts.FirstOrDefault(d => d.Category == platName && d.Name == name)?.Code ?? $"{name}{defaultValue}"; - } - - /// - /// 获得网站设置中的当前样式 - /// - /// - public static string RetrieveActiveTheme() - { - var theme = RetrieveDicts().Where(d => d.Name == "使用样式" && d.Category == "当前样式" && d.Define == 0).FirstOrDefault()?.Code; - return theme == null ? string.Empty : theme.Equals("site.css", StringComparison.OrdinalIgnoreCase) ? string.Empty : theme; - } - - /// - /// 获取头像路径 - /// - /// - public static string RetrieveIconFolderPath() => (RetrieveDicts().FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == 0) ?? new BootstrapDict() { Code = "~/images/uploader/" }).Code; - /// /// 获取验证码图床 /// /// - public static string RetrieveImagesLibUrl() => RetrieveDicts().FirstOrDefault(d => d.Name == "验证码图床" && d.Category == "系统设置" && d.Define == 0)?.Code ?? "http://images.sdgxgz.com/"; + public static string RetrieveImagesLibUrl() => DbHelper.RetrieveDictsWithCache().FirstOrDefault(d => d.Name == "验证码图床" && d.Category == "系统设置" && d.Define == 0)?.Code ?? "http://images.sdgxgz.com/"; } } diff --git a/Bootstrap.Client.DataAccess/MenuHelper.cs b/Bootstrap.Client.DataAccess/MenuHelper.cs deleted file mode 100644 index 470974ae..00000000 --- a/Bootstrap.Client.DataAccess/MenuHelper.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Bootstrap.Security; -using Bootstrap.Security.DataAccess; -using Longbow.Cache; -using Longbow.Configuration; -using System.Collections.Generic; -using System.Linq; - -namespace Bootstrap.Client.DataAccess -{ - /// - /// - /// - public static class MenuHelper - { - private const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus"; - - /// - /// - /// - /// - /// - /// - public static IEnumerable RetrieveAppMenus(string userName, string activeUrl) - { - var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0 && m.Application == ConfigurationManager.AppSettings["AppId"]); - return DbHelper.CascadeMenus(menus, activeUrl); - } - - /// - /// 通过用户获得所有菜单 - /// - /// - /// - private static IEnumerable RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbHelper.RetrieveAllMenus(userName), RetrieveMenusAll); - } -} diff --git a/Bootstrap.Client.DataAccess/RoleHelper.cs b/Bootstrap.Client.DataAccess/RoleHelper.cs deleted file mode 100644 index 26bb098f..00000000 --- a/Bootstrap.Client.DataAccess/RoleHelper.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Bootstrap.Security.DataAccess; -using Longbow.Cache; -using System.Collections.Generic; - -namespace Bootstrap.Client.DataAccess -{ - /// - /// - /// - public static class RoleHelper - { - private const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName"; - private const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl"; - /// - /// - /// - /// - /// - public static IEnumerable RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbHelper.RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey); - /// - /// - /// - /// - /// - public static IEnumerable RetrieveRolesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbHelper.RetrieveRolesByUrl(url), RetrieveRolesByUrlDataKey); - } -} \ No newline at end of file diff --git a/Bootstrap.Client.DataAccess/UserHelper.cs b/Bootstrap.Client.DataAccess/UserHelper.cs deleted file mode 100644 index 85d8144c..00000000 --- a/Bootstrap.Client.DataAccess/UserHelper.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Bootstrap.Security; -using Bootstrap.Security.DataAccess; -using Longbow.Cache; - -namespace Bootstrap.Client.DataAccess -{ - /// - /// 用户表相关操作类 - /// - public static class UserHelper - { - private const string RetrieveUsersByNameDataKey = "BootstrapUser-RetrieveUsersByName"; - /// - /// - /// - /// - /// - public static BootstrapUser RetrieveUserByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName), k => DbHelper.RetrieveUserByUserName(userName), RetrieveUsersByNameDataKey); - } -} diff --git a/Bootstrap.Client/Bootstrap.Client.csproj b/Bootstrap.Client/Bootstrap.Client.csproj index 5dde12bf..fe26d8c9 100644 --- a/Bootstrap.Client/Bootstrap.Client.csproj +++ b/Bootstrap.Client/Bootstrap.Client.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/Bootstrap.Client/Controllers/HomeController.cs b/Bootstrap.Client/Controllers/HomeController.cs index 5980dc3b..82fb4b62 100644 --- a/Bootstrap.Client/Controllers/HomeController.cs +++ b/Bootstrap.Client/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using Bootstrap.Client.Models; using Longbow.Configuration; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; @@ -32,11 +33,13 @@ namespace Bootstrap.Client.Controllers /// /// /// + /// /// [AllowAnonymous] public IActionResult Error(int id) { - var uriBuilder = new UriBuilder(ConfigurationManager.AppSettings["AuthHost"]) { Query = QueryString.Create(CookieAuthenticationDefaults.ReturnUrlParameter, $"{Request.Scheme}://{Request.Host}{Request.PathBase}").ToString() }; + var options = ConfigurationManager.Get(); + var uriBuilder = new UriBuilder(options.AuthHost) { Query = QueryString.Create(CookieAuthenticationDefaults.ReturnUrlParameter, $"{Request.Scheme}://{Request.Host}{Request.PathBase}").ToString() }; uriBuilder.Path = uriBuilder.Path == "/" ? Request.Path.Value : uriBuilder.Path + Request.Path.Value; return Redirect(uriBuilder.ToString()); } diff --git a/Bootstrap.Client/Models/HeaderBarModel.cs b/Bootstrap.Client/Models/HeaderBarModel.cs index 56c9bdfa..332fd4cf 100644 --- a/Bootstrap.Client/Models/HeaderBarModel.cs +++ b/Bootstrap.Client/Models/HeaderBarModel.cs @@ -1,4 +1,4 @@ -using Bootstrap.Client.DataAccess; +using Bootstrap.Security.DataAccess; using Longbow.Configuration; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Authentication.Cookies; @@ -18,12 +18,12 @@ namespace Bootstrap.Client.Models /// public HeaderBarModel(IIdentity identity) { - var user = UserHelper.RetrieveUserByUserName(identity.Name); + var user = DbHelper.RetrieveUserByUserName(identity.Name); DisplayName = user.DisplayName; UserName = user.UserName; - SettingsUrl = DictHelper.RetrieveSettingsUrl(); - ProfilesUrl = DictHelper.RetrieveProfilesUrl(); - NotisUrl = DictHelper.RetrieveNotisUrl(); + SettingsUrl = DbHelper.RetrieveSettingsUrl(); + ProfilesUrl = DbHelper.RetrieveProfilesUrl(); + NotisUrl = DbHelper.RetrieveNotisUrl(); // set LogoutUrl var authHost = ConfigurationManager.Get().AuthHost; @@ -32,7 +32,7 @@ namespace Bootstrap.Client.Models LogoutUrl = uriBuilder.ToString(); // set Icon - var icon = $"/{DictHelper.RetrieveIconFolderPath().Trim('~', '/')}/{user.Icon}"; + var icon = $"/{DbHelper.RetrieveIconFolderPath().Trim('~', '/')}/{user.Icon}"; Icon = $"{authHost.TrimEnd('/')}{icon}"; if (!string.IsNullOrEmpty(user.Css)) Theme = user.Css; } diff --git a/Bootstrap.Client/Models/ModelBase.cs b/Bootstrap.Client/Models/ModelBase.cs index f7d53c40..4c1898b5 100644 --- a/Bootstrap.Client/Models/ModelBase.cs +++ b/Bootstrap.Client/Models/ModelBase.cs @@ -1,4 +1,5 @@ using Bootstrap.Client.DataAccess; +using Bootstrap.Security.DataAccess; namespace Bootstrap.Client.Models { @@ -12,9 +13,9 @@ namespace Bootstrap.Client.Models /// public ModelBase() { - Title = DictHelper.RetrieveTitle(); - Footer = DictHelper.RetrieveFooter(); - Theme = DictHelper.RetrieveActiveTheme(); + Title = DbHelper.RetrieveTitle(); + Footer = DbHelper.RetrieveFooter(); + Theme = DbHelper.RetrieveActiveTheme(); } /// /// @@ -29,4 +30,4 @@ namespace Bootstrap.Client.Models /// public string Theme { get; protected set; } } -} \ No newline at end of file +} diff --git a/Bootstrap.Client/Models/NavigatorBarModel.cs b/Bootstrap.Client/Models/NavigatorBarModel.cs index ac29f40e..577b1140 100644 --- a/Bootstrap.Client/Models/NavigatorBarModel.cs +++ b/Bootstrap.Client/Models/NavigatorBarModel.cs @@ -1,5 +1,6 @@ using Bootstrap.Client.DataAccess; using Bootstrap.Security; +using Bootstrap.Security.DataAccess; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; @@ -16,7 +17,7 @@ namespace Bootstrap.Client.Models /// public NavigatorBarModel(ControllerBase controller) : base(controller.User.Identity) { - Navigations = MenuHelper.RetrieveAppMenus(UserName, $"~/{controller.ControllerContext.ActionDescriptor.ControllerName}/{controller.ControllerContext.ActionDescriptor.ActionName}"); + Navigations = DbHelper.RetrieveAppCascadeMenus(UserName, $"~/{controller.ControllerContext.ActionDescriptor.ControllerName}/{controller.ControllerContext.ActionDescriptor.ActionName}"); ImageLibUrl = DictHelper.RetrieveImagesLibUrl(); } @@ -30,4 +31,4 @@ namespace Bootstrap.Client.Models /// public string ImageLibUrl { get; set; } } -} \ No newline at end of file +} diff --git a/Bootstrap.Client/Startup.cs b/Bootstrap.Client/Startup.cs index a1afd781..b130d67b 100644 --- a/Bootstrap.Client/Startup.cs +++ b/Bootstrap.Client/Startup.cs @@ -1,7 +1,9 @@ using Bootstrap.Client.DataAccess; using Bootstrap.Security.DataAccess; +using Longbow.Configuration; using Longbow.Web; using Longbow.Web.SignalR; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -11,6 +13,7 @@ using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; +using System.Security.Claims; namespace Bootstrap.Client { @@ -90,7 +93,20 @@ namespace Bootstrap.Client app.UseResponseCompression(); app.UseStaticFiles(); app.UseCookiePolicy(); - app.UseBootstrapAdminAuthentication(RoleHelper.RetrieveRolesByUserName, RoleHelper.RetrieveRolesByUrl, DbHelper.RetrieveAppsByUserName); +#if DEBUG + app.Use(async (context, next) => + { + var userName = ConfigurationManager.GetValue("SimulateUserName", string.Empty); + if (!string.IsNullOrEmpty(userName)) + { + var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); + identity.AddClaim(new Claim(ClaimTypes.Name, userName)); + context.User = new ClaimsPrincipal(identity); + } + await next(); + }); +#endif + app.UseBootstrapAdminAuthentication(); app.UseCacheManagerCorsHandler(); app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); app.UseMvc(routes => diff --git a/Bootstrap.Client/appsettings.Development.json b/Bootstrap.Client/appsettings.Development.json index 2ddbfab9..53c73a59 100644 --- a/Bootstrap.Client/appsettings.Development.json +++ b/Bootstrap.Client/appsettings.Development.json @@ -30,6 +30,7 @@ } } ], + "SimulateUserName": "Admin", "BootstrapAdminOptions": { "AuthHost": "http://localhost:50852" }, diff --git a/Bootstrap.Client/appsettings.json b/Bootstrap.Client/appsettings.json index 6a7e85d6..59509234 100644 --- a/Bootstrap.Client/appsettings.json +++ b/Bootstrap.Client/appsettings.json @@ -50,6 +50,13 @@ "SlidingExpiration": true, "Desc": "通过菜单获得角色数据" }, + { + "Enabled": true, + "Key": "AppHelper-RetrieveAppsByUserName", + "Interval": 600000, + "SlidingExpiration": true, + "Desc": "指定用户授权应用数据缓存" + }, { "Enabled": true, "Key": "BootstrapUser-RetrieveUsersByName", diff --git a/Bootstrap.DataAccess/Helper/RoleHelper.cs b/Bootstrap.DataAccess/Helper/RoleHelper.cs index 81d98fb6..49420ac7 100644 --- a/Bootstrap.DataAccess/Helper/RoleHelper.cs +++ b/Bootstrap.DataAccess/Helper/RoleHelper.cs @@ -1,4 +1,5 @@ -using Longbow.Cache; +using Bootstrap.Security.DataAccess; +using Longbow.Cache; using Longbow.Data; using System; using System.Collections.Generic; @@ -27,15 +28,6 @@ namespace Bootstrap.DataAccess /// /// public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId"; - /// - /// - /// - public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName"; - /// - /// - /// - public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl"; - /// /// 查询所有角色 /// @@ -137,13 +129,13 @@ namespace Bootstrap.DataAccess /// /// /// - public static IEnumerable RetrievesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbContextManager.Create().RetrievesByUserName(userName), RetrieveRolesByUserNameDataKey); + public static IEnumerable RetrievesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", DbHelper.RetrieveRolesByUserNameDataKey, userName), key => DbContextManager.Create().RetrievesByUserName(userName), DbHelper.RetrieveRolesByUserNameDataKey); /// /// /// /// /// - public static IEnumerable RetrievesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbContextManager.Create().RetrievesByUrl(url), RetrieveRolesByUrlDataKey); + public static IEnumerable RetrievesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", DbHelper.RetrieveRolesByUrlDataKey, url), key => DbContextManager.Create().RetrievesByUrl(url), DbHelper.RetrieveRolesByUrlDataKey); } } diff --git a/Bootstrap.DataAccess/Role.cs b/Bootstrap.DataAccess/Role.cs index 8d1c4894..ea0ef7bf 100644 --- a/Bootstrap.DataAccess/Role.cs +++ b/Bootstrap.DataAccess/Role.cs @@ -192,7 +192,7 @@ namespace Bootstrap.DataAccess /// /// 根据菜单url查询某个所拥有的角色 /// 从NavigatorRole表查 - /// 从Navigators-〉GroupNavigatorRole-〉Role查查询某个用户所拥有的角色 + /// 从Navigators -> GroupNavigatorRole -> Role查查询某个用户所拥有的角色 /// /// public virtual IEnumerable RetrievesByUrl(string url) => DbHelper.RetrieveRolesByUrl(url);