fix: 修复MongoDB下通过用户名称获取菜单方法

#Comment
原有方法未经过权限过滤菜单
This commit is contained in:
Argo Zhang 2019-08-24 00:12:06 +08:00
parent 045ffc8ce2
commit 81b3390363
1 changed files with 18 additions and 1 deletions

View File

@ -1,5 +1,6 @@
using Bootstrap.Security;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
@ -17,14 +18,30 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName)
{
var user = UserHelper.Retrieves().Cast<User>().FirstOrDefault(u => u.UserName.ToLowerInvariant() == userName.ToLowerInvariant());
if (user == null) return Enumerable.Empty<BootstrapMenu>();
var dicts = DictHelper.RetrieveDicts().Where(m => m.Category == "菜单");
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList();
// 通过用户获取 角色列表
var roles = RoleHelper.Retrieves().Cast<Role>().Where(r => user.Roles.Any(rl => rl == r.Id)).ToList();
// 通过用户获取 组列表相关联的角色列表
roles = GroupHelper.RetrievesByUserName(userName).Aggregate(roles, (r, g) =>
{
r.AddRange(RoleHelper.RetrievesByGroupId(g.Id).Cast<Role>());
return r;
}).Distinct().ToList();
var menus = DbManager.Menus.Find(FilterDefinition<BootstrapMenu>.Empty).ToList()
.Where(m => roles.Any(r => r.RoleName.Equals("Administrators", StringComparison.OrdinalIgnoreCase) || r.Menus.Any(rm => rm.Equals(m.Url, StringComparison.OrdinalIgnoreCase))))
.ToList();
menus.ForEach(m =>
{
m.CategoryName = dicts.FirstOrDefault(d => d.Code == m.Category)?.Name;
if (m.ParentId != "0") m.ParentName = menus.FirstOrDefault(p => p.Id == m.ParentId)?.Name;
});
return menus;
}