-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Endpoint for fetching current party roles #983
base: main
Are you sure you want to change the base?
Changes from all commits
917989d
fbef215
1ca6d9a
c6780df
944a197
bfed4bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
using Altinn.Platform.Register.Models; | ||
using Altinn.Platform.Storage.Interface.Models; | ||
using AltinnCore.Authentication.Utils; | ||
using Authorization.Platform.Authorization.Models; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
@@ -179,4 +180,52 @@ | |
} | ||
return MultiDecisionHelper.ValidatePdpMultiDecision(actionsResult, response.Response, user); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves roles for a user on a specified party. | ||
/// </summary> | ||
/// <param name="userId">The user id.</param> | ||
/// <param name="userPartyId">The user party id.</param> | ||
/// <returns>A list of roles for the user on the specified party.</returns> | ||
public async Task<List<Role>> GetUserRolesAsync(int userId, int userPartyId) | ||
{ | ||
List<Role> roles = new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually we wrap the body in activity (distributed trace spans). You can probably find example usages of |
||
string apiUrl = $"roles?coveredByUserId={userId}&offeredByPartyId={userPartyId}"; | ||
string token = JwtTokenUtil.GetTokenFromContext(_httpContextAccessor.HttpContext, _settings.RuntimeCookieName); | ||
|
||
try | ||
{ | ||
HttpResponseMessage response = await _client.GetAsync(token, apiUrl); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
string responseContent = await response.Content.ReadAsStringAsync(); | ||
var deserialized = JsonConvert.DeserializeObject<List<Role>>(responseContent); | ||
if (deserialized is not null) | ||
{ | ||
roles = deserialized; | ||
} | ||
} | ||
else | ||
{ | ||
_logger.LogError( | ||
"Failed to retrieve roles for userId {UserId} and partyId {PartyId}. StatusCode: {StatusCode}", | ||
userId, | ||
userPartyId, | ||
response.StatusCode | ||
); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError( | ||
ex, | ||
"An error occurred while retrieving roles for userId {UserId} and partyId {PartyId}", | ||
userId, | ||
userPartyId | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about the error handling here. If the API fails for some reason we return an empty list, which might make it look like the user has no roles when in fact the problem was that we couldn't find out at all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. |
||
} | ||
Comment on lines
+218
to
+226
Check notice Code scanning / CodeQL Generic catch clause Note
Generic catch clause.
|
||
|
||
return roles; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should return
IEnumerable<>
orIReadOnlyList<>
instead, to express the intended usage