Skip to content

Commit

Permalink
Update parties controller
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar committed Dec 12, 2024
1 parent 029fd81 commit a4720f1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
13 changes: 1 addition & 12 deletions src/Altinn.App.Api/Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Text.Json.Serialization;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Constants;
using Altinn.App.Core.Helpers;
using Altinn.App.Core.Internal.App;
using Altinn.App.Core.Internal.Auth;
using Altinn.Platform.Profile.Models;
using Altinn.Platform.Register.Models;
using Altinn.Platform.Storage.Interface.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
Expand All @@ -20,7 +17,6 @@ public class AuthenticationController : ControllerBase
{
private readonly IAuthenticationClient _authenticationClient;
private readonly GeneralSettings _settings;
private readonly IAppMetadata _appMetadata;
private readonly IAuthenticationContext _authenticationContext;

/// <summary>
Expand All @@ -29,13 +25,11 @@ public class AuthenticationController : ControllerBase
public AuthenticationController(
IAuthenticationClient authenticationClient,
IOptions<GeneralSettings> settings,
IAppMetadata appMetadata,
IServiceProvider serviceProvider
)
{
_authenticationClient = authenticationClient;
_settings = settings.Value;
_appMetadata = appMetadata;
_authenticationContext = serviceProvider.GetRequiredService<IAuthenticationContext>();
}

Expand All @@ -50,8 +44,6 @@ public async Task<ActionResult> GetCurrent()
{
var current = _authenticationContext.Current;

Application application = await _appMetadata.GetApplicationMetadata();

CurrentAuthenticationBaseResponse response = current switch
{
AuthenticationInfo.Unauthenticated => new UnauthenticatedResponse(),
Expand All @@ -61,10 +53,7 @@ AuthenticationInfo.User user when await user.LoadDetails(validateSelectedParty:
Profile = details.Profile,
Party = details.Reportee,
Parties = details.Parties,
PartiesAllowedToInstantiate = InstantiationHelper.FilterPartiesByAllowedPartyTypes(
details.Parties,
application.PartyTypesAllowed
),
PartiesAllowedToInstantiate = details.PartiesAllowedToInstantiate,
},
AuthenticationInfo.Org org when await org.LoadDetails() is var details => new OrgResponse
{
Expand Down
46 changes: 33 additions & 13 deletions src/Altinn.App.Api/Controllers/PartiesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PartiesController : ControllerBase
private readonly IProfileClient _profileClient;
private readonly GeneralSettings _settings;
private readonly IAppMetadata _appMetadata;
private readonly IAuthenticationContext _authenticationContext;

/// <summary>
/// Initializes a new instance of the <see cref="PartiesController"/> class
Expand All @@ -37,14 +38,16 @@ public PartiesController(
IProfileClient profileClient,
IAltinnPartyClient altinnPartyClientClient,
IOptions<GeneralSettings> settings,
IAppMetadata appMetadata
IAppMetadata appMetadata,
IServiceProvider serviceProvider
)
{
_authorizationClient = authorizationClient;
_userHelper = new UserHelper(profileClient, altinnPartyClientClient, settings);
_profileClient = profileClient;
_settings = settings.Value;
_appMetadata = appMetadata;
_authenticationContext = serviceProvider.GetRequiredService<IAuthenticationContext>();
}

/// <summary>
Expand All @@ -58,20 +61,37 @@ IAppMetadata appMetadata
[HttpGet("{org}/{app}/api/v1/parties")]
public async Task<IActionResult> Get(string org, string app, bool allowedToInstantiateFilter = false)
{
UserContext userContext = await _userHelper.GetUserContext(HttpContext);
List<Party>? partyList = await _authorizationClient.GetPartyList(userContext.UserId);

if (allowedToInstantiateFilter)
var context = _authenticationContext.Current;
switch (context)
{
Application application = await _appMetadata.GetApplicationMetadata();
List<Party> validParties = InstantiationHelper.FilterPartiesByAllowedPartyTypes(
partyList,
application.PartyTypesAllowed
);
return Ok(validParties);
case AuthenticationInfo.Unauthenticated:
return Unauthorized();
case AuthenticationInfo.User user:
{
var details = await user.LoadDetails(validateSelectedParty: false);
return allowedToInstantiateFilter ? Ok(details.PartiesAllowedToInstantiate) : Ok(details.Parties);
}
case AuthenticationInfo.Org orgInfo:
{
var details = await orgInfo.LoadDetails();
IReadOnlyList<Party> parties = [details.Party];
return Ok(parties);
}
case AuthenticationInfo.ServiceOwner serviceOwner:
{
var details = await serviceOwner.LoadDetails();
IReadOnlyList<Party> parties = [details.Party];
return Ok(parties);
}
case AuthenticationInfo.SystemUser su:
{
var details = await su.LoadDetails();
IReadOnlyList<Party> parties = [details.Party];
return Ok(parties);
}
default:
throw new NotImplementedException();
}

return Ok(partyList);
}

/// <summary>
Expand Down
38 changes: 32 additions & 6 deletions src/Altinn.App.Core/Internal/Auth/IAuthenticationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Helpers;
using Altinn.App.Core.Internal.App;
using Altinn.App.Core.Internal.Profile;
using Altinn.App.Core.Internal.Registers;
using Altinn.App.Core.Models;
Expand Down Expand Up @@ -72,6 +74,7 @@ public sealed record User : AuthenticationInfo
private readonly Func<int, Task<Party?>> _lookupParty;
private readonly Func<int, Task<List<Party>?>> _getPartyList;
private readonly Func<int, int, Task<bool?>> _validateSelectedParty;
private readonly Func<Task<ApplicationMetadata>> _getApplicationMetadata;

internal User(
int userId,
Expand All @@ -82,7 +85,8 @@ internal User(
Func<int, Task<UserProfile?>> getUserProfile,
Func<int, Task<Party?>> lookupParty,
Func<int, Task<List<Party>?>> getPartyList,
Func<int, int, Task<bool?>> validateSelectedParty
Func<int, int, Task<bool?>> validateSelectedParty,
Func<Task<ApplicationMetadata>> getApplicationMetadata
)
: base(token)
{
Expand All @@ -94,6 +98,7 @@ internal User(
_lookupParty = lookupParty;
_getPartyList = getPartyList;
_validateSelectedParty = validateSelectedParty;
_getApplicationMetadata = getApplicationMetadata;
}

/// <summary>
Expand All @@ -103,12 +108,14 @@ internal User(
/// <param name="Profile">Users profile</param>
/// <param name="RepresentsSelf">True if the user represents itself</param>
/// <param name="Parties">List of parties the user can represent</param>
/// <param name="PartiesAllowedToInstantiate">List of parties the user can instantiate</param>
/// <param name="CanRepresent">True if the user can represent the selected party. Only set if details were loaded with validateSelectedParty set to true</param>
public sealed record Details(
Party Reportee,
UserProfile Profile,
bool RepresentsSelf,
IReadOnlyList<Party> Parties,
IReadOnlyList<Party> PartiesAllowedToInstantiate,
bool? CanRepresent = null
);

Expand Down Expand Up @@ -149,7 +156,20 @@ await _getUserProfile(UserId)
canRepresent = await _validateSelectedParty(UserId, PartyId);
}

_extra = new Details(reportee, userProfile, representsSelf, parties, canRepresent);
var application = await _getApplicationMetadata();
var partiesAllowedToInstantiate = InstantiationHelper.FilterPartiesByAllowedPartyTypes(
parties,
application.PartyTypesAllowed
);

_extra = new Details(
reportee,
userProfile,
representsSelf,
parties,
partiesAllowedToInstantiate,
canRepresent
);
return _extra;
}
}
Expand Down Expand Up @@ -319,7 +339,8 @@ internal static AuthenticationInfo From(
Func<int, Task<Party?>> lookupUserParty,
Func<string, Task<Party>> lookupOrgParty,
Func<int, Task<List<Party>?>> getPartyList,
Func<int, int, Task<bool?>> validateSelectedParty
Func<int, int, Task<bool?>> validateSelectedParty,
Func<Task<ApplicationMetadata>> getApplicationMetadata
)
{
string token = JwtTokenUtil.GetTokenFromContext(httpContext, authCookieName);
Expand Down Expand Up @@ -452,7 +473,8 @@ static void ParseAuthLevel(string? value, out int authLevel)
getUserProfile,
lookupUserParty,
getPartyList,
validateSelectedParty
validateSelectedParty,
getApplicationMetadata
);
}

Expand Down Expand Up @@ -491,14 +513,16 @@ internal sealed class AuthenticationContext : IAuthenticationContext
private readonly IProfileClient _profileClient;
private readonly IAltinnPartyClient _altinnPartyClient;
private readonly IAuthorizationClient _authorizationClient;
private readonly IAppMetadata _appMetadata;

public AuthenticationContext(
IHttpContextAccessor httpContextAccessor,
IOptionsMonitor<AppSettings> appSettings,
IOptionsMonitor<GeneralSettings> generalSettings,
IProfileClient profileClient,
IAltinnPartyClient altinnPartyClient,
IAuthorizationClient authorizationClient
IAuthorizationClient authorizationClient,
IAppMetadata appMetadata
)
{
_httpContextAccessor = httpContextAccessor;
Expand All @@ -507,6 +531,7 @@ IAuthorizationClient authorizationClient
_profileClient = profileClient;
_altinnPartyClient = altinnPartyClient;
_authorizationClient = authorizationClient;
_appMetadata = appMetadata;
}

// Currently we're coupling this to the HTTP context directly.
Expand All @@ -529,7 +554,8 @@ internal void ResolveCurrent()
_altinnPartyClient.GetParty,
(string orgNr) => _altinnPartyClient.LookupParty(new PartyLookup { OrgNo = orgNr }),
_authorizationClient.GetPartyList,
_authorizationClient.ValidateSelectedParty
_authorizationClient.ValidateSelectedParty,
_appMetadata.GetApplicationMetadata
);
httpContext.Items[ItemsKey] = authInfo;
}
Expand Down

0 comments on commit a4720f1

Please sign in to comment.