Skip to content
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

PIMS-1718: Keycloak not updating role #2434

Merged
merged 6 commits into from
Jun 5, 2024
Merged

PIMS-1718: Keycloak not updating role #2434

merged 6 commits into from
Jun 5, 2024

Conversation

LawrenceLau2020
Copy link
Collaborator

@LawrenceLau2020 LawrenceLau2020 commented Jun 4, 2024

🎯 Summary

PIMS-1718:

When changing the user’s role in the frontend, the role in the Keycloak SSO was not reflecting the change to the role in the database.
Updating the keycloak and user related services to allow for updating the user's role in the Keycloak SSO app.

This may need more work to handle errors when trying to save changes to users who don't exist in the Keycloak instance which will trigger this error message:
Submission failed with code 500. Message: Error: keycloakService.updateKeycloakUserRoles: keycloakService.getKeycloakUserRoles: undefined

🔰 Checklist

  • I have read and agree with the following checklist and am following the guidelines in our Code of Conduct document.
  • I have performed a self-review of my code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation where required.
  • I have tested my changes to the best of my ability.
  • My changes generate no new warnings.

Copy link

github-actions bot commented Jun 4, 2024

🚀 Deployment Information

The Express API Image has been built with the tag: 2434. Please make sure to utilize this specific tag when promoting these changes to the TEST and PROD environments during the API deployment. For more updates please monitor Image Tags Page on Wiki.

Copy link

codeclimate bot commented Jun 4, 2024

Code Climate has analyzed commit 5c48d2f and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 94.4%.

View more on Code Climate.

@@ -272,6 +273,7 @@ const updateUser = async (user: DeepPartial<User>) => {
...user,
DisplayName: `${user.LastName}, ${user.FirstName}`,
});
await KeycloakService.updateKeycloakUserRoles(resource.Username, [roleName]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check if a role was even included. If this is called with any area other than the status update section, the role is not included, and that user's roles are removed from Keycloak (but not PIMS).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Comment on lines 269 to 278
// Ensure existingRolesResponse is an array of roles
let existingRoles: IKeycloakRole[] = [];
if (Array.isArray(existingRolesResponse)) {
existingRoles = existingRolesResponse;
} else if (existingRolesResponse && typeof existingRolesResponse === 'object') {
const response = existingRolesResponse as { data?: IKeycloakRole[] }; // Type assertion
existingRoles = response.data || [];
} else {
throw new Error('Unexpected structure of existingRolesResponse');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could avoid all of these checks if we just wrapped most of this function in a try/catch block and handled the case if the getKeycloakUserRoles threw returned an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we also wouldn't have to change the loops below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made changes, ready for another review.

Copy link
Collaborator

@dbarkowsky dbarkowsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working well for me. Tried experimenting with not waiting for the Keycloak update to finish in an attempt to make it snappier. Unfortunately, we can't throw an error if we do that. Seemed more important to keep the error message visible.
Edited the error message thrown when a Keycloak user isn't found. Hopefully it's clearer now for frontend users.

@LawrenceLau2020 LawrenceLau2020 merged commit 6ee7476 into main Jun 5, 2024
7 checks passed
@LawrenceLau2020 LawrenceLau2020 deleted the PIMS-1718 branch June 5, 2024 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants