Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary constructors #1344

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/api/ApiEndpointTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class ApiEndpointTransformer : IOutboundParameterTransformer
return string.Empty;
}
var endpoint = value.ToString();
return Regex.Replace(endpoint!, "([a-z])([A-Z])", o => string.Format("{0}-{1}", o.Groups[1].Value, o.Groups[2].Value));
return Regex.Replace(endpoint!, "([a-z])([A-Z])", o => $"{o.Groups[1].Value}-{o.Groups[2].Value}");
}
}
34 changes: 11 additions & 23 deletions backend/api/Authorization/ApplicationRoleAuthorizationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,18 @@

namespace api.Authorization;

public class ApplicationRoleAuthorizationHandler : AuthorizationHandler<ApplicationRoleRequirement>
public class ApplicationRoleAuthorizationHandler(
IProjectAccessRepository projectAccessRepository,
IHttpContextAccessor httpContextAccessor,
ILogger<ApplicationRoleAuthorizationHandler> logger)
: AuthorizationHandler<ApplicationRoleRequirement>
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IProjectAccessRepository _projectAccessRepository;
private readonly ILogger<ApplicationRoleAuthorizationHandler> _logger;



public ApplicationRoleAuthorizationHandler(
IProjectAccessRepository projectAccessRepository,
IHttpContextAccessor httpContextAccessor,
ILogger<ApplicationRoleAuthorizationHandler> logger
)
{
_httpContextAccessor = httpContextAccessor;
_logger = logger;
_projectAccessRepository = projectAccessRepository;
}
protected override async Task<Task> HandleRequirementAsync(
AuthorizationHandlerContext context,
ApplicationRoleRequirement requirement
)
{
var requestPath = _httpContextAccessor.HttpContext?.Request.Path;
var requestPath = httpContextAccessor.HttpContext?.Request.Path;

// Accessing the swagger documentation is always allowed.
if (IsAccessingSwagger(requestPath))
Expand Down Expand Up @@ -134,7 +122,7 @@ List<ApplicationRole> userRoles

private ActionType? GetActionTypeFromEndpoint()
{
var endpoint = _httpContextAccessor.HttpContext?.GetEndpoint();
var endpoint = httpContextAccessor.HttpContext?.GetEndpoint();
if (endpoint == null) { return null; }

var controllerActionDescriptor = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
Expand All @@ -149,7 +137,7 @@ List<ApplicationRole> userRoles

private async Task<Project?> GetCurrentProject(AuthorizationHandlerContext context)
{
var projectId = _httpContextAccessor.HttpContext?.Request.RouteValues["projectId"];
var projectId = httpContextAccessor.HttpContext?.Request.RouteValues["projectId"];
if (projectId == null)
{
return null;
Expand All @@ -160,7 +148,7 @@ List<ApplicationRole> userRoles
return null;
}

var project = await _projectAccessRepository.GetProjectById(projectIdGuid);
var project = await projectAccessRepository.GetProjectById(projectIdGuid);

// /*
// Some projects have the external id set as the id.
Expand All @@ -186,7 +174,7 @@ List<ApplicationRole> userRoles
{
context.Fail();
var username = context.User.Identity!.Name;
_logger.LogWarning(
logger.LogWarning(
"User '{Username}' attempted to access '{RequestPath}' but was not authorized "
+ "- one of the following roles '{RequiredRoles}' is required , while user has the roles '{UserRoles}'",
username,
Expand All @@ -198,7 +186,7 @@ List<ApplicationRole> userRoles

private void HandleUnauthenticatedRequest(AuthorizationHandlerContext context, PathString? requestPath)
{
_logger.LogWarning("An unauthenticated user attempted to access '{RequestPath}'", requestPath);
logger.LogWarning("An unauthenticated user attempted to access '{RequestPath}'", requestPath);
context.Fail();
}
}
2 changes: 0 additions & 2 deletions backend/api/Authorization/ApplicationRolePolicyProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Diagnostics;

using Microsoft.AspNetCore.Authorization;

namespace api.Authorization;
Expand Down
9 changes: 2 additions & 7 deletions backend/api/Authorization/ApplicationRoleRequirement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace api.Authorization;

public class ApplicationRoleRequirement : IAuthorizationRequirement
public class ApplicationRoleRequirement(List<ApplicationRole> roles) : IAuthorizationRequirement
{
public ApplicationRoleRequirement(List<ApplicationRole> roles)
{
Roles = roles;
}

public static ApplicationRole DefaultApplicationRole { get; } = ApplicationRole.Admin;

public List<ApplicationRole> Roles { get; private set; }
public List<ApplicationRole> Roles { get; private set; } = roles;
}
36 changes: 15 additions & 21 deletions backend/api/Authorization/ClaimsMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@

namespace api.Authorization;

public class ClaimsMiddleware
public class ClaimsMiddleware(
RequestDelegate nextMiddleware,
ILogger<ClaimsMiddleware> logger)
{
public static readonly string ApplicationRoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";
private readonly ILogger<ClaimsMiddleware> _logger;
private readonly RequestDelegate _nextMiddleware;
public ClaimsMiddleware(RequestDelegate nextMiddleware,
ILogger<ClaimsMiddleware> logger,
IConfiguration configuration)
{
_nextMiddleware = nextMiddleware;
_logger = logger;
}
public const string ApplicationRoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role";

public async Task InvokeAsync(HttpContext httpContext, CurrentUser currentUser)
{
if (httpContext.User == null)
{
_logger.LogError("User null");
logger.LogError("User null");
}

currentUser.Username = httpContext.User?.Identity?.Name;
Expand All @@ -34,10 +28,10 @@ public async Task InvokeAsync(HttpContext httpContext, CurrentUser currentUser)
}
else
{
_logger.LogError("Unauthenticated access attempted on: " + httpContext.Request.Path);
logger.LogError("Unauthenticated access attempted on: " + httpContext.Request.Path);
}

await _nextMiddleware(httpContext);
await nextMiddleware(httpContext);
}

private void SetAppRoleClaims(HttpContext httpContext)
Expand All @@ -50,7 +44,7 @@ private void SetAppRoleClaims(HttpContext httpContext)
var fusionApplicationRole = RoleForAccountType(httpContext);
if (fusionApplicationRole != null)
{
_logger.LogInformation("Fusion Application Role: " + fusionApplicationRole.Value);
logger.LogInformation("Fusion Application Role: " + fusionApplicationRole.Value);
}

var applicationRoleClaims = applicationRoles
Expand All @@ -59,12 +53,12 @@ private void SetAppRoleClaims(HttpContext httpContext)

var rolesAsString = string.Join(",", applicationRoleClaims.Select(x => x.Value.ToString()));

_logger.LogInformation("Application Roles for User {UserName}: {roles}", httpContext.User?.Identity?.Name, rolesAsString);
logger.LogInformation("Application Roles for User {UserName}: {roles}", httpContext.User?.Identity?.Name, rolesAsString);

var claimsIdentity = httpContext.User?.Identity as ClaimsIdentity;
if (claimsIdentity == null)
{
_logger.LogError("ClaimsIdentity null");
logger.LogError("ClaimsIdentity null");
return;
}
claimsIdentity.AddClaims(applicationRoleClaims);
Expand All @@ -81,23 +75,23 @@ private void SetAppRoleClaims(HttpContext httpContext)
}
if (httpContext.User.IsAccountType(FusionAccountType.Employee))
{
_logger.LogInformation("Check for Fusion Account Type: " + ApplicationRole.User);
logger.LogInformation("Check for Fusion Account Type: " + ApplicationRole.User);
return ApplicationRole.User;
}

if (httpContext.User.IsAccountType(FusionAccountType.External))
{
_logger.LogInformation("Check for Fusion Account Type: " + ApplicationRole.User);
logger.LogInformation("Check for Fusion Account Type: " + ApplicationRole.User);
return ApplicationRole.Admin;
}

if (httpContext.User.IsAccountType(FusionAccountType.Consultant))
{
_logger.LogInformation("Check for Fusion Account Type: " + ApplicationRole.User);
logger.LogInformation("Check for Fusion Account Type: " + ApplicationRole.User);
return ApplicationRole.ReadOnly;
}

_logger.LogInformation("Check for Fusion Account Type: null");
logger.LogInformation("Check for Fusion Account Type: null");
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion backend/api/Context/ChangeLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace api.Context;

public static class ChangeLogService
{
private static readonly IReadOnlyList<string> PropertyNamesToIgnore = new List<string> { "ModifyTime", "Bar", "Baz" };
private static readonly IReadOnlyList<string> PropertyNamesToIgnore = new List<string> { "ModifyTime" };
public static List<ChangeLog> GenerateChangeLogs(DcdDbContext dbContext, CurrentUser? currentUser, DateTime utcNow)
{
var changes = dbContext.ChangeTracker
Expand Down
Loading