diff --git a/Bootstrap.Client.DataAccess.MongoDB/App.cs b/Bootstrap.Client.DataAccess.MongoDB/App.cs index a0dfe6d3..f8150397 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/App.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/App.cs @@ -23,14 +23,20 @@ namespace Bootstrap.Client.DataAccess.MongoDB public override IEnumerable RetrievesByUserName(string userName) { var ret = new List(); - 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().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; } diff --git a/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs b/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs index 4625c46d..f3f430dd 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/DbManager.cs @@ -155,6 +155,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB BsonClassMap.RegisterClassMap(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); }); } } diff --git a/Bootstrap.Client.DataAccess.MongoDB/Group.cs b/Bootstrap.Client.DataAccess.MongoDB/Group.cs index 13276773..51fb1821 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/Group.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/Group.cs @@ -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 /// internal class Group : BootstrapGroup { - /// - /// 获得/设置 群组描述 - /// - public string Description { get; set; } - /// /// 获得/设置 当前组授权角色数据集合 /// public IEnumerable Roles { get; set; } - - /// - /// 获得所有组数据方法 - /// - /// - public IEnumerable Retrieves() - { - return DbManager.Groups.Find(FilterDefinition.Empty).ToList(); - } } } diff --git a/Bootstrap.Client.DataAccess.MongoDB/Helper/GroupHelper.cs b/Bootstrap.Client.DataAccess.MongoDB/Helper/GroupHelper.cs deleted file mode 100644 index e5a0b894..00000000 --- a/Bootstrap.Client.DataAccess.MongoDB/Helper/GroupHelper.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Longbow.Cache; -using MongoDB.Driver; -using System.Collections.Generic; -using System.Linq; - -namespace Bootstrap.Client.DataAccess.MongoDB -{ - /// - /// 组数据相关操作帮助类 - /// - internal class GroupHelper - { - /// - /// 组数据缓存键值 - /// - public const string RetrieveGroupsDataKey = "GroupHelper-RetrieveGroups"; - - /// - /// 获取所有组数据集合方法 - /// - /// - public static IEnumerable Retrieves() - { - return CacheManager.GetOrAdd(RetrieveGroupsDataKey, key => DbManager.Groups.Find(FilterDefinition.Empty).ToList()); - } - } -} diff --git a/Bootstrap.Client.DataAccess.MongoDB/Helper/RoleHelper.cs b/Bootstrap.Client.DataAccess.MongoDB/Helper/RoleHelper.cs index 77128ac3..d4e0250d 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/Helper/RoleHelper.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/Helper/RoleHelper.cs @@ -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 /// /// public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbManager.Roles.Find(FilterDefinition.Empty).ToList()); - - /// - /// 通过指定用户名获取角色集合方法 - /// - /// - /// - public static IEnumerable RetrievesByUserName(string userName) - { - var roles = new List(); - 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; - } } } diff --git a/Bootstrap.Client.DataAccess.MongoDB/Helper/UserHelper.cs b/Bootstrap.Client.DataAccess.MongoDB/Helper/UserHelper.cs deleted file mode 100644 index ae4dd6a2..00000000 --- a/Bootstrap.Client.DataAccess.MongoDB/Helper/UserHelper.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Longbow.Cache; -using MongoDB.Driver; -using System; -using System.Collections.Generic; - -namespace Bootstrap.Client.DataAccess.MongoDB -{ - /// - /// 用户表相关操作帮助类 - /// - internal class UserHelper - { - /// - /// 获取所有用户缓存数据键值 - /// - public const string RetrieveUsersDataKey = "UserHelper-RetrieveUsers"; - - /// - /// 查询所有用户 - /// - /// - public static IEnumerable Retrieves() => CacheManager.GetOrAdd(RetrieveUsersDataKey, key => - { - var project = Builders.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(project).ToList(); - }); - } -} diff --git a/Bootstrap.Client.DataAccess.MongoDB/Menu.cs b/Bootstrap.Client.DataAccess.MongoDB/Menu.cs index 1e16a602..6c6a421c 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/Menu.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/Menu.cs @@ -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 /// public override IEnumerable 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(); - var roles = RoleHelper.Retrieves().Cast(); - var groups = GroupHelper.Retrieves().Cast(); + var roles = RoleHelper.Retrieves(); + var groups = DbManager.Groups.Find(FilterDefinition.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.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.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 => diff --git a/Bootstrap.Client.DataAccess.MongoDB/Role.cs b/Bootstrap.Client.DataAccess.MongoDB/Role.cs index 02635646..0fddc518 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/Role.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/Role.cs @@ -30,11 +30,6 @@ namespace Bootstrap.Client.DataAccess.MongoDB /// public string RoleName { get; set; } - /// - /// 获得/设置 角色描述 - /// - public string Description { get; set; } - /// /// 通过指定登录名获取授权角色名称数据集合 /// @@ -42,15 +37,8 @@ namespace Bootstrap.Client.DataAccess.MongoDB /// public override IEnumerable RetrievesByUserName(string userName) { - var roles = new List(); - 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); } /// @@ -62,7 +50,7 @@ namespace Bootstrap.Client.DataAccess.MongoDB public override IEnumerable 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; } diff --git a/Bootstrap.Client.DataAccess.MongoDB/User.cs b/Bootstrap.Client.DataAccess.MongoDB/User.cs index e09fa996..fc07373d 100644 --- a/Bootstrap.Client.DataAccess.MongoDB/User.cs +++ b/Bootstrap.Client.DataAccess.MongoDB/User.cs @@ -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(project).FirstOrDefault(); if (ret != null) diff --git a/Bootstrap.Client/Models/HeaderBarModel.cs b/Bootstrap.Client/Models/HeaderBarModel.cs index 85540636..7970a388 100644 --- a/Bootstrap.Client/Models/HeaderBarModel.cs +++ b/Bootstrap.Client/Models/HeaderBarModel.cs @@ -1,5 +1,4 @@ using Bootstrap.Client.DataAccess; -using Bootstrap.Security.DataAccess; using Longbow.Configuration; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Authentication.Cookies; diff --git a/Bootstrap.Client/appsettings.Development.json b/Bootstrap.Client/appsettings.Development.json index 5d79d63b..fd291487 100644 --- a/Bootstrap.Client/appsettings.Development.json +++ b/Bootstrap.Client/appsettings.Development.json @@ -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": "所有角色数据" + } + ] + } +} \ No newline at end of file diff --git a/Bootstrap.Client/appsettings.json b/Bootstrap.Client/appsettings.json index 23639d54..9597da70 100644 --- a/Bootstrap.Client/appsettings.json +++ b/Bootstrap.Client/appsettings.json @@ -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": "指定用户组数据缓存" } ] }