diff --git a/Bootstrap.Admin/Controllers/MenusController.cs b/Bootstrap.Admin/Controllers/MenusController.cs index ea3e39e5..873d34e2 100644 --- a/Bootstrap.Admin/Controllers/MenusController.cs +++ b/Bootstrap.Admin/Controllers/MenusController.cs @@ -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 Get([FromUri]QueryMenuOption value) { - return value.RetrieveData(); + return value.RetrieveData(User.Identity.Name); } /// /// diff --git a/Bootstrap.Admin/Models/HeaderBarModel.cs b/Bootstrap.Admin/Models/HeaderBarModel.cs index 80857fd1..90434b3a 100644 --- a/Bootstrap.Admin/Models/HeaderBarModel.cs +++ b/Bootstrap.Admin/Models/HeaderBarModel.cs @@ -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; } /// /// /// diff --git a/Bootstrap.Admin/Models/NavigatorBarModel.cs b/Bootstrap.Admin/Models/NavigatorBarModel.cs index 9104f349..db685a43 100644 --- a/Bootstrap.Admin/Models/NavigatorBarModel.cs +++ b/Bootstrap.Admin/Models/NavigatorBarModel.cs @@ -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"; } diff --git a/Bootstrap.Admin/Models/QueryMenuOption.cs b/Bootstrap.Admin/Models/QueryMenuOption.cs index 388280d7..685f4d90 100644 --- a/Bootstrap.Admin/Models/QueryMenuOption.cs +++ b/Bootstrap.Admin/Models/QueryMenuOption.cs @@ -19,9 +19,9 @@ namespace Bootstrap.Admin.Models /// public string Category { get; set; } - public QueryData RetrieveData() + public QueryData RetrieveData(string userName) { - var data = MenuHelper.RetrieveMenus(); + var data = MenuHelper.RetrieveMenus(userName); if (!string.IsNullOrEmpty(ParentName)) { data = data.Where(t => t.ParentName.Contains(ParentName)); diff --git a/Bootstrap.Admin/Views/Admin/Menus.cshtml b/Bootstrap.Admin/Views/Admin/Menus.cshtml index 2f7e2a48..83174697 100644 --- a/Bootstrap.Admin/Views/Admin/Menus.cshtml +++ b/Bootstrap.Admin/Views/Admin/Menus.cshtml @@ -20,11 +20,11 @@ } @section query {
-
+
-
+
@@ -36,7 +36,7 @@
-
+
diff --git a/Bootstrap.Admin/Views/Shared/NavigatorConfig.cshtml b/Bootstrap.Admin/Views/Shared/NavigatorConfig.cshtml index a2b1ba4a..294ac868 100644 --- a/Bootstrap.Admin/Views/Shared/NavigatorConfig.cshtml +++ b/Bootstrap.Admin/Views/Shared/NavigatorConfig.cshtml @@ -1,7 +1,6 @@ -@model IEnumerable - - +@model IEnumerable + \ No newline at end of file diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index c80738e0..af23201e 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -24,7 +24,6 @@ - diff --git a/Bootstrap.DataAccess/CacheCleanUtility.cs b/Bootstrap.DataAccess/CacheCleanUtility.cs index bf299317..1940e8aa 100644 --- a/Bootstrap.DataAccess/CacheCleanUtility.cs +++ b/Bootstrap.DataAccess/CacheCleanUtility.cs @@ -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)); diff --git a/Bootstrap.DataAccess/MenuHelper.cs b/Bootstrap.DataAccess/MenuHelper.cs index 04fc9ed1..f9393887 100644 --- a/Bootstrap.DataAccess/MenuHelper.cs +++ b/Bootstrap.DataAccess/MenuHelper.cs @@ -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"; /// /// 查询所有菜单信息 /// - /// + /// /// - public static IEnumerable RetrieveMenus() + public static IEnumerable 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 Menus = new List(); - 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)); } /// - /// 查询某个用户所配置的菜单 + /// /// /// /// - public static IEnumerable RetrieveMenusByUserId(int userId) + public static IEnumerable 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 Menus = new List(); - 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; } /// /// /// /// /// - public static IEnumerable RetrieveNavigationsByUserId(int userId) + public static IEnumerable 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 }); } /// - /// - /// - /// - /// - public static IEnumerable 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; - } - /// /// 删除菜单信息 /// /// @@ -198,26 +164,28 @@ namespace Bootstrap.DataAccess return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusByRoleIDDataKey), k => { List Menus = new List(); - 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" + }); + } } } } diff --git a/Bootstrap.DataAccessTests/MenuHelperTests.cs b/Bootstrap.DataAccessTests/MenuHelperTests.cs index abbb58c1..3a5aa09d 100644 --- a/Bootstrap.DataAccessTests/MenuHelperTests.cs +++ b/Bootstrap.DataAccessTests/MenuHelperTests.cs @@ -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() { diff --git a/DatabaseScripts/Procedures.sql b/DatabaseScripts/Procedures.sql index 37a29d3b..dfb21d45 100644 --- a/DatabaseScripts/Procedures.sql +++ b/DatabaseScripts/Procedures.sql @@ -105,4 +105,45 @@ BEGIN set @sql += 'delete from Navigations where ID in (' + @ids + ');' exec(@sql) END -GO \ No newline at end of file +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