Skip to content

Commit 1e47c45

Browse files
committed
requestcontext refactor
1 parent ea0a809 commit 1e47c45

File tree

20 files changed

+109
-48
lines changed

20 files changed

+109
-48
lines changed

src/Ahk.GradeManagement/Ahk.GradeManagement.Api/Ahk.GradeManagement.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
</ItemGroup>
3535

3636
<ItemGroup>
37+
<ProjectReference Include="..\Ahk.GradeManagement.Backend.Common\Ahk.GradeManagement.Backend.Common.csproj" />
3738
<ProjectReference Include="..\Ahk.GradeManagement.Dal\Ahk.GradeManagement.Dal.csproj" />
3839
<ProjectReference Include="..\Ahk.GradeManagement.Bll\Ahk.GradeManagement.Bll.csproj" />
3940
<ProjectReference Include="..\Ahk.GradeManagement.Shared\Ahk.GradeManagement.Shared.csproj" />

src/Ahk.GradeManagement/Ahk.GradeManagement.Api/Authorization/Handlers/DemonstratorOnSubjectRequirementHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Ahk.GradeManagement.Api.Authorization.Policies;
2+
using Ahk.GradeManagement.Backend.Common.RequestContext;
23
using Ahk.GradeManagement.Shared.Enums;
34

45
using Microsoft.AspNetCore.Authorization;
@@ -7,7 +8,7 @@
78

89
namespace Ahk.GradeManagement.Api.Authorization.Handlers;
910

10-
public class DemonstratorOnSubjectRequirementHandler(IHttpContextAccessor httpContextAccessor)
11+
public class DemonstratorOnSubjectRequirementHandler(IHttpContextAccessor httpContextAccessor, IRequestContext requestContext)
1112
: AuthorizationHandler<DemonstratorOnSubjectRequirement>
1213
{
1314
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, DemonstratorOnSubjectRequirement requirement)
@@ -25,13 +26,12 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
2526
return Task.CompletedTask;
2627
}
2728

