From 2ae1a241de38fe31e6f341608613eac565fe97e8 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Wed, 31 Oct 2018 16:50:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=92=E8=89=B2=E7=AE=A1=E7=90=86=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/MenusController.cs | 21 +++++++++++-------- Bootstrap.DataAccess.MongoDB/Menu.cs | 19 +++++++++++++++++ Bootstrap.DataAccess/Helper/MenuHelper.cs | 4 ++-- Bootstrap.DataAccess/Menu.cs | 4 ++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/MenusController.cs b/Bootstrap.Admin/Controllers/Api/MenusController.cs index 7d73b890..46391284 100644 --- a/Bootstrap.Admin/Controllers/Api/MenusController.cs +++ b/Bootstrap.Admin/Controllers/Api/MenusController.cs @@ -4,7 +4,6 @@ using Bootstrap.Security; using Longbow.Web.Mvc; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; -using System.Linq; namespace Bootstrap.Admin.Controllers.Api { @@ -24,6 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api { return value.RetrieveData(User.Identity.Name); } + /// /// 保存菜单调用 /// @@ -33,6 +33,7 @@ namespace Bootstrap.Admin.Controllers.Api { return MenuHelper.SaveMenu(value); } + /// /// 删除菜单调用 /// @@ -42,31 +43,33 @@ namespace Bootstrap.Admin.Controllers.Api { return MenuHelper.DeleteMenu(value); } + /// - /// + /// 角色管理菜单授权按钮调用 /// - /// - /// + /// 角色ID + /// type=role时,角色管理菜单授权调用;type=user时,菜单管理编辑页面父类菜单按钮调用 /// [HttpPost("{id}")] - public IEnumerable Post(string id, [FromQuery]string type) + public IEnumerable Post(string id, [FromQuery]string type) { - var ret = new List(); + IEnumerable ret = new List(); switch (type) { case "role": - ret = MenuHelper.RetrieveMenusByRoleId(id).ToList(); + ret = MenuHelper.RetrieveMenusByRoleId(id); break; case "user": - ret = MenuHelper.RetrieveMenus(User.Identity.Name).ToList(); + ret = MenuHelper.RetrieveMenus(User.Identity.Name); break; default: break; } return ret; } + /// - /// 角色管理页面分配菜单时调用 + /// 角色管理菜单授权保存按钮调用 /// /// 角色ID /// 菜单ID集合 diff --git a/Bootstrap.DataAccess.MongoDB/Menu.cs b/Bootstrap.DataAccess.MongoDB/Menu.cs index d6a53a69..86d4ede7 100644 --- a/Bootstrap.DataAccess.MongoDB/Menu.cs +++ b/Bootstrap.DataAccess.MongoDB/Menu.cs @@ -72,5 +72,24 @@ namespace Bootstrap.DataAccess.MongoDB MongoDbAccessManager.Menus.BulkWrite(list); return true; } + + /// + /// + /// + /// + /// + public override IEnumerable RetrieveMenusByRoleId(string roleId) => MongoDbAccessManager.Roles.Find(md => md.Id == roleId).FirstOrDefault().Menus.Select(m => new { Id = m }); + + /// + /// + /// + /// + /// + /// + public override bool SaveMenusByRoleId(string roleId, IEnumerable menuIds) + { + MongoDbAccessManager.Roles.FindOneAndUpdate(md => md.Id == roleId, Builders.Update.Set(md => md.Menus, menuIds)); + return true; + } } } diff --git a/Bootstrap.DataAccess/Helper/MenuHelper.cs b/Bootstrap.DataAccess/Helper/MenuHelper.cs index 6e290ae8..fabc96d8 100644 --- a/Bootstrap.DataAccess/Helper/MenuHelper.cs +++ b/Bootstrap.DataAccess/Helper/MenuHelper.cs @@ -58,7 +58,7 @@ namespace Bootstrap.DataAccess /// /// /// - public static IEnumerable RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); + public static IEnumerable RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); /// /// @@ -111,7 +111,7 @@ namespace Bootstrap.DataAccess /// /// /// - public static IEnumerable RetrieveMenus(string userName) + public static IEnumerable RetrieveMenus(string userName) { var menus = RetrieveAllMenus(userName); var root = menus.Where(m => m.ParentId == "0").OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); diff --git a/Bootstrap.DataAccess/Menu.cs b/Bootstrap.DataAccess/Menu.cs index c52ee502..38c32233 100644 --- a/Bootstrap.DataAccess/Menu.cs +++ b/Bootstrap.DataAccess/Menu.cs @@ -66,7 +66,7 @@ namespace Bootstrap.DataAccess /// /// /// - public virtual IEnumerable RetrieveMenusByRoleId(string roleId) + public virtual IEnumerable RetrieveMenusByRoleId(string roleId) { var menus = new List(); string sql = "select NavigationID from NavigationRole where RoleID = @RoleID"; @@ -84,7 +84,7 @@ namespace Bootstrap.DataAccess } } } - return menus; + return menus.Select(m => m.Id); } /// /// 通过角色ID保存当前授权菜单