refactor: 升级依赖组件DbHelper增加大量静态方法

#Comment
Bootstrap.Client 工程使用 DbHelper 静态方法
This commit is contained in:
Argo Zhang 2019-06-30 23:18:33 +08:00
parent 63798634a8
commit 3f5ca7c7d6
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
17 changed files with 63 additions and 202 deletions

View File

@ -12,8 +12,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.9" />
<PackageReference Include="Longbow.Configuration" Version="2.2.3" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.10" />
<PackageReference Include="Longbow.Configuration" Version="2.2.4" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.7.10" />

View File

@ -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",

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.5" />
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.6" />
<PackageReference Include="Longbow.Cache" Version="2.2.12" />
<PackageReference Include="Longbow.Data" Version="2.3.2" />
<PackageReference Include="Longbow.Web" Version="2.2.14" />

View File

@ -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
/// </summary>
public static class DictHelper
{
/// <summary>
/// 缓存索引BootstrapAdmin后台清理缓存时使用
/// </summary>
private const string RetrieveDictsDataKey = "BootstrapDict-RetrieveDicts";
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string RetrieveProfilesUrl()
{
return RetrieveAppName("个人中心地址");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string RetrieveSettingsUrl()
{
return RetrieveAppName("系统设置地址");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string RetrieveNotisUrl()
{
return RetrieveAppName("系统通知地址");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string RetrieveTitle()
{
return RetrieveAppName("网站标题");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string RetrieveFooter()
{
return RetrieveAppName("网站页脚");
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private static IEnumerable<BootstrapDict> 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}";
}
/// <summary>
/// 获得网站设置中的当前样式
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 获取头像路径
/// </summary>
/// <returns></returns>
public static string RetrieveIconFolderPath() => (RetrieveDicts().FirstOrDefault(d => d.Name == "头像路径" && d.Category == "头像地址" && d.Define == 0) ?? new BootstrapDict() { Code = "~/images/uploader/" }).Code;
/// <summary>
/// 获取验证码图床
/// </summary>
/// <returns></returns>
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/";
}
}

View File

@ -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
{
/// <summary>
///
/// </summary>
public static class MenuHelper
{
private const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="activeUrl"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> 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);
}
/// <summary>
/// 通过用户获得所有菜单
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
private static IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbHelper.RetrieveAllMenus(userName), RetrieveMenusAll);
}
}

View File

@ -1,27 +0,0 @@
using Bootstrap.Security.DataAccess;
using Longbow.Cache;
using System.Collections.Generic;
namespace Bootstrap.Client.DataAccess
{
/// <summary>
///
/// </summary>
public static class RoleHelper
{
private const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
private const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbHelper.RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey);
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbHelper.RetrieveRolesByUrl(url), RetrieveRolesByUrlDataKey);
}
}

View File

@ -1,20 +0,0 @@
using Bootstrap.Security;
using Bootstrap.Security.DataAccess;
using Longbow.Cache;
namespace Bootstrap.Client.DataAccess
{
/// <summary>
/// 用户表相关操作类
/// </summary>
public static class UserHelper
{
private const string RetrieveUsersByNameDataKey = "BootstrapUser-RetrieveUsersByName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static BootstrapUser RetrieveUserByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName), k => DbHelper.RetrieveUserByUserName(userName), RetrieveUsersByNameDataKey);
}
}

View File

@ -7,8 +7,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.9" />
<PackageReference Include="Longbow.Configuration" Version="2.2.3" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.10" />
<PackageReference Include="Longbow.Configuration" Version="2.2.4" />
<PackageReference Include="Longbow.Logging" Version="2.2.8" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

View File

@ -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
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[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<BootstrapAdminOptions>();
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());
}

View File

@ -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
/// <param name="identity"></param>
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<BootstrapAdminOptions>().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;
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Client.DataAccess;
using Bootstrap.Security.DataAccess;
namespace Bootstrap.Client.Models
{
@ -12,9 +13,9 @@ namespace Bootstrap.Client.Models
/// </summary>
public ModelBase()
{
Title = DictHelper.RetrieveTitle();
Footer = DictHelper.RetrieveFooter();
Theme = DictHelper.RetrieveActiveTheme();
Title = DbHelper.RetrieveTitle();
Footer = DbHelper.RetrieveFooter();
Theme = DbHelper.RetrieveActiveTheme();
}
/// <summary>
///
@ -29,4 +30,4 @@ namespace Bootstrap.Client.Models
/// </summary>
public string Theme { get; protected set; }
}
}
}

View File

@ -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
/// <param name="controller"></param>
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
/// </summary>
public string ImageLibUrl { get; set; }
}
}
}

View File

@ -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<SignalRHub>("/NotiHub"); });
app.UseMvc(routes =>

View File

@ -30,6 +30,7 @@
}
}
],
"SimulateUserName": "Admin",
"BootstrapAdminOptions": {
"AuthHost": "http://localhost:50852"
},

View File

@ -50,6 +50,13 @@
"SlidingExpiration": true,
"Desc": "通过菜单获得角色数据"
},
{
"Enabled": true,
"Key": "AppHelper-RetrieveAppsByUserName",
"Interval": 600000,
"SlidingExpiration": true,
"Desc": "指定用户授权应用数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapUser-RetrieveUsersByName",

View File

@ -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
///
/// </summary>
public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId";
/// <summary>
///
/// </summary>
public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
/// <summary>
///
/// </summary>
public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
/// <summary>
/// 查询所有角色
/// </summary>
@ -137,13 +129,13 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<string> RetrievesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbContextManager.Create<Role>().RetrievesByUserName(userName), RetrieveRolesByUserNameDataKey);
public static IEnumerable<string> RetrievesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", DbHelper.RetrieveRolesByUserNameDataKey, userName), key => DbContextManager.Create<Role>().RetrievesByUserName(userName), DbHelper.RetrieveRolesByUserNameDataKey);
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static IEnumerable<string> RetrievesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbContextManager.Create<Role>().RetrievesByUrl(url), RetrieveRolesByUrlDataKey);
public static IEnumerable<string> RetrievesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", DbHelper.RetrieveRolesByUrlDataKey, url), key => DbContextManager.Create<Role>().RetrievesByUrl(url), DbHelper.RetrieveRolesByUrlDataKey);
}
}

View File

@ -192,7 +192,7 @@ namespace Bootstrap.DataAccess
/// <summary>
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查
/// 从Navigators-〉GroupNavigatorRole-〉Role查查询某个用户所拥有的角色
/// 从Navigators -> GroupNavigatorRole -> Role查查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
public virtual IEnumerable<string> RetrievesByUrl(string url) => DbHelper.RetrieveRolesByUrl(url);