重构代码:增加Bootstrap.Security.SQLServer组件,直接调用SQLServer数据库

This commit is contained in:
Argo-MacBookPro 2018-10-25 17:52:04 +08:00
parent e4e7bb2449
commit e4e4d3bd8d
11 changed files with 38 additions and 191 deletions

View File

@ -46,7 +46,7 @@ namespace Bootstrap.Admin.Controllers.Api
///
/// </summary>
/// <param name="id"></param>
/// <param name="value"></param>
/// <param name="type"></param>
/// <returns></returns>
[HttpPost("{id}")]
public IEnumerable<BootstrapMenu> Post(int id, [FromQuery]string type)

View File

@ -11,10 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security" Version="1.0.4" />
<PackageReference Include="Longbow" Version="1.0.2" />
<PackageReference Include="Longbow.Configuration" Version="1.0.3" />
<PackageReference Include="Longbow.Data" Version="1.0.3" />
<PackageReference Include="Bootstrap.Security.SQLServer" Version="1.0.0" />
<PackageReference Include="Longbow.Web" Version="1.0.1" />
</ItemGroup>

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
using Longbow.Configuration;
using System;
using System.Collections.Generic;
@ -47,7 +48,7 @@ namespace Bootstrap.Client.DataAccess
///
/// </summary>
/// <returns></returns>
private static IEnumerable<BootstrapDict> RetrieveDicts() => BAHelper.RetrieveDicts();
private static IEnumerable<BootstrapDict> RetrieveDicts() => BASQLHelper.RetrieveDicts();
private static string RetrieveAppName(string name, string defaultValue = "未设置")
{

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
using Longbow.Configuration;
using System.Collections.Generic;
@ -15,6 +16,6 @@ namespace Bootstrap.Client.DataAccess
/// <param name="userName"></param>
/// <param name="url"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveAppMenus(string userName, string url) => BAHelper.RetrieveAppMenus(ConfigurationManager.AppSettings["AppId"], userName, url);
public static IEnumerable<BootstrapMenu> RetrieveAppMenus(string userName, string url) => BASQLHelper.RetrieveAppMenus(ConfigurationManager.AppSettings["AppId"], userName, url);
}
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
using System.Collections.Generic;
namespace Bootstrap.Client.DataAccess
@ -13,12 +14,12 @@ namespace Bootstrap.Client.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => BAHelper.RetrieveRolesByUserName(userName);
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => BASQLHelper.RetrieveRolesByUserName(userName);
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUrl(string url) => BAHelper.RetrieveRolesByUrl(url);
public static IEnumerable<string> RetrieveRolesByUrl(string url) => BASQLHelper.RetrieveRolesByUrl(url);
}
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
namespace Bootstrap.Client.DataAccess
{
@ -12,6 +13,6 @@ namespace Bootstrap.Client.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static BootstrapUser RetrieveUserByUserName(string userName)=> BAHelper.RetrieveUserByUserName(userName);
public static BootstrapUser RetrieveUserByUserName(string userName) => BASQLHelper.RetrieveUserByUserName(userName);
}
}

View File

