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

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 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);
}
/// <summary>
/// 保存菜单调用
/// </summary>
@ -33,6 +33,7 @@ namespace Bootstrap.Admin.Controllers.Api
{
return MenuHelper.SaveMenu(value);
}
/// <summary>
/// 删除菜单调用
/// </summary>
@ -42,31 +43,33 @@ namespace Bootstrap.Admin.Controllers.Api
{
return MenuHelper.DeleteMenu(value);
}
/// <summary>
///
/// 角色管理菜单授权按钮调用
/// </summary>
/// <param name="id"></param>
/// <param name="type"></param>
/// <param name="id">角色ID</param>
/// <param name="type">type=role时角色管理菜单授权调用type=user时菜单管理编辑页面父类菜单按钮调用</param>
/// <returns></returns>
[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)
{
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;
}
/// <summary>
/// 角色管理页面分配菜单时调用
/// 角色管理菜单授权保存按钮调用
/// </summary>
/// <param name="id">角色ID</param>
/// <param name="value">菜单ID集合</param>

View File

@ -72,5 +72,24 @@ namespace Bootstrap.DataAccess.MongoDB
MongoDbAccessManager.Menus.BulkWrite(list);
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>
/// <param name="roleId"></param>
/// <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>
///
@ -111,7 +111,7 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveMenus(string userName)
public static IEnumerable<object> RetrieveMenus(string userName)
{
var menus = RetrieveAllMenus(userName);
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>
/// <param name="roleId"></param>
/// <returns></returns>
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(string roleId)
public virtual IEnumerable<object> RetrieveMenusByRoleId(string roleId)
{
var menus = new List<BootstrapMenu>();
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>
/// 通过角色ID保存当前授权菜单