refactor: Bootstrap.Client MongoDB 代码优化
#Comment 移除 GroupHelper 与 UserHelper 静态类
This commit is contained in:
parent
5b0d65decd
commit
c57804faf8
|
@ -23,14 +23,20 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
public override IEnumerable<string> RetrievesByUserName(string userName)
|
public override IEnumerable<string> RetrievesByUserName(string userName)
|
||||||
{
|
{
|
||||||
var ret = new List<string>();
|
var ret = new List<string>();
|
||||||
var roles = RoleHelper.RetrievesByUserName(userName);
|
var user = UserHelper.RetrieveUserByUserName(userName) as User;
|
||||||
if (roles.Any(r => r.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
|
if (user != null)
|
||||||
{
|
{
|
||||||
ret.AddRange(RetrieveApps().Select(kv => kv.Key));
|
var roles = RoleHelper.Retrieves();
|
||||||
}
|
|
||||||
else
|
// check administrators
|
||||||
{
|
if (roles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
|
||||||
RoleHelper.Retrieves().Cast<Role>().Where(r => roles.Any(rn => rn == r.RoleName)).ToList().ForEach(r => ret.AddRange(r.Apps));
|
{
|
||||||
|
ret.AddRange(RetrieveApps().Select(kv => kv.Key));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
roles.Where(r => user.Roles.Any(role => role == r.Id)).ToList().ForEach(r => ret.AddRange(r.Apps));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,6 +155,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
BsonClassMap.RegisterClassMap<Group>(md =>
|
BsonClassMap.RegisterClassMap<Group>(md =>
|
||||||
{
|
{
|
||||||
md.AutoMap();
|
md.AutoMap();
|
||||||
|
md.SetIgnoreExtraElements(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!BsonClassMap.IsClassMapRegistered(typeof(Role)))
|
if (!BsonClassMap.IsClassMapRegistered(typeof(Role)))
|
||||||
|
@ -164,6 +165,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
md.AutoMap();
|
md.AutoMap();
|
||||||
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
|
||||||
md.IdMemberMap.SetIgnoreIfDefault(true);
|
md.IdMemberMap.SetIgnoreIfDefault(true);
|
||||||
|
md.SetIgnoreExtraElements(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using Bootstrap.Security;
|
using Bootstrap.Security;
|
||||||
using MongoDB.Driver;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Bootstrap.Client.DataAccess.MongoDB
|
namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
|
@ -9,23 +8,9 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class Group : BootstrapGroup
|
internal class Group : BootstrapGroup
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// 获得/设置 群组描述
|
|
||||||
/// </summary>
|
|
||||||
public string Description { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获得/设置 当前组授权角色数据集合
|
/// 获得/设置 当前组授权角色数据集合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<string> Roles { get; set; }
|
public IEnumerable<string> Roles { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获得所有组数据方法
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public IEnumerable<Group> Retrieves()
|
|
||||||
{
|
|
||||||
return DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
using Longbow.Cache;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Bootstrap.Client.DataAccess.MongoDB
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 组数据相关操作帮助类
|
|
||||||
/// </summary>
|
|
||||||
internal class GroupHelper
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 组数据缓存键值
|
|
||||||
/// </summary>
|
|
||||||
public const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取所有组数据集合方法
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<Group> Retrieves()
|
|
||||||
{
|
|
||||||
return CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,5 @@
|
||||||
using Longbow.Cache;
|
using Longbow.Cache;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
@ -21,23 +20,5 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IEnumerable<Role> Retrieves() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbManager.Roles.Find(FilterDefinition<Role>.Empty).ToList());
|
public static IEnumerable<Role> Retrieves() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbManager.Roles.Find(FilterDefinition<Role>.Empty).ToList());
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 通过指定用户名获取角色集合方法
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="userName"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<string> RetrievesByUserName(string userName)
|
|
||||||
{
|
|
||||||
var roles = new List<string>();
|
|
||||||
var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase));
|
|
||||||
if (user != null)
|
|
||||||
{
|
|
||||||
var role = Retrieves();
|
|
||||||
roles.AddRange(role.Where(r => user.Roles.Any(rl => rl == r.Id)).Select(r => r.RoleName));
|
|
||||||
if (roles.Count == 0) roles.Add("Default");
|
|
||||||
}
|
|
||||||
return roles;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
using Longbow.Cache;
|
|
||||||
using MongoDB.Driver;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Bootstrap.Client.DataAccess.MongoDB
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 用户表相关操作帮助类
|
|
||||||
/// </summary>
|
|
||||||
internal class UserHelper
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 获取所有用户缓存数据键值
|
|
||||||
/// </summary>
|
|
||||||
public const string RetrieveUsersDataKey = "UserHelper-RetrieveUsers";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 查询所有用户
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static IEnumerable<User> Retrieves() => CacheManager.GetOrAdd(RetrieveUsersDataKey, key =>
|
|
||||||
{
|
|
||||||
var project = Builders<User>.Projection.Include(u => u.Id)
|
|
||||||
.Include(u => u.UserName)
|
|
||||||
.Include(u => u.DisplayName)
|
|
||||||
.Include(u => u.Groups)
|
|
||||||
.Include(u => u.Roles)
|
|
||||||
.Include(u => u.ApprovedTime);
|
|
||||||
return DbManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,4 +1,5 @@
|
||||||
using Bootstrap.Security;
|
using Bootstrap.Security;
|
||||||
|
using Longbow.Cache;
|
||||||
using MongoDB.Driver;
|
using MongoDB.Driver;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -18,24 +19,28 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName)
|
public override IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName)
|
||||||
{
|
{
|
||||||
var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase));
|
var user = UserHelper.RetrieveUserByUserName(userName) as User;
|
||||||
if (user == null) return Enumerable.Empty<BootstrapMenu>();
|
if (user == null) return Enumerable.Empty<BootstrapMenu>();
|
||||||
|
|
||||||
var roles = RoleHelper.Retrieves().Cast<Role>();
|
var roles = RoleHelper.Retrieves();
|
||||||
var groups = GroupHelper.Retrieves().Cast<Group>();
|
var groups = DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
|
||||||
|
|
||||||
// 通过用户获取 组列表相关联的角色列表
|
// 通过用户获取 组列表相关联的角色列表
|
||||||
var userRoles = user.Groups.Aggregate(user.Roles.ToList(), (r, g) =>
|
var userRoles = user.Groups.Aggregate(user.Roles.ToList(), (r, g) =>
|
||||||
{
|
{
|
||||||
var groupRoles = groups.Where(group => group.Id == g).FirstOrDefault()?.Roles;
|
var groupRoles = groups.FirstOrDefault(group => group.Id == g)?.Roles;
|
||||||
if (groupRoles != null) r.AddRange(groupRoles);
|
if (groupRoles != null) r.AddRange(groupRoles);
|
||||||
return r;
|
return r;
|
||||||
}).Distinct().ToList();
|
}).Distinct().ToList();
|
||||||
|
|
||||||
var allRoles = roles.Where(r => userRoles.Any(rl => rl == r.Id)).ToList();
|
var allRoles = roles.Where(r => userRoles.Any(rl => rl == r.Id));
|
||||||
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList()
|
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
|
||||||
.Where(m => allRoles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase) || r.Menus.Any(rm => rm.Equals(m.Id, StringComparison.OrdinalIgnoreCase))))
|
|
||||||
.ToList();
|
// check administrators
|
||||||
|
if (!allRoles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
menus = menus.Where(m => allRoles.Any(r => r.Menus.Any(rm => rm == m.Id))).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
|
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
|
||||||
menus.ForEach(m =>
|
menus.ForEach(m =>
|
||||||
|
|
|
@ -30,11 +30,6 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string RoleName { get; set; }
|
public string RoleName { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获得/设置 角色描述
|
|
||||||
/// </summary>
|
|
||||||
public string Description { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过指定登录名获取授权角色名称数据集合
|
/// 通过指定登录名获取授权角色名称数据集合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -42,15 +37,8 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override IEnumerable<string> RetrievesByUserName(string userName)
|
public override IEnumerable<string> RetrievesByUserName(string userName)
|
||||||
{
|
{
|
||||||
var roles = new List<string>();
|
var user = UserHelper.RetrieveUserByUserName(userName) as User;
|
||||||
var user = UserHelper.Retrieves().FirstOrDefault(u => u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase));
|
return RoleHelper.Retrieves().Where(r => user.Roles.Any(ur => ur == r.Id)).Select(r => r.RoleName);
|
||||||
if (user != null)
|
|
||||||
{
|
|
||||||
var role = RoleHelper.Retrieves();
|
|
||||||
roles.AddRange(role.Where(r => user.Roles.Any(rl => rl == r.Id)).Select(r => r.RoleName));
|
|
||||||
if (roles.Count == 0) roles.Add("Default");
|
|
||||||
}
|
|
||||||
return roles;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -62,7 +50,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
public override IEnumerable<string> RetrievesByUrl(string url)
|
public override IEnumerable<string> RetrievesByUrl(string url)
|
||||||
{
|
{
|
||||||
var menu = DbManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
|
var menu = DbManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
|
||||||
var ret = RoleHelper.Retrieves().Where(md => md.Menus != null && md.Menus.Any(m => m.Equals(menu.Id, StringComparison.OrdinalIgnoreCase))).Select(m => m.RoleName).ToList();
|
var ret = RoleHelper.Retrieves().Where(md => md.Menus.Any(m => m == menu.Id)).Select(m => m.RoleName).ToList();
|
||||||
if (!ret.Any(r => r.Equals("Administrators", StringComparison.OrdinalIgnoreCase))) ret.Add("Administrators");
|
if (!ret.Any(r => r.Equals("Administrators", StringComparison.OrdinalIgnoreCase))) ret.Add("Administrators");
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,9 @@ namespace Bootstrap.Client.DataAccess.MongoDB
|
||||||
.Include(u => u.DisplayName)
|
.Include(u => u.DisplayName)
|
||||||
.Include(u => u.Icon)
|
.Include(u => u.Icon)
|
||||||
.Include(u => u.Css)
|
.Include(u => u.Css)
|
||||||
|
.Include(u => u.Roles)
|
||||||
|
.Include(u => u.Groups)
|
||||||
|
.Include(u => u.ApprovedTime)
|
||||||
.Include(u => u.App);
|
.Include(u => u.App);
|
||||||
var ret = DbManager.Users.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).Project<User>(project).FirstOrDefault();
|
var ret = DbManager.Users.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).Project<User>(project).FirstOrDefault();
|
||||||
if (ret != null)
|
if (ret != null)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using Bootstrap.Client.DataAccess;
|
using Bootstrap.Client.DataAccess;
|
||||||
using Bootstrap.Security.DataAccess;
|
|
||||||
using Longbow.Configuration;
|
using Longbow.Configuration;
|
||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||||
|
|
|
@ -45,5 +45,59 @@
|
||||||
"BootstrapAdminAuthenticationOptions": {
|
"BootstrapAdminAuthenticationOptions": {
|
||||||
"AuthHost": "http://localhost:50852"
|
"AuthHost": "http://localhost:50852"
|
||||||
},
|
},
|
||||||
"AllowOrigins": "http://localhost:50852"
|
"AllowOrigins": "http://localhost:50852",
|
||||||
}
|
"LongbowCache": {
|
||||||
|
"Enabled": true,
|
||||||
|
"CacheItems": [
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "RoleHelper-RetrieveRolesByUserName",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "指定用户角色数据缓存"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "RoleHelper-RetrieveRolesByUrl",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "通过菜单获得角色数据"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "AppHelper-RetrieveAppsByUserName",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "指定用户授权应用数据缓存"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "BootstrapUser-RetrieveUsersByName",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "登录用户数据"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "BootstrapDict-RetrieveDicts",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "所有字典数据缓存"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "BootstrapMenu-RetrieveMenus",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "用户所有菜单数据缓存"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Enabled": true,
|
||||||
|
"Key": "RoleHelper-RetrieveRoles",
|
||||||
|
"Interval": 600000,
|
||||||
|
"SlidingExpiration": true,
|
||||||
|
"Desc": "所有角色数据"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
|
@ -93,27 +93,6 @@
|
||||||
"Interval": 600000,
|
"Interval": 600000,
|
||||||
"SlidingExpiration": true,
|
"SlidingExpiration": true,
|
||||||
"Desc": "所有角色数据"
|
"Desc": "所有角色数据"
|
||||||
},
|
|
||||||
{
|
|
||||||
"Enabled": true,
|
|
||||||
"Key": "GroupHelper-RetrieveGroups",
|
|
||||||
"Interval": 600000,
|
|
||||||
"SlidingExpiration": true,
|
|
||||||
"Desc": "所有部门数据"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Enabled": true,
|
|
||||||
"Key": "UserHelper-RetrieveUsers",
|
|
||||||
"Interval": 600000,
|
|
||||||
"SlidingExpiration": true,
|
|
||||||
"Desc": "所有用户数据"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"Enabled": true,
|
|
||||||
"Key": "GroupHelper-RetrieveGroupsByUserName",
|
|
||||||
"Interval": 600000,
|
|
||||||
"SlidingExpiration": true,
|
|
||||||
"Desc": "指定用户组数据缓存"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue