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

[Debt] Update how roles are set as part of communities changes #10791

Merged
merged 35 commits into from
Jul 18, 2024

Conversation

vd1992
Copy link
Contributor

@vd1992 vd1992 commented Jun 25, 2024

🤖 Resolves #10351

👋 Introduction

Updates the shape of the update roles mutation, gets rid of syncing for roles, deprecates the other update roles mutation, and updates policy to account for new roles in the mutation.
Some TODOs have been identified for later

🧪 Testing

  1. Exercise updating roles for users in the frontend

🚚 Deployment

Add any additional details that are required for deploying the application.

Examples of when this is required include:

  • re-running database seeders
  • environment variable changes

Notes

  • Remove deployment section if no steps are needed
  • Add deployment label to the linked issue if deployment steps are needed

@vd1992 vd1992 marked this pull request as draft June 25, 2024 23:35
@codecov-commenter
Copy link

codecov-commenter commented Jun 25, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 56.48855% with 57 lines in your changes missing coverage. Please review.

Project coverage is 38.02%. Comparing base (92862dd) to head (9edb149).

Files Patch % Lines
api/app/Policies/UserPolicy.php 67.27% 18 Missing ⚠️
...eamMembersPage/components/EditTeamMemberDialog.tsx 0.00% 11 Missing ⚠️
...s/UpdateUserPage/components/EditTeamRoleDialog.tsx 0.00% 11 Missing ⚠️
...rs/UpdateUserPage/components/AddTeamRoleDialog.tsx 0.00% 5 Missing ⚠️
api/app/Models/Community.php 0.00% 2 Missing ⚠️
api/app/Models/Pool.php 0.00% 2 Missing ⚠️
...TeamMembersPage/components/AddTeamMemberDialog.tsx 0.00% 2 Missing ⚠️
...mMembersPage/components/RemoveTeamMemberDialog.tsx 0.00% 2 Missing ⚠️
...ateUserPage/components/AddIndividualRoleDialog.tsx 0.00% 2 Missing ⚠️
...UpdateUserPage/components/RemoveTeamRoleDialog.tsx 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main   #10791      +/-   ##
============================================
- Coverage     38.05%   38.02%   -0.04%     
- Complexity     1751     1792      +41     
============================================
  Files          1060     1061       +1     
  Lines         30913    31006      +93     
  Branches       6574     6568       -6     
============================================
+ Hits          11764    11790      +26     
- Misses        18974    19179     +205     
+ Partials        175       37     -138     
Flag Coverage Δ
integrationtests 66.01% <77.08%> (-0.26%) ⬇️
unittests 31.33% <0.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vd1992
Copy link
Contributor Author

vd1992 commented Jun 26, 2024

Okay @tristan-orourke

I did the suggested sync removal in 7e2d841 and the replace/deprecate in 7c660bb
Then set up the permission asserting in the remaining commits

Thoughts on things so far? Right direction?

Copy link
Member

@tristan-orourke tristan-orourke left a comment

Choose a reason for hiding this comment

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

Great job on the frontend, I like how you got replaced sync with attach & detach.

The downside is that the UserPolicy->updateRoles method is real ugly, now. 😆 And I expected that, but now that I see it I have a suggestion. What if we create a mutation class to handle updateUserRoles, and move some of the logic (especially for processing the input) from the Policy to there? The goal would be to end up with a policy method (or possibly break it down into several) which is straightforward and easy to read.

@vd1992
Copy link
Contributor Author

vd1992 commented Jun 27, 2024

The downside is that the UserPolicy->updateRoles method is real ugly, now. 😆 And I expected that, but now that I see it I have a suggestion. What if we create a mutation class to handle updateUserRoles, and move some of the logic (especially for processing the input) from the Policy to there? The goal would be to end up with a policy method (or possibly break it down into several) which is straightforward and easy to read.

I'm not sure I follow. The policy check precedes the mutation, so I don't see how you can shift the logic into the latter.

The operation itself is complex given it involves attach and detach, team-based roles. non-team roles, and all the possible roles and permissions present. I don't know how we can circumvent a complex policy check given it is a complex authorization process short of refactoring the role mutations in the front and back into separate granular mutations for each single role you can add/remove.

Copy link
Member

@tristan-orourke tristan-orourke left a comment

Choose a reason for hiding this comment

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

Okay, thanks for helping me think through this earlier.

In addition to the other comments, please update the HasRoleAssignments interface:

interface HasRoleAssignments {
  roleAssignments: [RoleAssignment!]
  teamId: ID! # Used to assign roles associated with this resource using the updateUserRoles mutation.
}

The Team, Pool and Community models will need a teamId accessor added, to return the team id.

api/graphql/schema.graphql Show resolved Hide resolved
}

