重构获取菜单架构,取消使用UserID,全部使用userName进行授权
This commit is contained in:
parent
4f131cd36d
commit
c822e452e5
|
@ -1,5 +1,6 @@
|
|||
using Bootstrap.Admin.Models;
|
||||
using Bootstrap.DataAccess;
|
||||
using Longbow.Security.Principal;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -17,7 +18,7 @@ namespace Bootstrap.Admin.Controllers
|
|||
[HttpGet]
|
||||
public QueryData<Menu> Get([FromUri]QueryMenuOption value)
|
||||
{
|
||||
return value.RetrieveData();
|
||||
return value.RetrieveData(User.Identity.Name);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
|
|
|
@ -13,10 +13,12 @@ namespace Bootstrap.Admin.Models
|
|||
{
|
||||
var user = UserHelper.RetrieveUsersByName(HttpContext.Current.User.Identity.Name);
|
||||
DisplayName = user.DisplayName;
|
||||
UserName = user.UserName;
|
||||
UserID = user.ID;
|
||||
HomeUrl = "~/";
|
||||
Menus = MenuHelper.RetrieveLinksByUserId(user.ID);
|
||||
Menus = MenuHelper.RetrieveLinksByUserName(UserName);
|
||||
}
|
||||
public string UserName { get; protected set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace Bootstrap.Admin.Models
|
|||
{
|
||||
public NavigatorBarModel(string url)
|
||||
{
|
||||
Navigations = MenuHelper.RetrieveNavigationsByUserId(UserID);
|
||||
Navigations = MenuHelper.RetrieveNavigationsByUserName(UserName);
|
||||
Navigations.ToList().ForEach(m => m.Active = m.Url.Equals(url, StringComparison.OrdinalIgnoreCase) ? "active" : "");
|
||||
HomeUrl = "~/Admin/Index";
|
||||
}
|
||||
|
|
|
@ -19,9 +19,9 @@ namespace Bootstrap.Admin.Models
|
|||
/// </summary>
|
||||
public string Category { get; set; }
|
||||
|
||||
public QueryData<Menu> RetrieveData()
|
||||
public QueryData<Menu> RetrieveData(string userName)
|
||||
{
|
||||
var data = MenuHelper.RetrieveMenus();
|
||||
var data = MenuHelper.RetrieveMenus(userName);
|
||||
if (!string.IsNullOrEmpty(ParentName))
|
||||
{
|
||||
data = data.Where(t => t.ParentName.Contains(ParentName));
|
||||
|
|
|
@ -20,11 +20,11 @@
|
|||
}
|
||||
@section query {
|
||||
<form class="form-inline" role="form">
|
||||
<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-4">
|
||||
<div class="form-group col-xs-12 col-sm-6 col-md-3 col-lg-3">
|
||||
<label class="control-label" for="txt_parent_menus_name">父级菜单</label>
|
||||
<input type="text" class="form-control" id="txt_parent_menus_name" />
|
||||
</div>
|
||||
<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-4">
|
||||
<div class="form-group col-xs-12 col-sm-6 col-md-3 col-lg-3">
|
||||
<label class="control-label" for="txt_menus_name">菜单名称</label>
|
||||
<input type="text" class="form-control" id="txt_menus_name" />
|
||||
</div>
|
||||
|
@ -36,7 +36,7 @@
|
|||
<option value="1">外部菜单</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-xs-12 col-sm-6 col-md-1 col-lg-1">
|
||||
<div class="form-group col-xs-12 col-sm-6 col-md-2 col-lg-2">
|
||||
<button type="button" id="btn_query" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span>查询</button>
|
||||
</div>
|
||||
</form>
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
@model IEnumerable<Menu>
|
||||
|
||||
<div class="modal fade" id="dialogMenu" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="myMenuModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
@Html.Partial("MenuTree", Model)
|
||||
</div>
|
||||
</div>
|
||||
@model IEnumerable<Menu>
|
||||
<div class="modal fade" id="dialogMenu" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="myMenuModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
@Html.Partial("MenuTree", Model)
|
||||
</div>
|
||||
</div>
|
|
@ -24,7 +24,6 @@
|
|||
|
||||
<cacheManager>
|
||||
<add key="MenuHelper-RetrieveMenus" interval="600" desc="所有菜单数据缓存" />
|
||||
<add key="MenuHelper-RetrieveMenusByUserId" interval="600" desc="指定用户菜单数据缓存"/>
|
||||
<add key="MenuHelper-RetrieveMenusByRoleId" interval="600" desc="角色菜单信息缓存" />
|
||||
<add key="UserHelper-RetrieveUsers" interval="600" desc="所有用户数据缓存"/>
|
||||
<add key="UserHelper-RetrieveUsersByName" interval="600" desc="指定用户名的用户数据缓存"/>
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace Bootstrap.DataAccess
|
|||
{
|
||||
cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIDDataKey, id));
|
||||
cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIDDataKey, id));
|
||||
cacheKeys.Add(string.Format("{0}-{1}", MenuHelper.RetrieveMenusByUserIDDataKey, id));
|
||||
cacheKeys.Add(MenuHelper.RetrieveMenusDataKey);
|
||||
});
|
||||
// final cleanup
|
||||
CacheManager.Clear(key => cacheKeys.Any(k => k == key) || key.Contains(UserHelper.RetrieveUsersDataKey) || key.Contains(RoleHelper.RetrieveRolesByUserNameDataKey));
|
||||
|
|
|
@ -3,12 +3,12 @@ using Longbow.Caching;
|
|||
using Longbow.Caching.Configuration;
|
||||
using Longbow.Data;
|
||||
using Longbow.ExceptionManagement;
|
||||
using Longbow.Security.Principal;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess
|
||||
|
@ -16,38 +16,41 @@ namespace Bootstrap.DataAccess
|
|||
public static class MenuHelper
|
||||
{
|
||||
internal const string RetrieveMenusDataKey = "MenuHelper-RetrieveMenus";
|
||||
internal const string RetrieveMenusByUserIDDataKey = "MenuHelper-RetrieveMenusByUserId";
|
||||
internal const string RetrieveMenusByRoleIDDataKey = "MenuHelper-RetrieveMenusByRoleId";
|
||||
/// <summary>
|
||||
/// 查询所有菜单信息
|
||||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <param name="userName"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Menu> RetrieveMenus()
|
||||
public static IEnumerable<Menu> RetrieveMenus(string userName = null)
|
||||
{
|
||||
return CacheManager.GetOrAdd(RetrieveMenusDataKey, CacheSection.RetrieveIntervalByKey(RetrieveMenusDataKey), key =>
|
||||
userName = LgbPrincipal.IsAdmin(userName) ? string.Empty : userName;
|
||||
string key = string.Format("{0}-{1}", RetrieveMenusDataKey, userName);
|
||||
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusDataKey), k =>
|
||||
{
|
||||
string sql = "select n.*, d.Name as CategoryName, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = N'菜单' and d.Define = 0 left join Navigations ln on n.ParentId = ln.ID";
|
||||
List<Menu> Menus = new List<Menu>();
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
try
|
||||
{
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_RetrieveMenus"))
|
||||
{
|
||||
while (reader.Read())
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", DBAccess.ToDBValue(userName), ParameterDirection.Input));
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
Menus.Add(new Menu()
|
||||
while (reader.Read())
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
ParentId = (int)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Order = (int)reader[3],
|
||||
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
|
||||
Url = LgbConvert.ReadValue(reader[5], string.Empty),
|
||||
Category = (string)reader[6],
|
||||
CategoryName = (string)reader[7],
|
||||
ParentName = LgbConvert.ReadValue(reader[8], string.Empty)
|
||||
});
|
||||
Menus.Add(new Menu()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
ParentId = (int)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Order = (int)reader[3],
|
||||
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
|
||||
Url = LgbConvert.ReadValue(reader[5], string.Empty),
|
||||
Category = (string)reader[6],
|
||||
CategoryName = (string)reader[7],
|
||||
ParentName = LgbConvert.ReadValue(reader[8], string.Empty)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,50 +59,25 @@ namespace Bootstrap.DataAccess
|
|||
}, CacheSection.RetrieveDescByKey(RetrieveMenusDataKey));
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询某个用户所配置的菜单
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Menu> RetrieveMenusByUserId(int userId)
|
||||
public static IEnumerable<Menu> RetrieveNavigationsByUserName(string userName)
|
||||
{
|
||||
string key = string.Format("{0}-{1}", RetrieveMenusByUserIDDataKey, userId);
|
||||
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusByUserIDDataKey), k =>
|
||||
{
|
||||
string sql = "select n.* from Navigations n inner join NavigationRole nr on n.ID = nr.NavigationID inner join UserRole ur on nr.RoleID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.ID = @UserID union select n.* from Navigations n inner join NavigationRole nr on n.ID = nr.NavigationID inner join RoleGroup rg on nr.RoleID = rg.RoleID inner join UserGroup ur on rg.GroupID = ur.GroupID inner join Users u on ur.UserID = u.ID where u.ID = @UserID";
|
||||
List<Menu> Menus = new List<Menu>();
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
try
|
||||
{
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId, ParameterDirection.Input));
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
Menus.Add(new Menu()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
ParentId = (int)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Order = (int)reader[3],
|
||||
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
|
||||
Url = LgbConvert.ReadValue(reader[5], string.Empty),
|
||||
Category = (string)reader[6]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
||||
return Menus;
|
||||
}, CacheSection.RetrieveDescByKey(RetrieveMenusByUserIDDataKey));
|
||||
var navs = RetrieveMenus(userName).Where(m => m.Category == "0");
|
||||
var root = navs.Where(m => m.ParentId == 0).OrderBy(m => m.Order);
|
||||
CascadeMenu(navs, root);
|
||||
return root;
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Menu> RetrieveNavigationsByUserId(int userId)
|
||||
public static IEnumerable<Menu> RetrieveLinksByUserName(string userName)
|
||||
{
|
||||
var navs = (userId == 0 ? RetrieveMenus() : RetrieveMenusByUserId(userId)).Where(m => m.Category == "0");
|
||||
var navs = RetrieveMenus(userName).Where(m => m.Category == "1");
|
||||
var root = navs.Where(m => m.ParentId == 0).OrderBy(m => m.Order);
|
||||
CascadeMenu(navs, root);
|
||||
return root;
|
||||
|
@ -113,18 +91,6 @@ namespace Bootstrap.DataAccess
|
|||
});
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Menu> RetrieveLinksByUserId(int userId)
|
||||
{
|
||||
var navs = (userId == 0 ? RetrieveMenus() : RetrieveMenusByUserId(userId)).Where(m => m.Category == "1");
|
||||
var root = navs.Where(m => m.ParentId == 0).OrderBy(m => m.Order);
|
||||
CascadeMenu(navs, root);
|
||||
return root;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除菜单信息
|
||||
/// </summary>
|
||||
/// <param name="ids"></param>
|
||||
|
@ -198,26 +164,28 @@ namespace Bootstrap.DataAccess
|
|||
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusByRoleIDDataKey), k =>
|
||||
{
|
||||
List<Menu> Menus = new List<Menu>();
|
||||
string sql = "select n.ID,n.ParentId, n.Name,n.[Order],n.Icon,n.Url,n.Category, case nr.NavigationID when n.ID then 'active' else '' end [status] from Navigations n left join NavigationRole nr on n.ID = nr.NavigationID and RoleID = @RoleID";
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId, ParameterDirection.Input));
|
||||
try
|
||||
{
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
string sql = "select n.ID,n.ParentId, n.Name,n.[Order],n.Icon,n.Url,n.Category, case nr.NavigationID when n.ID then 'active' else '' end [status] from Navigations n left join NavigationRole nr on n.ID = nr.NavigationID and RoleID = @RoleID";
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
||||
{
|
||||
while (reader.Read())
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId, ParameterDirection.Input));
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
Menus.Add(new Menu()
|
||||
while (reader.Read())
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
ParentId = (int)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Order = (int)reader[3],
|
||||
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
|
||||
Url = LgbConvert.ReadValue(reader[5], string.Empty),
|
||||
Category = (string)reader[6],
|
||||
Active = (string)reader[7] == "" ? "" : "checked"
|
||||
});
|
||||
Menus.Add(new Menu()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
ParentId = (int)reader[1],
|
||||
Name = (string)reader[2],
|
||||
Order = (int)reader[3],
|
||||
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
|
||||
Url = LgbConvert.ReadValue(reader[5], string.Empty),
|
||||
Category = (string)reader[6],
|
||||
Active = (string)reader[7] == "" ? "" : "checked"
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,11 +32,6 @@ namespace Bootstrap.DataAccess.Tests
|
|||
Assert.IsTrue(MenuHelper.RetrieveMenus().Count() > 1, "不带参数的MenuHelper.RetrieveMenus方法调用失败");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RetrieveMenuByUserIDTest()
|
||||
{
|
||||
Assert.IsTrue(MenuHelper.RetrieveMenusByUserId(1).Count() > 1, "根据用户ID查询菜单的MenuHelper.RetrieveMenusByUserId方法调用失败");
|
||||
}
|
||||
[TestMethod]
|
||||
public void RetrieveMenuByRoleIDTest()
|
||||
{
|
||||
|
|
|
@ -105,4 +105,45 @@ BEGIN
|
|||
set @sql += 'delete from Navigations where ID in (' + @ids + ');'
|
||||
exec(@sql)
|
||||
END
|
||||
GO
|
||||
GO
|
||||
|
||||
Drop PROCEDURE Proc_RetrieveMenus
|
||||
GO
|
||||
-- =============================================
|
||||
-- Author: Argo Zhang
|
||||
-- Create date: 2016-11-08
|
||||
-- Description:
|
||||
-- =============================================
|
||||
Create PROCEDURE Proc_RetrieveMenus
|
||||
-- Add the parameters for the stored procedure here
|
||||
@userName varchar(50) = null
|
||||
WITH ENCRYPTION
|
||||
AS
|
||||
BEGIN
|
||||
-- SET NOCOUNT ON added to prevent extra result sets from
|
||||
-- interfering with SELECT statements.
|
||||
SET NOCOUNT ON;
|
||||
SET XACT_ABORT ON;
|
||||
-- Insert statements for procedure here
|
||||
if @userName = '' or @userName is null
|
||||
select n.*, d.Name as CategoryName, ln.Name as ParentName
|
||||
from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = N'²Ëµ¥' and d.Define = 0
|
||||
left join Navigations ln on n.ParentId = ln.ID
|
||||
else
|
||||
select n.*, d.Name as CategoryName, ln.Name as ParentName
|
||||
from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = N'²Ëµ¥' and d.Define = 0
|
||||
left join Navigations ln on n.ParentId = ln.ID
|
||||
inner join (
|
||||
select nr.NavigationID from Users u
|
||||
inner join UserRole ur on ur.UserID = u.ID
|
||||
inner join NavigationRole nr on nr.RoleID = ur.RoleID
|
||||
where u.UserName = @userName
|
||||
union
|
||||
select nr.NavigationID from Users u
|
||||
inner join UserGroup ug on u.ID = ug.UserID
|
||||
inner join RoleGroup rg on rg.GroupID = ug.GroupID
|
||||
inner join NavigationRole nr on nr.RoleID = rg.RoleID
|
||||
where u.UserName = @userName
|
||||
) nav on n.ID = nav.NavigationID
|
||||
END
|
||||
GO
|
||||
|
|
Loading…
Reference in New Issue