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 bug when adding roles to a model that doesn't yet exist #1663

Merged
merged 7 commits into from Jan 27, 2021
Merged

Fix bug when adding roles to a model that doesn't yet exist #1663

merged 7 commits into from Jan 27, 2021

Conversation

duncanmcclean
Copy link
Contributor

Brief description of the bug
In our app, we have Nova setup so the client can create new users and assign them different roles within the application. We're using this package to bring the Role adding, etc functionality to Nova.

However, whenever a new user was created and roles were assigned to that user, it seemed to override the roles of other users as well.

Debugging
I spent an hour or so trying to get to the bottom of this. I did some debugging in the Nova package itself but couldn't find anything, then I started looking at this package.

When dd'ing (sorry not Ray just yet), that $object was an instance of one of the other models (a different user), whereas $model was the instance of the model I was creating in Nova.

I changed around references of $object to $model and hey-presto, it started to function as expected (eg. the roles for the new user were only assigned to the new user, not anyone else).


Let me know if you have any questions or if you'd like me to make any changes.

@drbyte
Copy link
Collaborator

drbyte commented Jan 13, 2021

@damcclean Thanks for this.

What are your thoughts on updating the test suite to match?
And perhaps we should apply the same change for permissions as well?

@duncanmcclean
Copy link
Contributor Author

Hi,

I'm happy to update the tests. Which ones do you think would need updating?

Sure, I'll add the same to Permissions as well.

Hopefully I'll be able to look at it in the next couple of days.

@drbyte
Copy link
Collaborator

drbyte commented Jan 13, 2021

I wonder if these tests are incomplete, given that they didn't catch the situation you're reporting:

it_will_sync_roles_to_a_model_that_is_not_persisted()
calling_syncRoles_before_saving_object_doesnt_interfere_with_other_objects()
calling_assignRole_before_saving_object_doesnt_interfere_with_other_objects()
.. and their permission equivalents

@duncanmcclean
Copy link
Contributor Author

@drbyte I've applied the same fix to the HasPermissions trait and I've updated the various tests to hopefully cover the issue.

@drbyte
Copy link
Collaborator

drbyte commented Jan 21, 2021

@damcclean Thanks.

In looking at the tests, I'm not certain these test changes are covering the situation you're trying to fix. Ideally we want the test to fail if the changes to the trait aren't made.

@drbyte drbyte added the bug label Jan 21, 2021
By avoiding the temporary/static variable here, and using $model directly, the detection of persisted object is more accurate.
By avoiding the temporary/static variable here, and using $model directly, the detection of persisted object is more accurate.
@drbyte
Copy link
Collaborator

drbyte commented Jan 21, 2021

@damcclean I've made a couple updates to your PR which I think solve this. By skipping the temporary static variable I believe the purpose it was attempting to solve is handled even better by swapping $model for $object in the remaining lines.

Can you test your app with this change?

@duncanmcclean
Copy link
Contributor Author

Hi there,

Sorry for taking a long time to get back to you - I only work on this project once a week so I was trying to fit in changes when I'm on the project 🙂

We've just updated the vendor files on the project and we're able to verify the issue is resolved by them.

@drbyte drbyte merged commit 63047ef into spatie:master Jan 27, 2021
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.

None yet

2 participants