Skip to content

Commit 34988bd

Browse files
committed
Role Manager
1 parent 10583c2 commit 34988bd

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Common;
2+
3+
public interface IRoleManager
4+
{
5+
public bool ValidateRole(Cis2User user, Role role);
6+
}

application/CohortManager/src/Functions/Shared/Common/Authentication/RoleMapper.cs renamed to application/CohortManager/src/Functions/Shared/Common/Authentication/RoleManager.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ namespace Common;
22

33
using Microsoft.Extensions.Options;
44

5-
public class RoleMapper
5+
public class RoleManager : IRoleManager
66
{
77
private readonly Dictionary<string, Role> _roleMappings;
88

99
IOptions<RoleConfig> _roleConfig;
10-
public RoleMapper(IOptions<RoleConfig> roleConfig)
10+
public RoleManager(IOptions<RoleConfig> roleConfig)
1111
{
1212
_roleConfig = roleConfig;
1313
_roleMappings = new Dictionary<string, Role>
@@ -16,9 +16,16 @@ public RoleMapper(IOptions<RoleConfig> roleConfig)
1616
{ _roleConfig.Value.CohortManagerDummyGpRemovalWorkgroupId, Role.CohortManagerDummyGpRemoval }
1717
};
1818
}
19-
20-
public Role? GetRole(string workgroupId)
19+
public bool ValidateRole(Cis2User user, Role role)
2120
{
22-
return _roleMappings.TryGetValue(workgroupId, out var mappedRole) ? mappedRole : null;
21+
22+
var workgroupId = _roleMappings.FirstOrDefault(x => x.Value == role).Key;
23+
24+
if (workgroupId == null)
25+
{
26+
return false;
27+
}
28+
return user.NhsidNrbacRoles.Count(x => x.WorkgroupsCodes.Contains(workgroupId)) > 0;
2329
}
30+
2431
}

application/CohortManager/src/Functions/Shared/Common/Authentication/Role.cs renamed to application/CohortManager/src/Functions/Shared/Common/Authentication/RolesEnum.cs

File renamed without changes.

application/CohortManager/src/Functions/Shared/Common/Extensions/AuthenticationExtension.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static IHostBuilder AddAuthentication(this IHostBuilder hostBuilder)
99
{
1010

1111
hostBuilder.AddConfiguration<AuthConfig>();
12+
hostBuilder.AddConfiguration<RoleConfig>();
1213
hostBuilder.ConfigureFunctionsWorkerDefaults(workerOptions =>
1314
{
1415
workerOptions.UseMiddleware<Cis2AuthMiddleware>();
@@ -17,6 +18,7 @@ public static IHostBuilder AddAuthentication(this IHostBuilder hostBuilder)
1718
{
1819
services.AddSingleton<IAuthenticationService, JwtAuthentication>();
1920
services.AddSingleton<ICis2UserService,Cis2UserService>();
21+
services.AddSingleton<IRoleManager, RoleManager>();
2022
});
2123
return hostBuilder;
2224
}

application/CohortManager/src/Functions/screeningDataServices/GetValidationExceptions/GetValidationExceptions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ public class GetValidationExceptions
2424
private readonly IValidationExceptionData _validationData;
2525
private readonly IHttpParserHelper _httpParserHelper;
2626
private readonly IPaginationService<ValidationException> _paginationService;
27+
private readonly IRoleManager _roleManager;
2728

28-
public GetValidationExceptions(ILogger<GetValidationExceptions> logger, ICreateResponse createResponse, IValidationExceptionData validationData, IHttpParserHelper httpParserHelper, IPaginationService<ValidationException> paginationService)
29+
public GetValidationExceptions(ILogger<GetValidationExceptions> logger, ICreateResponse createResponse, IValidationExceptionData validationData, IHttpParserHelper httpParserHelper, IPaginationService<ValidationException> paginationService, IRoleManager roleManager)
2930
{
3031
_logger = logger;
3132
_createResponse = createResponse;
3233
_validationData = validationData;
3334
_httpParserHelper = httpParserHelper;
3435
_paginationService = paginationService;
36+
_roleManager = roleManager;
3537
}
3638

3739
/// <summary>
@@ -57,6 +59,12 @@ public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymou
5759
var isReport = _httpParserHelper.GetQueryParameterAsBool(req, "isReport");
5860
var ruleId = _httpParserHelper.GetQueryParameterAsNullableInt(req, "ruleId");
5961
var dateCreated = _httpParserHelper.GetQueryParameterAsDateTime(req, "dateCreated");
62+
63+
if (!_roleManager.ValidateRole((Cis2User)req.FunctionContext.Items["Cis2User"]!, Role.CohortManagerUser))
64+
{
65+
return _createResponse.CreateHttpResponse(HttpStatusCode.Forbidden, req);
66+
}
67+
6068
try
6169
{
6270
if (exceptionId > 0)

0 commit comments

Comments
 (0)