IResourceFilter to IAsyncResourceFilter

This commit is contained in:
Anton Suhorukov 2023-05-21 00:09:11 +03:00
parent 8d7219c0e0
commit fd3f479f7f
4 changed files with 22 additions and 19 deletions

View File

@ -27,7 +27,7 @@
namespace ASC.Api.Core.Middleware;
[Scope]
public class IpSecurityFilter : IResourceFilter
public class IpSecurityFilter : IAsyncResourceFilter
{
private readonly AuthContext _authContext;
private readonly IPSecurity.IPSecurity _iPSecurity;
@ -46,15 +46,13 @@ public class IpSecurityFilter : IResourceFilter
_settingsManager = settingsManager;
}
public void OnResourceExecuted(ResourceExecutedContext context) { }
public void OnResourceExecuting(ResourceExecutingContext context)
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
if (_authContext.IsAuthenticated)
{
var enable = _settingsManager.Load<IPRestrictionsSettings>().Enable;
var enable = (await _settingsManager.LoadAsync<IPRestrictionsSettings>()).Enable;
if (enable && !_iPSecurity.VerifyAsync().GetAwaiter().GetResult())
if (enable && !(await _iPSecurity.VerifyAsync()))
{
context.Result = new ObjectResult(Resource.ErrorIpSecurity)
{
@ -64,5 +62,6 @@ public class IpSecurityFilter : IResourceFilter
return;
}
}
await next();
}
}

View File

@ -27,7 +27,7 @@
namespace ASC.Api.Core.Middleware;
[Scope]
public class PaymentFilter : IResourceFilter
public class PaymentFilter : IAsyncResourceFilter
{
private readonly TenantExtra _tenantExtra;
private readonly ILogger<PaymentFilter> _logger;
@ -40,24 +40,26 @@ public class PaymentFilter : IResourceFilter
public void OnResourceExecuted(ResourceExecutedContext context) { }
public void OnResourceExecuting(ResourceExecutingContext context)
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor
&& controllerActionDescriptor.EndpointMetadata.OfType<AllowNotPaymentAttribute>().Any())
{
_logger.DebugPaymentIsNotRequired();
await next();
return;
}
var header = context.HttpContext.Request.Headers["Payment-Info"];
if (string.IsNullOrEmpty(header) || (bool.TryParse(header, out var flag) && flag))
{
if (_tenantExtra.IsNotPaidAsync(false).Result)
if (await _tenantExtra.IsNotPaidAsync(false))
{
context.Result = new StatusCodeResult((int)HttpStatusCode.PaymentRequired);
_logger.WarningPaymentRequired(context.HttpContext.Request.Url());
return;
}
}
await next();
}
}

View File

@ -29,7 +29,7 @@ using CallContext = ASC.Common.Notify.Engine.CallContext;
namespace ASC.Api.Core.Middleware;
[Scope]
public class ProductSecurityFilter : IResourceFilter
public class ProductSecurityFilter : IAsyncResourceFilter
{
private static readonly IDictionary<string, Guid> _products;
private readonly ILogger<ProductSecurityFilter> _logger;
@ -74,12 +74,11 @@ public class ProductSecurityFilter : IResourceFilter
_authContext = authContext;
}
public void OnResourceExecuted(ResourceExecutedContext context) { }
public void OnResourceExecuting(ResourceExecutingContext context)
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
if (!_authContext.IsAuthenticated)
{
await next();
return;
}
@ -93,13 +92,15 @@ public class ProductSecurityFilter : IResourceFilter
CallContext.SetData("asc.web.product_id", pid);
}
if (! _webItemSecurity.IsAvailableForMeAsync(pid).GetAwaiter().GetResult())
if (!_webItemSecurity.IsAvailableForMeAsync(pid).GetAwaiter().GetResult())
{
context.Result = new StatusCodeResult((int)HttpStatusCode.Forbidden);
_logger.WarningPaymentRequired(controllerActionDescriptor.ControllerName, _authContext.CurrentAccount.ID);
return;
}
}
}
await next();
}
private static Guid FindProduct(ControllerActionDescriptor method)

View File

@ -27,7 +27,7 @@
namespace ASC.Api.Core.Middleware;
[Scope]
public class TenantStatusFilter : IResourceFilter
public class TenantStatusFilter : IAsyncResourceFilter
{
private readonly TenantManager _tenantManager;
private readonly ILogger<TenantStatusFilter> _logger;
@ -40,9 +40,7 @@ public class TenantStatusFilter : IResourceFilter
_tenantManager = tenantManager;
}
public void OnResourceExecuted(ResourceExecutedContext context) { }
public void OnResourceExecuting(ResourceExecutingContext context)
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
var tenant = _tenantManager.GetCurrentTenant(false);
if (tenant == null)
@ -58,6 +56,7 @@ public class TenantStatusFilter : IResourceFilter
context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor &&
controllerActionDescriptor.EndpointMetadata.OfType<AllowSuspendedAttribute>().Any())
{
await next();
return;
}
@ -70,6 +69,7 @@ public class TenantStatusFilter : IResourceFilter
{
if (_passthroughtRequestEndings.Any(path => context.HttpContext.Request.Path.ToString().EndsWith(path, StringComparison.InvariantCultureIgnoreCase)))
{
await next();
return;
}
@ -77,5 +77,6 @@ public class TenantStatusFilter : IResourceFilter
_logger.WarningTenantStatus(tenant.Id, tenant.Status);
return;
}
await next();
}
}