feat: 重构 Block 授权逻辑

This commit is contained in:
Argo-Tianyi 2022-01-20 13:17:46 +08:00
parent 3e5de3e936
commit 08b02e9579
7 changed files with 35 additions and 43 deletions

View File

@ -67,21 +67,4 @@ class NavigationService : INavigation
}
return ret;
}
public bool AuthorizationBlock(string userName, string url, string authKey)
{
var menus = GetAllMenus(userName);
var activeMeun = menus.FirstOrDefault(s => s.Url.Equals($"~/{url}", StringComparison.OrdinalIgnoreCase));
if (activeMeun == null)
{
return false;
}
IEnumerable<string> authorKeys = from m in menus
where m.ParentId == activeMeun.Id && m.IsResource == EnumResource.Block
select m.Url;
return authorKeys.Any(s => s.Equals(authKey, StringComparison.OrdinalIgnoreCase));
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Blazor" Version="6.0.1-beta01" />
<PackageReference Include="Bootstrap.Security.Blazor" Version="6.0.1" />
</ItemGroup>
<ItemGroup>

View File

@ -27,13 +27,4 @@ public interface INavigation
/// <param name="menuIds"></param>
/// <returns></returns>
bool SaveMenusByRoleId(string? roleId, List<string> menuIds);
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="url"></param>
/// <param name="authKey"></param>
/// <returns></returns>
bool AuthorizationBlock(string userName, string url, string authKey);
}

View File

@ -1,4 +1,6 @@
using Bootstrap.Security.Blazor;
using BootstrapAdmin.DataAccess.Models;
using Microsoft.AspNetCore.Components.Authorization;
namespace BootstrapAdmin.Web.Core.Services;
@ -44,12 +46,29 @@ public class AdminService : IBootstrapAdminService
/// <returns></returns>
public Task<bool> AuhorizingNavigation(string userName, string url)
{
var ret = false;
if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uri))
{
ret = Navigations.GetAllMenus(userName)
.Any(m => m.Url.Contains(uri.AbsolutePath, StringComparison.OrdinalIgnoreCase));
}
var ret = Navigations.GetAllMenus(userName).Any(m => m.Url.Contains(url, StringComparison.OrdinalIgnoreCase));
return Task.FromResult(ret);
}
/// <summary>
/// 通过用户名检查当前请求 Url 是否已授权方法
/// </summary>
/// <param name="userName"></param>
/// <param name="url"></param>
/// <param name="blockName"></param>
/// <returns></returns>
public bool AuhorizingBlock(string userName, string url, string blockName)
{
var ret = User.GetRoles(userName).Any(i => i.Equals("Administrators", StringComparison.OrdinalIgnoreCase));
if (!ret)
{
var menus = Navigations.GetAllMenus(userName);
var menu = menus.FirstOrDefault(m => m.Url.Contains(url, StringComparison.OrdinalIgnoreCase));
if (menu != null)
{
ret = menus.FirstOrDefault(m => m.ParentId == menu.Id && m.IsResource == EnumResource.Block && m.Url.Equals(blockName, StringComparison.OrdinalIgnoreCase)) != null;
}
}
return ret;
}
}

View File

@ -1,5 +1,6 @@
using BootstrapAdmin.Web.Core;
using Bootstrap.Security.Blazor;
using BootstrapAdmin.Web.Services;
using Microsoft.AspNetCore.Components.Authorization;
namespace BootstrapAdmin.Web.Components
{
@ -30,7 +31,7 @@ namespace BootstrapAdmin.Web.Components
[Inject]
[NotNull]
private INavigation? NavigationService { get; set; }
private IBootstrapAdminService? AdminService { get; set; }
[Inject]
[NotNull]
@ -43,8 +44,7 @@ namespace BootstrapAdmin.Web.Components
private Task<bool> OnQueryCondition(string name)
{
var url = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
return Task.FromResult(NavigationService.AuthorizationBlock(AppContext.UserName, url, name));
return Task.FromResult(AdminService.AuhorizingBlock(AppContext.UserName, url, name));
}
}
}

View File

@ -1,5 +1,5 @@
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Models;
using Bootstrap.Security.Blazor;
using BootstrapAdmin.Web.Core.Services;
using BootstrapAdmin.Web.Services;
namespace BootstrapAdmin.Web.Components
@ -194,7 +194,7 @@ namespace BootstrapAdmin.Web.Components
[Inject]
[NotNull]
private INavigation? NavigationService { get; set; }
private IBootstrapAdminService? AdminService { get; set; }
[Inject]
[NotNull]
@ -207,8 +207,7 @@ namespace BootstrapAdmin.Web.Components
private bool AuthorizeButton(string operate)
{
var url = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
return NavigationService.AuthorizationBlock(url, AppContext.UserName, operate);
return AdminService.AuhorizingBlock(AppContext.UserName, url, operate);
}
}
}

View File

@ -144,7 +144,7 @@ namespace BootstrapAdmin.Web.Shared
LockInterval = Convert.ToInt32(DictsService.GetAutoLockScreenInterval());
}
private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, url);
private Task<bool> OnAuthorizing(string url) => SecurityService.AuhorizingNavigation(Context.UserName, NavigationManager.ToBaseRelativePath(url));
private async Task OnErrorHandleAsync(ILogger logger, Exception ex)
{