Skip to content

Commit

Permalink
Merge pull request #1179 from DFE-Digital/feature/179914-assign-user-…
Browse files Browse the repository at this point in the history
…role

Assign user with role capabilities
  • Loading branch information
mshakirdfe authored Sep 17, 2024
2 parents b71c4ca + 43892f8 commit 3fe624c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace Dfe.PrepareConversions.Data.Models.UserRole
{
public enum RoleCapability
{
DeleteTransferProject,
DeleteGroupProject,
CreateConversionProject,
CreateTransferProject,
DeleteConversionProject,
UserAdministrator
DeleteTransferProject
}
}
Original file line number Diff line number Diff line change
@@ -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<ApiResponse<RoleCapabilitiesModel>> GetRolesCapabilities(List<string> roles);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiResponse<RoleCapabilitiesModel>> GetUserRoleCapabilities(string email)
public async Task<ApiResponse<RoleCapabilitiesModel>> GetRolesCapabilities(List<string> roles)
{
var httpClient = httpClientFactory.CreateAcademisationClient();

ApiResponse<RoleCapabilitiesModel> result = await httpClientService.Get<RoleCapabilitiesModel>(
var result = await httpClientService.Post<List<string>, RoleCapabilitiesModel>(
httpClient,
string.Format(PathFor.GetCapabilities, email));
PathFor.GetRoleCapabilities, roles);

if (result.Success is false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ ISystemClock clock

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
List<Claim> claims = new() { new(ClaimTypes.Name, "Name") };
List<Claim> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,16 @@ public UpdateAcademyConversionProject AddPatchProject<TProperty>(AcademyConversi
return request;
}

public void GetRoleCapabilities(string name)
public void GetRoleCapabilities(List<string> 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<UpdateAcademyConversionProject> configure = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,16 +17,16 @@ public class CapabilitiyMiddleware(RequestDelegate next, ILogger<CapabilitiyMidd
public const string SESSION_KEY = "RoleCapabilities";
private readonly ILogger<CapabilitiyMiddleware> _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())
{
var sessionKey = $"{SESSION_KEY}_{httpContext.User.Identity.Name}";
if (session.Get<string>(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));
}
}
Expand Down
2 changes: 1 addition & 1 deletion Dfe.PrepareConversions/Dfe.PrepareConversions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped<KeyStagePerformanceService>();
services.AddScoped<ITrustsRepository, TrustsRepository>();
services.AddScoped<IProjectGroupsRepository, ProjectGroupsRepository>();
services.AddScoped<IUserRoleRepository, UserRoleRepository>();
services.AddScoped<IRoleCapablitiesRepository, RoleCapablitiesRepository>();
services.AddScoped<IAcademyConversionProjectRepository, AcademyConversionProjectRepository>();
services.AddScoped<IAcademyConversionAdvisoryBoardDecisionRepository, AcademyConversionAdvisoryBoardDecisionRepository>();
services.AddScoped<IHttpClientService, HttpClientService>();
Expand Down

0 comments on commit 3fe624c

Please sign in to comment.