@ -11,10 +11,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security" Version="1.0.4" />
<PackageReference Include="Bootstrap.Security.SQLServer" Version="1.0.0" />
<PackageReference Include="Longbow.Web" Version="1.0.1" />
<PackageReference Include="Longbow.Cache" Version="1.0.2" />
<PackageReference Include="Longbow.Data" Version="1.0.3" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,5 @@
using Bootstrap.Security;
using Longbow;
using Bootstrap.Security.SQLServer;
using Longbow.Cache;
using System;
using System.Collections.Generic;
@ -176,27 +176,6 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="db">数据库连接实例</param>
/// <returns></returns>
public virtual IEnumerable<BootstrapDict> RetrieveDicts() => CacheManager.GetOrAdd(RetrieveDictsDataKey, key =>
{
string sql = "select ID, Category, Name, Code, Define from Dicts";
var Dicts = new List<BootstrapDict>();
var db = DbAccessManager.DBAccess;
var cmd = db.CreateCommand(CommandType.Text, sql);
using (var reader = db.ExecuteReader(cmd))
{
while (reader.Read())
{
Dicts.Add(new BootstrapDict
{
Id = LgbConvert.ReadValue(reader[0], 0),
Category = (string)reader[1],
Name = (string)reader[2],
Code = (string)reader[3],
Define = LgbConvert.ReadValue(reader[4], 0)
});
}
}
return Dicts;
});
public virtual IEnumerable<BootstrapDict> RetrieveDicts() => BASQLHelper.RetrieveDicts();
}
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
using Longbow;
using Longbow.Cache;
using Longbow.Data;
@ -20,7 +21,13 @@ namespace Bootstrap.DataAccess
///
/// </summary>
public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
/// <summary>
///
/// </summary>
public const string RetrieveMenusDataKey = "BootstrapMenu-RetrieveMenusByUserName";
/// <summary>
///
/// </summary>
public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus";
/// <summary>
/// 删除菜单信息
@ -70,7 +77,6 @@ namespace Bootstrap.DataAccess
CacheCleanUtility.ClearCache(menuIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
return ret;
}
/// <summary>
/// 查询某个角色所配置的菜单
/// </summary>
@ -148,13 +154,12 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <param name="connName">连接字符串名称默认为ba</param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName, string activeUrl = null)
{
var menus = RetrieveMenusByUserName(userName, activeUrl);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.Category).ThenBy(m => m.ApplicationCode).ThenBy(m => m.Order);
CascadeMenus(menus, root);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
/// <summary>
@ -164,31 +169,7 @@ namespace Bootstrap.DataAccess
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string activeUrl = null)
{
var menus = RetrieveMenusByUserName(userName, activeUrl, appId).Where(m => m.Category == "1" && m.IsResource == 0);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);
CascadeMenus(menus, root);
return root;
}
/// <summary>
/// 通过当前用户名获得所有菜单
/// </summary>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <param name="appId"></param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName, string activeUrl = null, string appId = "0")
{
var key = string.Format("{0}-{1}-{2}", RetrieveMenusDataKey, userName, appId);
var navs = CacheManager.GetOrAdd(key, k =>
{
var menus = RetrieveAllMenus(userName);
return appId == "0" ? menus : menus.Where(m => m.ApplicationCode == appId);
}, RetrieveMenusDataKey);
if (!string.IsNullOrEmpty(activeUrl)) ActiveMenu(null, navs, activeUrl);
return navs;
}
public virtual IEnumerable<BootstrapMenu> RetrieveAppMenus(string appId, string userName, string activeUrl = null) => BASQLHelper.RetrieveAppMenus(appId, userName, activeUrl);
/// <summary>
/// 通过当前用户名获得后台菜单,层次化后集合
/// </summary>
@ -201,65 +182,16 @@ namespace Bootstrap.DataAccess
{
var menus = RetrieveMenusByUserName(userName, activeUrl).Where(m => m.Category == "0" && m.IsResource == 0);
var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);
CascadeMenus(menus, root);
BASQLHelper.CascadeMenus(menus, root);
return root;
}
private static IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName)
{
return CacheManager.GetOrAdd(RetrieveMenusAll, k =>
{
var menus = new List<BootstrapMenu>();
var db = DbAccessManager.DBAccess;
using (DbCommand cmd = db.CreateCommand(CommandType.Text, "select n.ID, n.ParentId, n.Name, n.[Order], n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.[Application], d.Name as CategoryName, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = @Category and d.Define = 0 left join Navigations ln on n.ParentId = ln.ID inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID"))
{
cmd.Parameters.Add(db.CreateParameter("@UserName", userName));
cmd.Parameters.Add(db.CreateParameter("@Category", "菜单"));
cmd.Parameters.Add(db.CreateParameter("@RoleName", "Administrators"));
using (DbDataReader reader = db.ExecuteReader(cmd))
{
while (reader.Read())
{
menus.Add(new BootstrapMenu
{
Id = LgbConvert.ReadValue(reader[0], 0),
ParentId = LgbConvert.ReadValue(reader[1], 0),
Name = (string)reader[2],
Order = LgbConvert.ReadValue(reader[3], 0),
Icon = reader.IsDBNull(4) ? string.Empty : (string)reader[4],
Url = reader.IsDBNull(5) ? string.Empty : (string)reader[5],
Category = (string)reader[6],
Target = (string)reader[7],
IsResource = LgbConvert.ReadValue(reader[8], false) ? 1 : 0,
ApplicationCode = reader.IsDBNull(9) ? string.Empty : (string)reader[9],
CategoryName = (string)reader[10],
ParentName = reader.IsDBNull(11) ? string.Empty : (string)reader[11],
});
}
}
}
return menus;
});
}
private static void CascadeMenus(IEnumerable<BootstrapMenu> navs, IEnumerable<BootstrapMenu> level)
{
level.ToList().ForEach(m =>
{
m.Menus = navs.Where(sub => sub.ParentId == m.Id).OrderBy(sub => sub.Order);
CascadeMenus(navs, m.Menus);
});
}
private static void ActiveMenu(BootstrapMenu parent, IEnumerable<BootstrapMenu> menus, string url)
{
if (menus == null || !menus.Any()) return;
menus.AsParallel().ForAll(m =>
{
m.Active = m.Url.Equals(url, StringComparison.OrdinalIgnoreCase) ? "active" : "";
ActiveMenu(m, m.Menus, url);
if (parent != null && m.Active != "") parent.Active = m.Active;
});
}
/// <summary>
/// 通过当前用户名获得所有菜单
/// </summary>
/// <param name="userName">当前登陆的用户名</param>
/// <param name="activeUrl">当前访问菜单</param>
/// <param name="appId"></param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByUserName(string userName, string activeUrl = null, string appId = "0") => BASQLHelper.RetrieveMenusByUserName(userName, activeUrl, appId);
}
}

