增加功能:增加角色管理菜单授权

This commit is contained in:
Argo-MacBookPro 2018-10-31 16:50:55 +08:00
parent 82abbc30c1
commit 2ae1a241de
4 changed files with 35 additions and 13 deletions

View File

@ -4,7 +4,6 @@ using Bootstrap.Security;
using Longbow.Web.Mvc; using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api namespace Bootstrap.Admin.Controllers.Api
{ {
@ -24,6 +23,7 @@ namespace Bootstrap.Admin.Controllers.Api
{ {
return value.RetrieveData(User.Identity.Name); return value.RetrieveData(User.Identity.Name);
} }
/// <summary> /// <summary>
/// 保存菜单调用 /// 保存菜单调用
/// </summary> /// </summary>
@ -33,6 +33,7 @@ namespace Bootstrap.Admin.Controllers.Api
{ {
return MenuHelper.SaveMenu(value); return MenuHelper.SaveMenu(value);
} }
/// <summary> /// <summary>
/// 删除菜单调用 /// 删除菜单调用
/// </summary> /// </summary>
@ -42,31 +43,33 @@ namespace Bootstrap.Admin.Controllers.Api
{ {
return MenuHelper.DeleteMenu(value); return MenuHelper.DeleteMenu(value);
} }
/// <summary> /// <summary>
/// /// 角色管理菜单授权按钮调用
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id">角色ID</param>
/// <param name="type"></param> /// <param name="type">type=role时角色管理菜单授权调用type=user时菜单管理编辑页面父类菜单按钮调用</param>
/// <returns></returns> /// <returns></returns>
[HttpPost("{id}")] [HttpPost("{id}")]
public IEnumerable<BootstrapMenu> Post(string id, [FromQuery]string type) public IEnumerable<object> Post(string id, [FromQuery]string type)
{ {
var ret = new List<BootstrapMenu>(); IEnumerable<object> ret = new List<object>();
switch (type) switch (type)
{ {
case "role": case "role":
ret = MenuHelper.RetrieveMenusByRoleId(id).ToList(); ret = MenuHelper.RetrieveMenusByRoleId(id);
break; break;
case "user": case "user":
ret = MenuHelper.RetrieveMenus(User.Identity.Name).ToList(); ret = MenuHelper.RetrieveMenus(User.Identity.Name);
break; break;
default: default:
break; break;
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 角色管理页面分配菜单时调用 /// 角色管理菜单授权保存按钮调用
/// </summary> /// </summary>
/// <param name="id">角色ID</param> /// <param name="id">角色ID</param>
/// <param name="value">菜单ID集合</param> /// <param name="value">菜单ID集合</param>

View File

@ -72,5 +72,24 @@ namespace Bootstrap.DataAccess.MongoDB
MongoDbAccessManager.Menus.BulkWrite(list); MongoDbAccessManager.Menus.BulkWrite(list);
return true; return true;
} }
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public override IEnumerable<object> RetrieveMenusByRoleId(string roleId) => MongoDbAccessManager.Roles.Find(md => md.Id == roleId).FirstOrDefault().Menus.Select(m => new { Id = m });
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="menuIds"></param>
/// <returns></returns>
public override bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
{
MongoDbAccessManager.Roles.FindOneAndUpdate(md => md.Id == roleId, Builders<Role>.Update.Set(md => md.Menus, menuIds));
return true;
}
} }
} }

View File

@ -58,7 +58,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); public static IEnumerable<object> RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create<Menu>().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey);
/// <summary> /// <summary>
/// ///
@ -111,7 +111,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenus(string userName) public static IEnumerable<object> RetrieveMenus(string userName)
{ {
var menus = RetrieveAllMenus(userName); var menus = RetrieveAllMenus(userName);
var root = menus.Where(m => m.ParentId == "0").OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); var root = menus.Where(m => m.ParentId == "0").OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order);

View File

@ -66,7 +66,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="roleId"></param> /// <param name="roleId"></param>
/// <returns></returns> /// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(string roleId) public virtual IEnumerable<object> RetrieveMenusByRoleId(string roleId)
{ {
var menus = new List<BootstrapMenu>(); var menus = new List<BootstrapMenu>();
string sql = "select NavigationID from NavigationRole where RoleID = @RoleID"; string sql = "select NavigationID from NavigationRole where RoleID = @RoleID";
@ -84,7 +84,7 @@ namespace Bootstrap.DataAccess
} }
} }
} }
return menus; return menus.Select(m => m.Id);
} }
/// <summary> /// <summary>
/// 通过角色ID保存当前授权菜单 /// 通过角色ID保存当前授权菜单