// adding or removing team based roles
if (isset($attachRoles['team']) || isset($detachRoles['team'])) {
Copy link
Member

Choose a reason for hiding this comment

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

I take back my previous comment about decoupling this policy from the schema. However, if we follow my suggested change to the schema, then it probably makes more sense to simply loop through all the attach roles then all the detach roles, instead of dealing with team roles first then non-team roles.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusting the policy to fit the new mutation input type simplifies the policy file a fair bit
4980080

api/app/Policies/UserPolicy.php Outdated Show resolved Hide resolved
api/app/Policies/UserPolicy.php Outdated Show resolved Hide resolved
@@ -1795,7 +1794,7 @@ type Mutation {
updateUserRolesInput: UpdateUserRolesInput! @spread
): UserAuthInfo
@update(model: "User")
Copy link
Member

Choose a reason for hiding this comment

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

If we change the UpdateUserRolesInput, we'll need to remove the @update directive which lets Lighthouse magically handle the update, and do it in a mutation class.

@@ -1795,7 +1794,7 @@ type Mutation {
updateUserRolesInput: UpdateUserRolesInput! @spread
): UserAuthInfo
@update(model: "User")
@canModel(ability: "updateRoles", model: "User")
Copy link
Member

Choose a reason for hiding this comment

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

We should also add a @guard directive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure b909155

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 5, 2024

In addition to the other comments, please update the HasRoleAssignments interface:

interface HasRoleAssignments {
  roleAssignments: [RoleAssignment!]
  teamId: ID! # Used to assign roles associated with this resource using the updateUserRoles mutation.
}

The Team, Pool and Community models will need a teamId accessor added, to return the team id.

Sure 3af0b17

EDIT
Okay, had to make nullable to avoid type errors in d7214cf
Specifically here

image

image

Due to

image

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 5, 2024

Overall thoughts now?

Copy link
Member

@tristan-orourke tristan-orourke left a comment

Choose a reason for hiding this comment

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

Great work! You covered everything I mentioned, with some additional nice touches like making RoleTeamConsistent a rule. I just need to compile and test manually now.

api/tests/Feature/UserRoleTest.php Outdated Show resolved Hide resolved
@brindasasi
Copy link
Contributor

I'm getting below error in http://localhost:8000/graphiql and couldn't run any mutation. I took the latest of the branch
Screenshot 2024-07-15 at 4 13 42 PM

@brindasasi brindasasi self-requested a review July 15, 2024 20:44
Copy link
Contributor

@brindasasi brindasasi left a comment

Choose a reason for hiding this comment

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

http://localhost:8000/graphiql interface throws validation error and couldn't verify

Its possible to use a mutation to assign new Community and Pool based rolesand couldn't exercise the new roles via mutation.
Everything else looks good 👍

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 15, 2024

http://localhost:8000/graphiql interface throws validation error and couldn't verify

Its possible to use a mutation to assign new Community and Pool based rolesand couldn't exercise the new roles via mutation. Everything else looks good 👍

Okay I think I just needed to do this to fix it ? f808353

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 15, 2024

Okay, using teamId for the interface was breaking things, it appeared records with a teamId and legacy team relation were getting mixed up by Laravel/Lighthouse. I suppose the legacyTeam relation acting through team_id and then adding a field called teamId messes things up understandably

Renamed to a hopefully temporary name that can be changed afterwards in e4beaf0

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 15, 2024

You can test with the newer roles by removing the filtering in the frontend. There are a few places to remove the filter from then you can give them to people. I found it handy to test due to weird permission caching mysteries in graphiql

Example below

image

@vd1992 vd1992 requested a review from brindasasi July 16, 2024 00:05
@tristan-orourke
Copy link
Member

Good catches! I think it should be possible to make it work without renaming teamId... I'll try playing with it a bit.

@brindasasi
Copy link
Contributor

I only see this problem with this user.
Steps to reproduce:

  1. Logged in as platform admin
  2. I took the No roles user and assigned him into multiple pools
    Screenshot 2024-07-16 at 10 53 32 AM
  3. Tried to view that user via View User page and got this error

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 16, 2024

I only see this problem with this user. Steps to reproduce:

1. Logged in as platform admin

2. I took the `No roles` user and assigned him into multiple pools

Can't seem to replicate 😕

image

image

image

@brindasasi
Copy link
Contributor

brindasasi commented Jul 16, 2024

So tried different browser firefox. Still the same.
Screenshot 2024-07-16 at 3 31 40 PM

Tried few diff things & results are :

  1. Deleted all pools from the above user. -> Result : Page loads fine.
  2. Added one pool Ex-03 Complex -> Result : Page loads fine.
  3. Added another pool IT-5 Simple closed pool -> Result : Page gets error
  4. Seeded fresh and repeated the same steps -> Result : Page gets error
    @GCTC-NTGC/developers can someone else try in your local?

@tristan-orourke
Copy link
Member

@vd1992, I didn't have a chance to look at teamId, but since it doesn't work, can I at least recommend a different name? I'm thinking teamIdForRoleAssignment, since that purpose is the only reason we're exposing it.

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 16, 2024

@vd1992, I didn't have a chance to look at teamId, but since it doesn't work, can I at least recommend a different name? I'm thinking teamIdForRoleAssignment, since that purpose is the only reason we're exposing it.

Okay, if you dislike the other name a bunch 😛
Changed in 0f8c697

Copy link
Member

@tristan-orourke tristan-orourke left a comment

Choose a reason for hiding this comment

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

I've now been able to test it manually, and it all seems fine to me. I haven't reproduced any issues with [email protected] user.

@brindasasi
Copy link
Contributor

I've now been able to test it manually, and it all seems fine to me. I haven't reproduced any issues with [email protected] user.

Should be good to merge then. I'll keep an eye on UAT on this.

@vd1992
Copy link
Contributor Author

vd1992 commented Jul 17, 2024

I've now been able to test it manually, and it all seems fine to me. I haven't reproduced any issues with [email protected] user.

Should be good to merge then. I'll keep an eye on UAT on this.

Approval please

Copy link
Contributor

@brindasasi brindasasi left a comment

Choose a reason for hiding this comment

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

Good to go!

@vd1992 vd1992 added this pull request to the merge queue Jul 18, 2024
Merged via the queue into main with commit a004dca Jul 18, 2024
11 of 12 checks passed
@vd1992 vd1992 deleted the 10351-updateUserRoles-changes branch July 18, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

♻️ Update the updateUserRoles mutation
4 participants