28-
if (httpContext.Request.Headers.TryGetValue("X-Subject-Id-Value", out var subjectIdHeader)
29-
&& long.TryParse(subjectIdHeader, out var subjectId))
29+
if (requestContext.CurrentUser?.CurrentSubjectId is not null)
3030
{
3131
var subjectAccessClaims = context.User.FindAll(CustomClaimTypes.SubjectAccess).Select(c => c.Value).ToList();
32-
var roleOnSubjectClaim = context.User.FindFirst($"{CustomClaimTypes.AccessLevel}_{subjectId}");
32+
var roleOnSubjectClaim = context.User.FindFirst($"{CustomClaimTypes.AccessLevel}_{requestContext.CurrentUser.CurrentSubjectId}");
3333

34-
if (subjectAccessClaims.Contains(subjectId.ToString(CultureInfo.InvariantCulture))
34+
if (subjectAccessClaims.Contains(requestContext.CurrentUser.CurrentSubjectId.Value.ToString(CultureInfo.InvariantCulture))
3535
&& roleOnSubjectClaim != null
3636
&& (roleOnSubjectClaim.Value == UserRoleOnSubject.Demonstrator.ToString() || roleOnSubjectClaim.Value == UserRoleOnSubject.Teacher.ToString()))
3737
{

src/Ahk.GradeManagement/Ahk.GradeManagement.Api/Authorization/Handlers/TeacherOnSubjectRequirementHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Ahk.GradeManagement.Api.Authorization.Policies;
2+
using Ahk.GradeManagement.Backend.Common.RequestContext;
23
using Ahk.GradeManagement.Shared.Enums;
34

45
using Microsoft.AspNetCore.Authorization;
@@ -7,7 +8,7 @@
78

89
namespace Ahk.GradeManagement.Api.Authorization.Handlers;
910

10-
public class TeacherOnSubjectRequirementHandler(IHttpContextAccessor httpContextAccessor)
11+
public class TeacherOnSubjectRequirementHandler(IHttpContextAccessor httpContextAccessor, IRequestContext requestContext)
1112
: AuthorizationHandler<TeacherOnSubjectRequirement>
1213
{
1314
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TeacherOnSubjectRequirement requirement)
@@ -25,13 +26,12 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
2526
return Task.CompletedTask;
2627
}
2728

28-
if (httpContext.Request.Headers.TryGetValue("X-Subject-Id-Value", out var subjectIdHeader)
29-
&& long.TryParse(subjectIdHeader, out var subjectId))
29+
if (requestContext.CurrentUser?.CurrentSubjectId is not null)
3030
{
3131
var subjectAccessClaims = context.User.FindAll(CustomClaimTypes.SubjectAccess).Select(c => c.Value).ToList();
32-
var roleOnSubjectClaim = context.User.FindFirst($"{CustomClaimTypes.AccessLevel}_{subjectId}");
32+
var roleOnSubjectClaim = context.User.FindFirst($"{CustomClaimTypes.AccessLevel}_{requestContext.CurrentUser.CurrentSubjectId}");
3333

34-
if (subjectAccessClaims.Contains(subjectId.ToString(CultureInfo.InvariantCulture))
34+
if (subjectAccessClaims.Contains(requestContext.CurrentUser.CurrentSubjectId.Value.ToString(CultureInfo.InvariantCulture))
3535
&& roleOnSubjectClaim != null
3636
&& roleOnSubjectClaim.Value == UserRoleOnSubject.Teacher.ToString())
3737
{

src/Ahk.GradeManagement/Ahk.GradeManagement.Api/Controllers/SubjectController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Ahk.GradeManagement.Api.Controllers;
1212

1313
[Route("api/subjects")]
1414
[ApiController]
15-
public class SubjectController(SubjectService subjectService, IHttpContextAccessor httpContextAccessor)
15+
public class SubjectController(SubjectService subjectService)
1616
: CrudControllerBase<SubjectRequest, SubjectResponse>(subjectService)
1717
{
1818
[HttpPut("{id:long}")]

src/Ahk.GradeManagement/Ahk.GradeManagement.Api/Middlewares/HeaderMiddleware.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Ahk.GradeManagement/Ahk.GradeManagement.Api/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Ahk.GradeManagement.Api.Authorization;
22
using Ahk.GradeManagement.Api.Middlewares;
33
using Ahk.GradeManagement.Api.Middlewares.ExceptionHandlers;
4+
using Ahk.GradeManagement.Api.RequestContext;
45
using Ahk.GradeManagement.Bll;
56
using Ahk.GradeManagement.Bll.Profiles;
67
using Ahk.GradeManagement.Dal;
@@ -29,7 +30,7 @@ public static void Main(string[] args)
2930
new DefaultAzureCredential());
3031
}
3132

32-
builder.Services.AddHttpContextAccessor();
33+
builder.Services.AddRequestContext();
3334
builder.Services.AddHttpClient();
3435

3536
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
@@ -81,7 +82,6 @@ public static void Main(string[] args)
8182
app.UseHttpsRedirection();
8283

8384
app.UseExceptionHandler();
84-
app.UseMiddleware<HeaderMiddleware>();
8585

8686
app.UseBlazorFrameworkFiles();
8787
app.UseStaticFiles();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Ahk.GradeManagement.Backend.Common.RequestContext;
2+
using Ahk.GradeManagement.Bll.Services;
3+
using Ahk.GradeManagement.Shared.Constants;
4+
5+
using Microsoft.Identity.Web;
6+
7+
namespace Ahk.GradeManagement.Api.RequestContext;
8+
9+
public class HttpRequestContext(IHttpContextAccessor httpContextAccessor) : IRequestContext
10+
{
11+
public bool IsAuthenticated => HttpContext.User?.Identity?.IsAuthenticated ?? false;
12+
13+
public RequestUser? CurrentUser => HttpContext.User?.Identity?.IsAuthenticated ?? false
14+
? new RequestUser(
15+
DisplayName: HttpContext.User.GetDisplayName()!,
16+
Email: HttpContext.User.GetCurrentUserEmail(),
17+
CurrentSubjectId: HttpContext.Request.Headers.TryGetValue(Headers.XSubjectId, out var subjectIdHeader) && long.TryParse(subjectIdHeader, out var subjectId) ? subjectId : null)
18+
: null;
19+
20+
public CancellationToken RequestAborted => HttpContext.RequestAborted;
21+
22+
private HttpContext HttpContext => httpContextAccessor.HttpContext!;
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Ahk.GradeManagement.Backend.Common.RequestContext;
2+
3+
namespace Ahk.GradeManagement.Api.RequestContext;
4+
5+
public static class RequestContextExtensions
6+
{
7+
public static IServiceCollection AddRequestContext(this IServiceCollection services)
8+
{
9+
services.AddHttpContextAccessor();
10+
services.AddSingleton<IRequestContext, HttpRequestContext>();
11+
12+
return services;
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Ahk.GradeManagement.Backend.Common.RequestContext;
2+
3+
public interface IRequestContext
4+
{
5+
public bool IsAuthenticated { get; }
6+
public RequestUser? CurrentUser { get; }
7+
public CancellationToken RequestAborted { get; }
8+
}
9+
10+
public record RequestUser(string Email, string DisplayName, long? CurrentSubjectId);

0 commit comments

Comments
 (0)