committed by
GitHub
20 changed files with 265 additions and 277 deletions
@ -1,32 +1,46 @@ |
|||
using Hangfire.Annotations; |
|||
using Hangfire.Dashboard; |
|||
using Hangfire.Dashboard; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using System.Linq; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.Threading; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace LINGYUN.Abp.Hangfire.Dashboard.Authorization |
|||
{ |
|||
public class DashboardAuthorizationFilter : IDashboardAuthorizationFilter |
|||
public class DashboardAuthorizationFilter : IDashboardAsyncAuthorizationFilter |
|||
{ |
|||
internal readonly static string[] AllowRoutePrefixs = new string[] |
|||
private readonly string[] _requiredPermissionNames; |
|||
|
|||
public DashboardAuthorizationFilter(params string[] requiredPermissionNames) |
|||
{ |
|||
_requiredPermissionNames = requiredPermissionNames; |
|||
} |
|||
|
|||
public async Task<bool> AuthorizeAsync(DashboardContext context) |
|||
{ |
|||
"/stats", |
|||
"/js", |
|||
"/css", |
|||
"/fonts" |
|||
}; |
|||
public bool Authorize([NotNull] DashboardContext context) |
|||
if (!IsLoggedIn(context)) |
|||
{ |
|||
if (AllowRoutePrefixs.Any(url => context.Request.Path.StartsWith(url))) |
|||
return false; |
|||
} |
|||
|
|||
if (_requiredPermissionNames.IsNullOrEmpty()) |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
var httpContext = context.GetHttpContext(); |
|||
var permissionChecker = httpContext.RequestServices.GetRequiredService<IPermissionChecker>(); |
|||
return AsyncHelper.RunSync(async () => |
|||
await permissionChecker.IsGrantedAsync(httpContext.User, HangfireDashboardPermissions.Dashboard.Default)); |
|||
return await IsPermissionGrantedAsync(context, _requiredPermissionNames); |
|||
} |
|||
|
|||
private static bool IsLoggedIn(DashboardContext context) |
|||
{ |
|||
var currentUser = context.GetHttpContext().RequestServices.GetRequiredService<ICurrentUser>(); |
|||
return currentUser.IsAuthenticated; |
|||
} |
|||
|
|||
private static async Task<bool> IsPermissionGrantedAsync(DashboardContext context, string[] requiredPermissionNames) |
|||
{ |
|||
var permissionChecker = context.GetHttpContext().RequestServices.GetRequiredService<IDashboardPermissionChecker>(); |
|||
return await permissionChecker.IsGrantedAsync(context, requiredPermissionNames); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,48 @@ |
|||
using Hangfire.Dashboard; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace LINGYUN.Abp.Hangfire.Dashboard.Authorization |
|||
{ |
|||
public class DashboardPermissionChecker : IDashboardPermissionChecker, ITransientDependency |
|||
{ |
|||
// 仪表板属于高频访问, 设定有效期的二级权限缓存
|
|||
private readonly IMemoryCache _memoryCache; |
|||
private readonly IPermissionChecker _permissionChecker; |
|||
|
|||
public DashboardPermissionChecker( |
|||
IMemoryCache memoryCache, |
|||
IPermissionChecker permissionChecker) |
|||
{ |
|||
_memoryCache = memoryCache; |
|||
_permissionChecker = permissionChecker; |
|||
} |
|||
|
|||
public virtual async Task<bool> IsGrantedAsync(DashboardContext context, string[] requiredPermissionNames) |
|||
{ |
|||
var localPermissionKey = $"_HDPS:{requiredPermissionNames.JoinAsString(";")}"; |
|||
|
|||
if (_memoryCache.TryGetValue(localPermissionKey, out MultiplePermissionGrantResult cacheItem)) |
|||
{ |
|||
return cacheItem.AllGranted; |
|||
} |
|||
|
|||
cacheItem = await _permissionChecker.IsGrantedAsync(requiredPermissionNames); |
|||
|
|||
_memoryCache.Set( |
|||
localPermissionKey, |
|||
cacheItem, |
|||
new MemoryCacheEntryOptions |
|||
{ |
|||
// 5分钟过期
|
|||
AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(5d), |
|||
}); |
|||
|
|||
return cacheItem.AllGranted; |
|||
} |
|||
} |
|||
} |
|||
@ -1,33 +0,0 @@ |
|||
using LINGYUN.Abp.Hangfire.Dashboard.Localization; |
|||
using Volo.Abp.Authorization.Permissions; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace LINGYUN.Abp.Hangfire.Dashboard.Authorization |
|||
{ |
|||
public class HangfireDashboardPermissionDefinitionProvider : PermissionDefinitionProvider |
|||
{ |
|||
public override void Define(IPermissionDefinitionContext context) |
|||
{ |
|||
var group = context.AddGroup( |
|||
HangfireDashboardPermissions.GroupName, |
|||
L("Permission:Hangfire"), |
|||
MultiTenancySides.Host); // 除非对Hangfire Api进行改造,否则不能区分租户
|
|||
|
|||
var dashboard = group.AddPermission( |
|||
HangfireDashboardPermissions.Dashboard.Default, |
|||
L("Permission:Dashboard"), |
|||
MultiTenancySides.Host); |
|||
|
|||
dashboard.AddChild( |
|||
HangfireDashboardPermissions.Dashboard.ManageJobs, |
|||
L("Permission:ManageJobs"), |
|||
MultiTenancySides.Host); |
|||
} |
|||
|
|||
private static LocalizableString L(string name) |
|||
{ |
|||
return LocalizableString.Create<HangfireDashboardResource>(name); |
|||
} |
|||
} |
|||
} |
|||
@ -1,15 +0,0 @@ |
|||
namespace LINGYUN.Abp.Hangfire.Dashboard.Authorization |
|||
{ |
|||
public static class HangfireDashboardPermissions |
|||
{ |
|||
public const string GroupName = "Hangfire"; |
|||
|
|||
public static class Dashboard |
|||
{ |
|||
public const string Default = GroupName + ".Dashboard"; |
|||
|
|||
public const string ManageJobs = Default + ".ManageJobs"; |
|||
// TODO: other pages...
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using Hangfire.Dashboard; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace LINGYUN.Abp.Hangfire.Dashboard.Authorization |
|||
{ |
|||
public interface IDashboardPermissionChecker |
|||
{ |
|||
Task<bool> IsGrantedAsync(DashboardContext context, string[] requiredPermissionNames); |
|||
} |
|||
} |
|||
@ -1,9 +0,0 @@ |
|||
using Volo.Abp.Localization; |
|||
|
|||
namespace LINGYUN.Abp.Hangfire.Dashboard.Localization |
|||
{ |
|||
[LocalizationResourceName("HangfireDashboard")] |
|||
public class HangfireDashboardResource |
|||
{ |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
{ |
|||
"culture": "en", |
|||
"texts": { |
|||
"Permission:Hangfire": "Hangfire", |
|||
"Permission:Dashboard": "Dashboard" |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
{ |
|||
"culture": "zh-Hans", |
|||
"texts": { |
|||
"Permission:Hangfire": "Hangfire", |
|||
"Permission:Dashboard": "仪表板" |
|||
} |
|||
} |
|||
Loading…
Reference in new issue