Files
Backend-Api/ServiceHost/MiddleWare/CheckTaskMiddleware.cs
2024-10-23 17:49:04 +03:30

60 lines
2.2 KiB
C#

using System.Security.Claims;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using AccountManagement.Domain.TaskAgg;
using _0_Framework.Application;
namespace ServiceHost.MiddleWare
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class CheckTaskMiddleware
{
private readonly RequestDelegate _next;
private readonly IAuthHelper _authHelper;
public CheckTaskMiddleware(RequestDelegate next, IAuthHelper authHelper)
{
_next = next;
_authHelper = authHelper;
}
public Task Invoke(HttpContext httpContext, ITaskRepository taskRepository)
{
if (httpContext.User.Identity is { IsAuthenticated: true })
{
if (httpContext.User.HasClaim(x => x is { Type: "AdminAreaPermission", Value: "true" }))
{
string path = httpContext.Request.Path.ToString();
if (!httpContext.User.HasClaim(x => (x is { Type: "RoleId", Value: "1" })) &&
!httpContext.Request.Path.Equals("/task", StringComparison.OrdinalIgnoreCase)&&
!httpContext.Request.Path.Equals("/serviceWorker", StringComparison.OrdinalIgnoreCase))
{
long userId = _authHelper.CurrentAccountId();
var result = taskRepository.HasOverdueTasks(userId);
if (result)
{
httpContext.Response.Redirect("/task");
}
}
else
{
Console.WriteLine("else");
}
}
}
return _next(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CheckTaskMiddlewareExtensions
{
public static IApplicationBuilder UseCheckTaskMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CheckTaskMiddleware>();
}
}
}