Skip to content

Commit

Permalink
Include full system and resource objects in GET vendorrequest (#380)
Browse files Browse the repository at this point in the history
* enrich right with actual resource in bff before returning request object to frontend

* add method to GET single system

* include system in VendorRequest return object

* fix tests

* lookup system vendor orgn name in register when setting system in vendorrequest response

* remove frontend mapping of system and resources for vendorrequest

* move mapping of system vendor name and rights for systemuser to backend

* use data from backend

* add function to get list of resources from list of rights. Refactor DTOs to return resources in separate property

* comment improvements

* test-data

* await async calls

* show error message if request.system is null

* cleanup system DTO + move filtering of visible systems to bff

* WIP remove enrichment from bff

* refactor after change in altinn-authentication

* remove register client (supplier name is set in altinn-authentication instead)

* add registerClient + vendor org name lookup back

* fix request path

* add null checks

* rename entity

* rename method name + add cancellationToken to registry request

* fix path after change in backend

* test api change

* fix route again

* test hardcode action

* remove hardcode
  • Loading branch information
mgunnerud authored Oct 8, 2024
1 parent a4aed4c commit 40c9601
Show file tree
Hide file tree
Showing 35 changed files with 370 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public interface IRegisterClient
/// <returns>
/// Party information
/// </returns>
Task<Party> GetPartyForOrganization(string organizationNumber);
Task<Party> GetPartyForOrganization(string organizationNumber, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Altinn.Authentication.UI.Core.Common.Rights;

namespace Altinn.Authentication.UI.Core.SystemRegister;

public interface IResourceRegistryClient
{
Task<ServiceResource?> GetResource(string resourceId, CancellationToken cancellationToken = default);
Task<List<ServiceResource>> GetResources(List<Right> rights, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Altinn.Authentication.UI.Core.SystemRegister;

public interface ISystemRegisterClient
{
Task<List<RegisterSystemResponse>> GetListRegSys(CancellationToken cancellationToken = default);
Task<List<Right>> GetRightFromSystem(string systemId, CancellationToken cancellationToken);
Task<List<RegisteredSystemDTO>> GetListRegSys(CancellationToken cancellationToken = default);
Task<RegisteredSystemDTO?> GetSystem(string systemId, CancellationToken cancellationToken = default);
Task<List<Right>> GetRightsFromSystem(string systemId, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public interface ISystemRegisterService
{
Task<List<RegisterSystemResponse>> GetListRegSys(CancellationToken cancellation = default);
Task<List<RegisteredSystemDTO>> GetListRegSys(CancellationToken cancellation = default);
Task<List<ServiceResource>> GetSystemRights(string systemId, CancellationToken cancellationToken);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Diagnostics.CodeAnalysis;
using Altinn.Authentication.UI.Core.Common.Rights;

namespace Altinn.Authentication.UI.Core.SystemRegister
{
/// <summary>
/// Model for the response of a registered system
/// A Registered System is a product supplied by a System Vendor,
/// it may need Rights to use or acccess Resources at a Service Provider.
/// </summary>
[ExcludeFromCodeCoverage]
public record class RegisteredSystemDTO
{
/// <summary>
/// A unique External Id for this System, in human-readable string format.
/// </summary>
public required string SystemId { get; set; } = string.Empty;

/// <summary>
/// Organization number of the system Vendor that offers the product (system)
/// </summary>
public required string SystemVendorOrgNumber { get; set; }

/// <summary>
/// Organization number of the system Vendor that offers the product (system)
/// </summary>
public string SystemVendorOrgName { get; set; } = string.Empty;

/// <summary>
/// A short name of the product, used when displaying to the user
/// </summary>
public required IDictionary<string, string> Name { get; set; }

/// <summary>
/// A short description of the product, used when displaying to the user
/// </summary>
public required IDictionary<string, string> Description { get; set; }

/// <summary>
/// The array of Rights versus System Provider's Resources needed to use this Registered System
/// </summary>
public List<Right> Rights { get; set; } = [];
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Altinn.Authentication.UI.Core.SystemRegister;
using Altinn.Authentication.UI.Core.Common.Rights;

namespace Altinn.Authentication.UI.Core.SystemRegister;

public class SystemRegisterService : ISystemRegisterService
{
Expand All @@ -16,28 +18,15 @@ public SystemRegisterService(
_resourceRegistryClient = resourceRegistryClient;
}

public async Task<List<RegisterSystemResponse>> GetListRegSys(CancellationToken cancellationToken)
public async Task<List<RegisteredSystemDTO>> GetListRegSys(CancellationToken cancellationToken)
{
List<RegisterSystemResponse> lista = [];

lista = await _systemRegisterClient.GetListRegSys(cancellationToken );

foreach (RegisterSystemResponse response in lista)
List<RegisteredSystemDTO> lista = await _systemRegisterClient.GetListRegSys(cancellationToken);
foreach (RegisteredSystemDTO response in lista)
{
foreach (RightFrontEnd right in response.Rights)
{
string? resourceId = right.Resource.Find(x => x.Id == "urn:altinn:resource")?.Value;

if (resourceId != null)
{
right.ServiceResource = await _resourceRegistryClient.GetResource(resourceId, cancellationToken);
}
}

try
{
response.SystemVendorOrgName =
(await _registerClient.GetPartyForOrganization(response.SystemVendorOrgNumber)).Organization.Name;
(await _registerClient.GetPartyForOrganization(response.SystemVendorOrgNumber, cancellationToken)).Organization.Name;
}
catch (Exception ex)
{
Expand All @@ -48,4 +37,10 @@ public async Task<List<RegisterSystemResponse>> GetListRegSys(CancellationToken

return lista;
}

public async Task<List<ServiceResource>> GetSystemRights(string systemId, CancellationToken cancellationToken)
{
List<Right> right = await _systemRegisterClient.GetRightsFromSystem(systemId, cancellationToken);
return await _resourceRegistryClient.GetResources(right, cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Altinn.Authorization.ProblemDetails;
using Altinn.Authentication.UI.Core.SystemRegister;
using Altinn.Authorization.ProblemDetails;

namespace Altinn.Authentication.UI.Core.SystemUsers;

Expand All @@ -7,12 +8,42 @@ namespace Altinn.Authentication.UI.Core.SystemUsers;
/// </summary>
/// <param name="requestClient">The client</param>
public class RequestService(
IRequestClient requestClient
IRequestClient requestClient,
IResourceRegistryClient resourceRegistryClient,
ISystemRegisterClient systemRegisterClient,
IRegisterClient registerClient
) : IRequestService
{
public async Task<Result<VendorRequest>> GetVendorRequest(int partyId, Guid requestId, CancellationToken cancellationToken = default)
{
return await requestClient.GetVendorRequest(partyId, requestId, cancellationToken);
Result<VendorRequest> request = await requestClient.GetVendorRequest(partyId, requestId, cancellationToken);

if (request.Value != null)
{
// add resources
request.Value.Resources = await resourceRegistryClient.GetResources(request.Value.Rights, cancellationToken);

// add system
RegisteredSystemDTO? system = await systemRegisterClient.GetSystem(request.Value.SystemId, cancellationToken);
request.Value.System = system;

if (request.Value.System != null)
{
// add system name
try
{
request.Value.System.SystemVendorOrgName =
(await registerClient.GetPartyForOrganization(request.Value.System.SystemVendorOrgNumber, cancellationToken)).Organization.Name;
}
catch (Exception ex)
{
request.Value.System.SystemVendorOrgName = "N/A"; // "N/A" stands for "Not Available
Console.Write(ex.ToString());
}
}
}

return request;
}

public async Task<Result<bool>> ApproveRequest(int partyId, Guid requestId, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Altinn.Authentication.UI.Core.Common.Rights;
using Altinn.Authentication.UI.Core.SystemRegister;

namespace Altinn.Authentication.UI.Core.SystemUsers;

Expand Down Expand Up @@ -81,4 +83,16 @@ public class SystemUser
/// </summary>
[JsonPropertyName("supplierOrgno")]
public string SupplierOrgNo { get; set; } = string.Empty;

/// <summary>
/// List of rights for this systemuser
/// </summary>
[JsonPropertyName("rights")]
public List<Right> Rights { get; set; } = [];

/// <summary>
/// List of resources information
/// </summary>
[JsonPropertyName("resources")]
public List<ServiceResource> Resources { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ public class SystemUserService : ISystemUserService
private readonly ISystemUserClient _systemUserClient;
private readonly IAccessManagementClient _accessManagementClient;
private readonly ISystemRegisterClient _systemRegisterClient;
private readonly IRegisterClient _registerClient;
private readonly IResourceRegistryClient _resourceRegistryClient;

public SystemUserService(
ISystemUserClient systemUserClient,
IAccessManagementClient accessManagementClient,
ISystemRegisterClient systemRegisterClient)
ISystemRegisterClient systemRegisterClient,
IRegisterClient registerClient,
IResourceRegistryClient resourceRegistryClient)
{
_systemUserClient = systemUserClient;
_accessManagementClient = accessManagementClient;
_systemRegisterClient = systemRegisterClient;
_registerClient = registerClient;
_resourceRegistryClient = resourceRegistryClient;
}

public async Task<bool> ChangeSystemUserDescription(string newDescr, Guid id, CancellationToken cancellationToken = default)
Expand All @@ -48,12 +54,24 @@ public async Task<List<SystemUser>> GetAllSystemUsersForParty(int id, Cancellati
int reporteePartyId = reportee.PartyId;

var lista = await _systemUserClient.GetSystemUserRealsForChosenUser(reporteePartyId, cancellationToken);

foreach (SystemUser systemUser in lista)
{
await AddRights(systemUser, cancellationToken);
}

return lista;
}

public async Task<SystemUser?> GetSpecificSystemUserDTO(int partyId, Guid id, CancellationToken cancellationToken = default)
{
return await _systemUserClient.GetSpecificSystemUserReal(partyId, id, cancellationToken);
SystemUser? systemUser = await _systemUserClient.GetSpecificSystemUserReal(partyId, id, cancellationToken);
if (systemUser != null)
{
await AddRights(systemUser, cancellationToken);
}

return systemUser;
}

public async Task<Result<SystemUser>> CreateSystemUser(int partyId, CreateSystemUserRequestToAuthComp newSystemUserDescriptor, CancellationToken cancellation = default)
Expand Down Expand Up @@ -82,7 +100,7 @@ public async Task<bool> ChangeSystemUserProduct(string selectedSystemType, Guid

private async Task<DelegationCheckResult> UserDelegationCheckForReportee(int partyId, string systemId ,CancellationToken cancellationToken = default)
{
List<Right> rights = await _systemRegisterClient.GetRightFromSystem(systemId, cancellationToken);
List<Right> rights = await _systemRegisterClient.GetRightsFromSystem(systemId, cancellationToken);
List<RightResponses> rightResponsesList = [];

foreach (Right right in rights)
Expand Down Expand Up @@ -115,4 +133,27 @@ private static bool ResolveIfHasAccess(List<DelegationResponseData> rightRespons

return true;
}

private async Task AddRights(SystemUser systemUser, CancellationToken cancellationToken)
{

// TODO: rights for a systemuser is not 1:1 with system rights, but we have no way to
// get rights for a specific systemuser yet, so return the rights for the system for now.
List<Right> rights = await _systemRegisterClient.GetRightsFromSystem(systemUser.SystemId, cancellationToken);

// add resources
systemUser.Resources = await _resourceRegistryClient.GetResources(rights, cancellationToken);

// add system name
try
{
systemUser.SupplierName =
(await _registerClient.GetPartyForOrganization(systemUser.SupplierOrgNo, cancellationToken)).Organization.Name;
}
catch (Exception ex)
{
systemUser.SupplierName = "N/A"; // "N/A" stands for "Not Available
Console.Write(ex.ToString());
}
}
}
Loading

0 comments on commit 40c9601

Please sign in to comment.