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

fix(auth): hasRole handles when currentUser.roles is a string #4678

Merged

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented Mar 7, 2022

We had an edge case where we had missing logic in hasRoles.

The updated code now handles the case where:

  • currentUser.roles is a string
  • but hasRoles is called with an array of strings

@dac09 dac09 requested a review from Tobbe March 7, 2022 09:33
@dac09
Copy link
Collaborator Author

dac09 commented Mar 7, 2022

@Tobbe mind taking a look please? This is something Rob and I already fixed on the dbAuth template

@dac09
Copy link
Collaborator Author

dac09 commented Mar 7, 2022

Related #4489

@dthyresson
Copy link
Contributor

@dac09 Does this fix #4488 or #4489

I am almost done with #4492 so the directive can take a string or an array of string roles.

One changge I was making to auth was from:

export const hasRole = ({ roles }: { roles: AllowedRoles }): boolean => {

to

export const hasRole = ( roles : AllowedRoles ): boolean => {

so one could use hasRole('bazinga') instead of hasRole(roles: 'bazinga') per @cannikin

Copy link
Collaborator Author

dac09 commented Mar 7, 2022

It doesn't fix those issues DT (which is why I haven't linked them) - this fixes a different edge case we don't handle, as explained in the description

@dthyresson
Copy link
Contributor

Ok just checking. Thanks,

packages/auth/src/__tests__/AuthProvider.test.tsx Outdated Show resolved Hide resolved
if (typeof role === 'string') {
return this.state.currentUser.roles?.includes(role) || false
if (typeof roleToCheck === 'string') {
return this.state.currentUser.roles?.includes(roleToCheck) || false
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need an isArray check on currentUser.roles here as well? As you added 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.

Ummm in theory, if you passed a function or something, but we assume that you will pass either a string or an array, and don't do exhaustive checks for that. Think thats ok!

Copy link
Member

Choose a reason for hiding this comment

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

What I'm saying is if roleToCheck is a string, and this.state.currentUser.roles is a string we should not do includes(roleToCheck) but rather use ===.

Copy link
Member

Choose a reason for hiding this comment

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

Unless there's something I don't understand about the code here....

Copy link
Member

Choose a reason for hiding this comment

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

That's true, if you had admin_user and user, it'd return true in both cases if you checked for the role user with includes() 😬

Copy link
Member

Choose a reason for hiding this comment

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

So with the current setup there's 4 cases it needs to handle:

  1. currentUser.roles is a string, roleToCheck is a string
  2. currentUser.roles is an array, roleToCheck is a string
  3. currentUser.roles is a string, roleToCheck is an array
  4. currentUser.roles is an array, roleToCheck is an array

I feel like we're only covering 3 of these in here aren't we?

Copy link
Member

@cannikin cannikin Mar 7, 2022

Choose a reason for hiding this comment

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

That check on lines 195-196 would cause the partial string problem if currentUser.roles is itself a string, but if it changes to === then it'll fail if currentUser.roles is an array.

Copy link
Member

Choose a reason for hiding this comment

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

Exactly, we have 4 cases it needs to handle, but only three branches in the code. I'd add another one to (explicitly) handle the missing case too

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok bit late for me to play with this logic right now, but will look at it again in the morning :)

Once I have it I'll update the hasRole functions in all the auth templates!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cannikin, @Tobbe how about this one?

Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com>
@dac09 dac09 mentioned this pull request Mar 8, 2022
3 tasks
@dac09
Copy link
Collaborator Author

dac09 commented Mar 9, 2022

Ok gents, I've updated all the templates!

.replace('select: { id: true }', 'select: { id: true, roles: true }')
.replace(
'const currentUserRoles = context.currentUser?.roles',
'const currentUserRoles = context.currentUser?.roles as string | string[]'
Copy link
Collaborator Author

@dac09 dac09 Mar 9, 2022

Choose a reason for hiding this comment

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

This is to prevent typescript complaining on the smoke tests. TS knows from Primsa types that currentUser.roles can never be a string[] in the test project, so part of the code gets marked as an error, causing smoke tests with tsc to fail.

A TS error in a user's project is the desired effect, but not in the test project.

Copy link
Collaborator Author

dac09 commented Mar 10, 2022

If you look at the auth file, it's littered with long comments everywhere. I will change it to roles, if you really want, because I don't think its worth either our time debating this, and it adds 0-0.1% more value.

…:dac09/redwood into fix/has-roles-when-currentUser-is-string

* 'fix/has-roles-when-currentUser-is-string' of github.com:dac09/redwood:
  Fix react/prop-types lint warnings (redwoodjs#4674)
  Allow the number 0 for numericality validation values (redwoodjs#4700)
@dac09
Copy link
Collaborator Author

dac09 commented Mar 10, 2022

I'll wait for @cannikin to do a once over, and then merge!

@thedavidprice
Copy link
Contributor

Merging this to continue work on #4673

Thanks, all!

@thedavidprice thedavidprice merged commit ead826d into redwoodjs:main Mar 10, 2022
@jtoar jtoar added this to the next-release milestone Mar 10, 2022
dac09 added a commit to dac09/redwood that referenced this pull request Mar 11, 2022
…d into feat/auth-checks-smoke-test

* 'feat/auth-checks-smoke-test' of github.com:dac09/redwood: (21 commits)
  Remove supertokens-node from packages/api dependencies (redwoodjs#4715)
  fix(auth): hasRole handles when currentUser.roles is a string (redwoodjs#4678)
  Update dependency systeminformation to v5.11.7 (redwoodjs#4716)
  Update dependency webpack-manifest-plugin to v5 (redwoodjs#4693)
  Update graphqlcodegenerator monorepo (redwoodjs#4714)
  Update dependency @clerk/types to v1.28.3 (redwoodjs#4708)
  Update dependency @testing-library/react to v12.1.4 (redwoodjs#4709)
  Update dependency pino to v7.8.1 (redwoodjs#4703)
  Update dependency fastify to v3.27.4 (redwoodjs#4702)
  Update dependency @clerk/clerk-sdk-node to v2.9.8 (redwoodjs#4707)
  Update dependency @types/react to v17.0.40 (redwoodjs#4711)
  Update dependency @clerk/clerk-js to v2.17.3 (redwoodjs#4706)
  Fix react/prop-types lint warnings (redwoodjs#4674)
  Allow the number 0 for numericality validation values (redwoodjs#4700)
  update yarn.lock
  v0.49.1
  update yarn.lock
  remove storybook type check (redwoodjs#4699)
  add bin proxy for rw-log-formatter to core (redwoodjs#4695)
  remove storybook type check (redwoodjs#4699)
  ...
@thedavidprice thedavidprice modified the milestones: next-release, v0.50.0 Mar 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix
Projects
No open projects
Status: Archived
Development

Successfully merging this pull request may close these issues.

None yet

6 participants