refactor(AuthorizateButton): MenuHelper移除按钮授权方法

This commit is contained in:
Argo Zhang 2019-07-01 18:48:16 +08:00
parent 8ce6778bb3
commit ce897986a7
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
3 changed files with 1 additions and 70 deletions

View File

@ -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<BootstrapAdminAuthorizeFilter>();

View File

@ -112,26 +112,6 @@ namespace Bootstrap.DataAccess
return DbHelper.CascadeMenus(menus, activeUrl);
}
/// <summary>
/// 通过当前用户名与指定菜单路径获取此菜单下所有授权按钮集合
/// </summary>
/// <param name="context">请求上下文</param>
/// <param name="url">资源按钮所属菜单</param>
/// <param name="key">资源授权码</param>
/// <returns></returns>
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));
}
/// <summary>
/// 通过当前用户名获得所有菜单,层次化后集合
/// </summary>

View File

@ -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<object, object> 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();
}
}
}