From ce897986a788e4616e5a4a35422a7b6a66217d52 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 1 Jul 2019 18:48:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(AuthorizateButton):=20MenuHelper?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=8C=89=E9=92=AE=E6=8E=88=E6=9D=83=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Startup.cs | 2 +- Bootstrap.DataAccess/Helper/MenuHelper.cs | 20 -------- .../Bootstrap.DataAccess/AuthButtonTest.cs | 49 ------------------- 3 files changed, 1 insertion(+), 70 deletions(-) delete mode 100644 UnitTest/Bootstrap.DataAccess/AuthButtonTest.cs diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 18957079..77a18409 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -62,7 +62,7 @@ namespace Bootstrap.Admin services.AddResponseCompression(); services.AddBootstrapAdminAuthentication(); services.AddSwagger(); - services.AddButtonAuthorization(MenuHelper.AuthorizateButtons); + services.AddButtonAuthorization(); services.AddMvc(options => { options.Filters.Add(); diff --git a/Bootstrap.DataAccess/Helper/MenuHelper.cs b/Bootstrap.DataAccess/Helper/MenuHelper.cs index 0bcd0ce1..833224ff 100644 --- a/Bootstrap.DataAccess/Helper/MenuHelper.cs +++ b/Bootstrap.DataAccess/Helper/MenuHelper.cs @@ -112,26 +112,6 @@ namespace Bootstrap.DataAccess return DbHelper.CascadeMenus(menus, activeUrl); } - /// - /// 通过当前用户名与指定菜单路径获取此菜单下所有授权按钮集合 - /// - /// 请求上下文 - /// 资源按钮所属菜单 - /// 资源授权码 - /// - public static bool AuthorizateButtons(HttpContext context, string url, string key) - { - if (context.User.IsInRole("Administrators")) return true; - - var menus = RetrieveAllMenus(context.User.Identity.Name); - var activeMenu = menus.FirstOrDefault(m => m.Url.Equals(url, StringComparison.OrdinalIgnoreCase)); - if (activeMenu == null) return false; - - var authorKeys = menus.Where(m => m.ParentId == activeMenu.Id && m.IsResource == 2).Select(m => m.Url); - var keys = key.SpanSplitAny(",. ;", StringSplitOptions.RemoveEmptyEntries); - return keys.Any(m => authorKeys.Any(k => k == m)); - } - /// /// 通过当前用户名获得所有菜单,层次化后集合 /// diff --git a/UnitTest/Bootstrap.DataAccess/AuthButtonTest.cs b/UnitTest/Bootstrap.DataAccess/AuthButtonTest.cs deleted file mode 100644 index 3a78ef00..00000000 --- a/UnitTest/Bootstrap.DataAccess/AuthButtonTest.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Http.Authentication; -using Microsoft.AspNetCore.Http.Features; -using System; -using System.Collections.Generic; -using System.Security.Claims; -using System.Threading; -using Xunit; - -namespace Bootstrap.DataAccess -{ - - [Collection("SQLServerContext")] - public class AuthButtonTest - { - [Fact] - public void User_Ok() - { - Assert.False(MenuHelper.AuthorizateButtons(new FooHttpContext(), "~/Admin/Profiles1", "saveDisplayName")); - Assert.False(MenuHelper.AuthorizateButtons(new FooHttpContext(), "~/Admin/Index", "saveDisplayName")); - } - - private class FooHttpContext : HttpContext - { - public override IFeatureCollection Features => throw new NotImplementedException(); - - public override HttpRequest Request => throw new NotImplementedException(); - - public override HttpResponse Response => throw new NotImplementedException(); - - public override ConnectionInfo Connection => throw new NotImplementedException(); - - public override WebSocketManager WebSockets => throw new NotImplementedException(); - - [Obsolete] - public override AuthenticationManager Authentication => throw new NotImplementedException(); - - public override ClaimsPrincipal User { get; set; } = new ClaimsPrincipal(new System.Security.Principal.GenericIdentity("User")); - - public override IDictionary Items { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override IServiceProvider RequestServices { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override CancellationToken RequestAborted { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override string TraceIdentifier { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - public override ISession Session { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - public override void Abort() => throw new NotImplementedException(); - } - } -}