refactor: Bootstrap.Client MongoDB 代码优化

#Comment
移除 GroupHelper 与 UserHelper 静态类
This commit is contained in:
Argo Zhang 2019-09-01 13:54:43 +08:00
parent 5b0d65decd
commit c57804faf8
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
12 changed files with 90 additions and 148 deletions

View File

@ -23,14 +23,20 @@ namespace Bootstrap.Client.DataAccess.MongoDB
public override IEnumerable<string> RetrievesByUserName(string userName)
{
var ret = new List<string>();
var roles = RoleHelper.RetrievesByUserName(userName);
if (roles.Any(r => r.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
var user = UserHelper.RetrieveUserByUserName(userName) as User;
if (user != null)
{
ret.AddRange(RetrieveApps().Select(kv => kv.Key));
}
else
{
RoleHelper.Retrieves().Cast<Role>().Where(r => roles.Any(rn => rn == r.RoleName)).ToList().ForEach(r => ret.AddRange(r.Apps));
var roles = RoleHelper.Retrieves();
// check administrators
if (roles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase)))
{
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;
}

View File

@ -155,6 +155,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB
BsonClassMap.RegisterClassMap<Group>(md =>
{
md.AutoMap();
md.SetIgnoreExtraElements(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(Role)))
@ -164,6 +165,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB
md.AutoMap();
md.IdMemberMap.SetSerializer(new StringSerializer(BsonType.ObjectId));
md.IdMemberMap.SetIgnoreIfDefault(true);
md.SetIgnoreExtraElements(true);
});
}
}

View File

@ -1,5 +1,4 @@
using Bootstrap.Security;
using MongoDB.Driver;
using System.Collections.Generic;
namespace Bootstrap.Client.DataAccess.MongoDB
@ -9,23 +8,9 @@ namespace Bootstrap.Client.DataAccess.MongoDB
/// </summary>
internal class Group : BootstrapGroup
{
/// <summary>
/// 获得/设置 群组描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 获得/设置 当前组授权角色数据集合
/// </summary>
public IEnumerable<string> Roles { get; set; }
/// <summary>
/// 获得所有组数据方法
/// </summary>
/// <returns></returns>
public IEnumerable<Group> Retrieves()
{
return DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
}
}
}

View File

@ -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());
}
}
}

View File

@ -1,6 +1,5 @@
using Longbow.Cache;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
@ -21,23 +20,5 @@ namespace Bootstrap.Client.DataAccess.MongoDB
/// </summary>
/// <returns></returns>
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;
}
}
}

View File

@ -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();
});
}
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Security;
using Longbow.Cache;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
@ -18,24 +19,28 @@ namespace Bootstrap.Client.DataAccess.MongoDB
/// <returns></returns>
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>();
var roles = RoleHelper.Retrieves().Cast<Role>();
var groups = GroupHelper.Retrieves().Cast<Group>();
var roles = RoleHelper.Retrieves();
var groups = DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
// 通过用户获取 组列表相关联的角色列表
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);
return r;
}).Distinct().ToList();
var allRoles = roles.Where(r => userRoles.Any(rl => rl == r.Id)).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();
var allRoles = roles.Where(r => userRoles.Any(rl => rl == r.Id));
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).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 == "菜单");
menus.ForEach(m =>

View File

@ -30,11 +30,6 @@ namespace Bootstrap.Client.DataAccess.MongoDB
/// </summary>
public string RoleName { get; set; }
/// <summary>
/// 获得/设置 角色描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 通过指定登录名获取授权角色名称数据集合
/// </summary>
@ -42,15 +37,8 @@ namespace Bootstrap.Client.DataAccess.MongoDB
/// <returns></returns>
public override 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 = 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;
var user = UserHelper.RetrieveUserByUserName(userName) as User;
return RoleHelper.Retrieves().Where(r => user.Roles.Any(ur => ur == r.Id)).Select(r => r.RoleName);
}
/// <summary>
@ -62,7 +50,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB
public override IEnumerable<string> RetrievesByUrl(string url)
{
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");
return ret;
}

View File

@ -42,6 +42,9 @@ namespace Bootstrap.Client.DataAccess.MongoDB
.Include(u => u.DisplayName)
.Include(u => u.Icon)
.Include(u => u.Css)
.Include(u => u.Roles)
.Include(u => u.Groups)
.Include(u => u.ApprovedTime)
.Include(u => u.App);
var ret = DbManager.Users.Find(user => user.UserName.ToLowerInvariant() == userName.ToLowerInvariant()).Project<User>(project).FirstOrDefault();
if (ret != null)

View File

@ -1,5 +1,4 @@
using Bootstrap.Client.DataAccess;
using Bootstrap.Security.DataAccess;
using Longbow.Configuration;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Authentication.Cookies;

View File

@ -45,5 +45,59 @@
"BootstrapAdminAuthenticationOptions": {
"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": "所有角色数据"
}
]
}
}

View File

@ -93,27 +93,6 @@
"Interval": 600000,
"SlidingExpiration": true,
"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": "指定用户组数据缓存"
}
]
}