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

Create FetchUserRolesAndGroupsService #392

Merged
merged 28 commits into from
Sep 4, 2023
Merged

Create FetchUserRolesAndGroupsService #392

merged 28 commits into from
Sep 4, 2023

Conversation

StuAA78
Copy link
Contributor

@StuAA78 StuAA78 commented Aug 30, 2023

https://eaflood.atlassian.net/browse/WATER-4085

We create a service FetchUserRolesAndGroupsService to look up a user in the idm schema and return the roles and groups assigned to them.

Roles are assigned as follows:

  • Users can have roles assigned to them in the user_roles table
  • Users can have groups assigned to them in the user_groups table
    • Groups have one or more roles assigned to them in the group_roles table

We want to collate all roles, no matter how they're assigned, and return the resulting list, along with the groups the user belongs to.

https://eaflood.atlassian.net/browse/WATER-4085

We create a service to look up a user in the `idm` schema and return the roles assigned to them.
@StuAA78 StuAA78 added the enhancement New feature or request label Aug 30, 2023
@StuAA78 StuAA78 self-assigned this Aug 30, 2023
StuAA78 added 21 commits August 30, 2023 11:32
Our first pass at a solution is to make multiple calls to the db. This lets us know we're on the right track and can start refactoring and optimising the service
We overlooked adding the `idm` model folder to the `modelPaths` of our `LegacyBaseModel`. Fixing this means we are able to use the models correctly.
We want to replace our naive approach to retrieving the roles with something a little nicer. In order to do this, we need to add relations to the models so we can easily go from one to another. Note that for now we only add "forward" relations, eg. from user to user role to role. We don't yet add ones going the other way, eg. from role to user role to user. We also have not yet written unit tests for these.
We add a unit test case to ensure that if a role is added directly to the user, and the user also has that role via a group, then the role is only returned once from the service
Now that our models have relations going from one table to another, we can replace our multiple queries with a single query which fetches all roles and groups belonging to a user
We provide the service with the user id so there's no need for it to return it
We initially implemented only the minimum relations required to join the tables "forwards" so we can retrieve groups and roles from a user. We now go back and implement the relations going the other way.
We rename the service to reflect the fact it returns groups as well as roles
If the user id is not found then we ensure that we return empty arrays for `roles` and `groups`
@StuAA78 StuAA78 changed the title Create GetUserRolesService Create GetUserRolesAndGroupsService Sep 4, 2023
@StuAA78 StuAA78 marked this pull request as ready for review September 4, 2023 13:07
We rename the service to `FetchUserRolesAndGroupsService` to better reflect our standard of "fetching" things from the db
We add an additional group and roles to the unit tests which we don't assign to the user, to confirm that they are not returned and therefore we are only returning the relevant groups and roles
Copy link
Member

@Cruikshanks Cruikshanks left a comment

Choose a reason for hiding this comment

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

Just copy & paste typo. Other than that all gravy!

test/models/idm/group.model.test.js Outdated Show resolved Hide resolved
Copy link
Member

@Cruikshanks Cruikshanks left a comment

Choose a reason for hiding this comment

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

@StuAA78 StuAA78 changed the title Create GetUserRolesAndGroupsService Create FetchUserRolesAndGroupsService Sep 4, 2023
@StuAA78 StuAA78 merged commit 6698bb2 into main Sep 4, 2023
@StuAA78 StuAA78 deleted the get-user-roles-service branch September 4, 2023 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants