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

[4.x] Fix user groups/roles querying #6131

Merged
merged 27 commits into from Jan 23, 2024

Conversation

ryanmitchell
Copy link
Contributor

@ryanmitchell ryanmitchell commented Jun 1, 2022

This PR updates the state store paths for user groups to use -> rather than / which resolves the errors in #6124 and follows up on the comments in #2498.

It also updates the getKeysWithWhere method in the UserQueryBuilder to have logic for roles and groups so that we do a comparison on their handle, rather than the entire object (which was failing). Not sure how this fits in with Eloquent... I might need to update this logic or handle it a different way?

Fixes: #6124.
Fixes: #2498.
Fixes: #7578

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

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

Made a few change requests in individual comments. Happy for pushback.

Not sure how this fits in with Eloquent

This whole group query thing was clearly not very well thought out. 😞
Probably ideally we'd be able to treat it more like a relationship somehow. Or maybe do a similar thing to entries and the whereTaxonomy method. We could get the user ids in the appropriate groups then have a whereIn('id', all the ids from the groups)

src/Auth/File/UserGroup.php Outdated Show resolved Hide resolved
src/Stache/Indexes/Users/Group.php Outdated Show resolved Hide resolved
src/Stache/Query/UserQueryBuilder.php Outdated Show resolved Hide resolved
src/Stache/Stores/UsersStore.php Outdated Show resolved Hide resolved
@ryanmitchell
Copy link
Contributor Author

ryanmitchell commented Jun 2, 2022

Asides from the individual comment replies....

Bigger picture, while this works, the ->where('groups/{slug}') approach seems too stache-specific to me, I agree it would be better to try and aim for a whereIn('groups', [ids]), whereHas('groups', ...) or even a custom whereGroups() approach, so that its compatible with eloquent users. At the moment this tag wouldn't work at all with them (?).

It would need brought into the UserQueryBuilder so that it can be overridden by Eloquent.

Something similar also would need implemented for roles.

Do you have a preference?

[edit] A further question - should the User hasRole method not also be checking the user's groups for roles assigned?

@ryanmitchell
Copy link
Contributor Author

ryanmitchell commented Jan 19, 2023

@jasonvarga I've gone ahead and added whereRole, whereGroup etc to the query builder to be used for role and group querying.

It builds on the previous work so still uses the ->where('group/x') approach internally for file based users, but custom methods also then allow eloquent to use its own approach. You'll see I've added queries to the eloquent builder that should cover the common scenarios, but devs would be free to change these methods if they need something different.

@jmalko
Copy link

jmalko commented Mar 9, 2023

Please consider this PR - this documented functionality is broken in today's version of Statamic.

@edalzell edalzell changed the base branch from 3.3 to 4.x July 5, 2023 21:41
@edalzell edalzell changed the title Fix stache user groups/roles querying [4.x ] Fix stache user groups/roles querying Jul 5, 2023
@edalzell
Copy link
Contributor

edalzell commented Jul 5, 2023

Sorry, I retargeted onto 4.x, locally resolved the conflicts (perhaps incorrectly), but I'm getting an error now, Call to undefined method App\Models\User::roles().

CleanShot 2023-07-05 at 15 02 47@2x

Maybe the model is missing a relationship?

@edalzell edalzell marked this pull request as draft July 5, 2023 22:08
# Conflicts:
#	src/Query/Scopes/Filters/UserGroup.php
#	src/Query/Scopes/Filters/UserRole.php
#	src/Stache/Stores/UsersStore.php
@jasonvarga
Copy link
Member

@edalzell I resolved the merge and pushed up the change. I didn't verify that the PR works yet, though. Feel free to try it out now. Maybe your issue was an incorrect merge. This PR is over a year old (sorry 😬) so it might just need more tweaking.

@ryanmitchell
Copy link
Contributor Author

Ok @edalzell @jasonvarga I've updated this to not require a relation to be set. You'll see in the eloquent UserQueryBuilder it now checks if a relation exists, and if not it performs a whereExists on the role_user or group_user table.

This should cover off the default usage of people switching over from stache. If a dev wants to use a different approach for roles/groups (eg my PR) they would need to define the relationship on their user model.

@ryanmitchell ryanmitchell marked this pull request as ready for review July 10, 2023 14:55
src/Auth/Eloquent/UserQueryBuilder.php Outdated Show resolved Hide resolved
src/Auth/Eloquent/UserQueryBuilder.php Outdated Show resolved Hide resolved
@jasonvarga jasonvarga changed the title [4.x] Fix stache user groups/roles querying [4.x] Fix user groups/roles querying Jul 11, 2023
@jasperjorna
Copy link

This one is quite breaking at the moment when using Eloquent for users. Can we expect this to be included in a hotfix release?

@jasonvarga
Copy link
Member

It won't be a hotfix, it'll be a minor release.

@edalzell
Copy link
Contributor

This one is quite breaking at the moment when using Eloquent for users. Can we expect this to be included in a hotfix release?

In the meantime you can composer patch it in to start using it.

@jasonvarga jasonvarga merged commit cd26efd into statamic:4.x Jan 23, 2024
18 checks passed
@ryanmitchell ryanmitchell deleted the fix/stache-user-groups-querying branch January 23, 2024 20:09
@eiiot
Copy link

eiiot commented May 2, 2024

Anyone know when this will be released?

@edalzell
Copy link
Contributor

edalzell commented May 2, 2024

It was released a while back

@duncanmcclean
Copy link
Member

Anyone know when this will be released?

This was released as part of v4.46.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants