From 905fcae0e274893a2e610f2ea5b20c346f535bc8 Mon Sep 17 00:00:00 2001 From: "SHAKIR, Muhammad" Date: Mon, 16 Sep 2024 12:24:26 +0100 Subject: [PATCH 1/2] Assign user with role capabilitiies --- .../Dfe.PrepareConversions.Data/Features/PathFor.cs | 2 +- .../Models/UserRole/RoleCapability.cs | 6 +++--- .../Interfaces/IRoleCapablitiesRepository.cs | 12 ++++++++++++ .../Services/Interfaces/IUserRoleRepository.cs | 11 ----------- ...oleRepository.cs => RoleCapablitiesRepository.cs} | 11 ++++++----- .../Pages/BaseIntegrationTests.MockData.cs | 11 +++++++---- .../Pages/BaseIntegrationTests.cs | 2 +- .../Middleware/CapabilitiyMiddleware.cs | 8 +++++--- .../Dfe.PrepareConversions/Startup.cs | 2 +- 9 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IRoleCapablitiesRepository.cs delete mode 100644 Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IUserRoleRepository.cs rename Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/{UserRoleRepository.cs => RoleCapablitiesRepository.cs} (60%) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Features/PathFor.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Features/PathFor.cs index 3c7dd312f..434f8496c 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Features/PathFor.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Features/PathFor.cs @@ -30,7 +30,7 @@ public PathFor(IFeatureManager features) public static string SetAssignedUser => "/conversion-project/{0}/SetAssignedUser"; public static string SetFormAMatAssignedUser => "/conversion-project/{0}/SetFormAMatAssignedUser"; public static string DeleteConversionProject => "/conversion-project/{0}/delete"; - public static string GetCapabilities => "/user-role/{0}"; + public static string GetRoleCapabilities => "/role-capabilities/capabilities"; public static string GetAllProjectsV2 => "/conversion-project/projects"; public static string GetFormAMatProjects => "/conversion-project/FormAMatProjects"; public static string SearchFormAMatProjects => "/conversion-project/search-formamatprojects"; diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Models/UserRole/RoleCapability.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Models/UserRole/RoleCapability.cs index e069993bc..196580196 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Models/UserRole/RoleCapability.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Models/UserRole/RoleCapability.cs @@ -3,9 +3,9 @@ namespace Dfe.PrepareConversions.Data.Models.UserRole { public enum RoleCapability { - DeleteTransferProject, - DeleteGroupProject, + CreateConversionProject, + CreateTransferProject, DeleteConversionProject, - UserAdministrator + DeleteTransferProject } } diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IRoleCapablitiesRepository.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IRoleCapablitiesRepository.cs new file mode 100644 index 000000000..f39b9c985 --- /dev/null +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IRoleCapablitiesRepository.cs @@ -0,0 +1,12 @@ + +using Dfe.PrepareConversions.Data.Models.UserRole; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Dfe.PrepareConversions.Data.Services.Interfaces +{ + public interface IRoleCapablitiesRepository + { + Task> GetRolesCapabilities(List roles); + } +} diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IUserRoleRepository.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IUserRoleRepository.cs deleted file mode 100644 index ea0981ca5..000000000 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/Interfaces/IUserRoleRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ - -using Dfe.PrepareConversions.Data.Models.UserRole; -using System.Threading.Tasks; - -namespace Dfe.PrepareConversions.Data.Services.Interfaces -{ - public interface IUserRoleRepository - { - Task> GetUserRoleCapabilities(string email); - } -} diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/UserRoleRepository.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/RoleCapablitiesRepository.cs similarity index 60% rename from Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/UserRoleRepository.cs rename to Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/RoleCapablitiesRepository.cs index ba8c4bdcc..c80255fa9 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/UserRoleRepository.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Data/Services/RoleCapablitiesRepository.cs @@ -2,20 +2,21 @@ using Dfe.PrepareConversions.Data.Features; using Dfe.PrepareConversions.Data.Models.UserRole; using Dfe.PrepareConversions.Data.Services.Interfaces; +using System.Collections.Generic; using System.Threading.Tasks; namespace Dfe.PrepareConversions.Data.Services { - public class UserRoleRepository(IDfeHttpClientFactory httpClientFactory, - IHttpClientService httpClientService) : IUserRoleRepository + public class RoleCapablitiesRepository(IDfeHttpClientFactory httpClientFactory, + IHttpClientService httpClientService) : IRoleCapablitiesRepository { - public async Task> GetUserRoleCapabilities(string email) + public async Task> GetRolesCapabilities(List roles) { var httpClient = httpClientFactory.CreateAcademisationClient(); - ApiResponse result = await httpClientService.Get( + var result = await httpClientService.Post, RoleCapabilitiesModel>( httpClient, - string.Format(PathFor.GetCapabilities, email)); + PathFor.GetRoleCapabilities, roles); if (result.Success is false) { diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.MockData.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.MockData.cs index 79b9e7ebf..094d3794a 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.MockData.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.MockData.cs @@ -119,13 +119,16 @@ public UpdateAcademyConversionProject AddPatchProject(AcademyConversi return request; } - public void GetRoleCapabilities(string name) + public void GetRoleCapabilities(List roles) { var response = new RoleCapabilitiesModel - { - Capabilities = [RoleCapability.DeleteConversionProject] + { + Capabilities = [ + RoleCapability.CreateConversionProject, + RoleCapability.CreateTransferProject + ] }; - _factory.AddGetWithJsonResponse(string.Format(PathFor.GetCapabilities, name), response); + _factory.AddPostWithJsonRequest(PathFor.GetRoleCapabilities, roles, response); } public UpdateAcademyConversionProject AddPatchConfiguredProject(AcademyConversionProject project, Action configure = null) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.cs index b50bea6ee..8a439a09a 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/Pages/BaseIntegrationTests.cs @@ -34,7 +34,7 @@ protected BaseIntegrationTests(IntegrationTestingWebApplicationFactory factory) _pathFor = new PathFor(featureManager.Object); Context = CreateBrowsingContext(factory.CreateClient()); - GetRoleCapabilities("Name"); + GetRoleCapabilities(["conversionRole", "transferRoles"]); } protected IDocument Document => Context.Active; diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Middleware/CapabilitiyMiddleware.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions/Middleware/CapabilitiyMiddleware.cs index 9b78e54e1..0297cf547 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Middleware/CapabilitiyMiddleware.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Middleware/CapabilitiyMiddleware.cs @@ -6,6 +6,8 @@ using Microsoft.Extensions.Primitives; using Microsoft.IdentityModel.Tokens; using System; +using System.Linq; +using System.Security.Claims; using System.Threading.Tasks; namespace Dfe.PrepareConversions.Middleware @@ -15,7 +17,7 @@ public class CapabilitiyMiddleware(RequestDelegate next, ILogger _logger = logger ?? throw new ArgumentNullException("logger"); - public async Task Invoke(HttpContext httpContext, ICorrelationContext correlationContext, ISession session, IUserRoleRepository userRoleRepository) + public async Task Invoke(HttpContext httpContext, ICorrelationContext correlationContext, ISession session, IRoleCapablitiesRepository roleCapablitiesRepository) { if (httpContext.User.Identity.IsAuthenticated && !httpContext.User.Identity.Name.IsNullOrEmpty()) { @@ -23,8 +25,8 @@ public async Task Invoke(HttpContext httpContext, ICorrelationContext correlatio if (session.Get(sessionKey).IsNullOrEmpty()) { SetCorrelationId(httpContext, correlationContext); - - var roleCapabilitiesModel = await userRoleRepository.GetUserRoleCapabilities(httpContext.User.Identity.Name); + var roles = httpContext.User.Claims.Where(c => c.Type == ClaimTypes.Role).Select(x=> x.Value).ToList(); + var roleCapabilitiesModel = await roleCapablitiesRepository.GetRolesCapabilities(roles); httpContext.Session.Set(sessionKey, string.Join(",", roleCapabilitiesModel.Body.Capabilities)); } } diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs index 378b44c2a..0910f56e9 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs @@ -149,7 +149,7 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); From 43892f843b9fac1d687778e012b001f2f42c5fe3 Mon Sep 17 00:00:00 2001 From: "SHAKIR, Muhammad" Date: Mon, 16 Sep 2024 12:56:15 +0100 Subject: [PATCH 2/2] Fixed tests --- .../IntegrationTestingWebApplicationFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/IntegrationTestingWebApplicationFactory.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/IntegrationTestingWebApplicationFactory.cs index 421bf8fc3..367f267dd 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/IntegrationTestingWebApplicationFactory.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions.Tests/IntegrationTestingWebApplicationFactory.cs @@ -254,7 +254,7 @@ ISystemClock clock protected override Task HandleAuthenticateAsync() { - List claims = new() { new(ClaimTypes.Name, "Name") }; + List claims = [new(ClaimTypes.Name, "Name"), new(ClaimTypes.Role, "conversionRole"), new(ClaimTypes.Role, "transferRoles")]; ClaimsIdentity identity = new(claims, "Test"); ClaimsPrincipal principal = new(identity); AuthenticationTicket ticket = new(principal, "Test");