View File

@ -1,6 +1,6 @@
using Longbow;
using Bootstrap.Security.SQLServer;
using Longbow;
using Longbow.Cache;
using Longbow.Configuration;
using Longbow.Data;
using System;
using System.Collections.Generic;
@ -288,7 +288,6 @@ namespace Bootstrap.DataAccess
return roles;
}, RetrieveRolesByGroupIdDataKey);
}
/// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary>
@ -340,51 +339,13 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public virtual IEnumerable<string> RetrieveRolesByUserName(string userName)
{
return CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), r =>
{
var entities = new List<string>();
var db = DbAccessManager.DBAccess;
using (DbCommand cmd = db.CreateCommand(CommandType.Text, "select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @UserName union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName=@UserName"))
{
cmd.Parameters.Add(db.CreateParameter("@UserName", userName));
using (DbDataReader reader = db.ExecuteReader(cmd))
{
while (reader.Read())
{
entities.Add((string)reader[0]);
}
}
}
return entities;
}, RetrieveRolesByUserNameDataKey);
}
public virtual IEnumerable<string> RetrieveRolesByUserName(string userName) => BASQLHelper.RetrieveRolesByUserName(userName);
/// <summary>
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查
/// 从Navigators-〉GroupNavigatorRole-〉Role查查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
public virtual IEnumerable<string> RetrieveRolesByUrl(string url)
{
return CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), k =>
{
string sql = "select distinct r.RoleName, r.[Description] from Roles r inner join NavigationRole nr on r.ID = nr.RoleID inner join Navigations n on nr.NavigationID = n.ID and n.[Application] = @AppId and n.Url like @Url";
var Roles = new List<string> { "Administrators" };
var db = DbAccessManager.DBAccess;
var cmd = db.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(db.CreateParameter("@Url", string.Format("{0}%", url)));
cmd.Parameters.Add(db.CreateParameter("@AppId", LgbConvert.ReadValue(ConfigurationManager.AppSettings["AppId"], "0")));
using (DbDataReader reader = db.ExecuteReader(cmd))
{
while (reader.Read())
{
Roles.Add((string)reader[0]);
}
}
return Roles;
}, RetrieveRolesByUrlDataKey);
}
public virtual IEnumerable<string> RetrieveRolesByUrl(string url) => BASQLHelper.RetrieveRolesByUrl(url);
}
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Bootstrap.Security.SQLServer;
using Longbow;
using Longbow.Cache;
using Longbow.Data;
@ -490,32 +491,7 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public virtual BootstrapUser RetrieveUserByUserName(string userName)
{
var key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
return CacheManager.GetOrAdd(key, k =>
{
BootstrapUser user = null;
var sql = "select UserName, DisplayName, case isnull(d.Code, '') when '' then '~/images/uploader/' else d.Code end + Icon Icon, u.Css from Users u left join Dicts d on d.Define = '0' and d.Category = N'头像地址' and Name = N'头像路径' where ApprovedTime is not null and UserName = @UserName";
var db = DbAccessManager.DBAccess;
var cmd = db.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(db.CreateParameter("@UserName", userName));
using (DbDataReader reader = db.ExecuteReader(cmd))
{
if (reader.Read())
{
user = new BootstrapUser
{
UserName = (string)reader[0],
DisplayName = (string)reader[1],
Icon = (string)reader[2],
Css = reader.IsDBNull(3) ? string.Empty : (string)reader[3]
};
}
}
return user;
}, RetrieveUsersByNameDataKey);
}
public virtual BootstrapUser RetrieveUserByUserName(string userName) => BASQLHelper.RetrieveUserByUserName(userName);
/// <summary>
///
/// </summary>