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

[6.x] Re-work super user authorization check #11516

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

duncanmcclean
Copy link
Member

@duncanmcclean duncanmcclean commented Feb 28, 2025

This pull request attempts to fix an issue with how super users are authorized in Statamic.

Currently, when an authorization check is performed against a super user, authorization will be granted. This is obviously far from ideal, especially when you're integrating Statamic as part of an existing Laravel application.

This PR aims to fix this issue by only granting authorization when we know the ability/permission "belongs" to Statamic.

Closes #8337.
Closes #10832.

Otherwise, when the `Authorize` middleware checks if the user has access to the CP, `Permission:all()` in the `Gate::before()` closure won't return any permissions as they haven't been booted yet.
Until I figure out a better solution. 🤔
Since there's no `DroidsClass` policy, I've updated this test to authorize using a permission instead, which'll work.
@@ -84,7 +85,13 @@ public function boot()
});

Gate::before(function ($user, $ability) {
return optional(User::fromUser($user))->isSuper() ? true : null;
Permission::boot();
Copy link
Member Author

@duncanmcclean duncanmcclean Feb 28, 2025

Choose a reason for hiding this comment

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

While I think this approach is broadly on the right track, I don't really want to be booting permissions here. Although, I'm not sure what the best alternative is... 🤔

  • Without booting permissions here, users can't access the CP because the Authorize middleware checks if the user has the relevant permissions, however it happens before the BootPermissions middleware is called.
  • We can't reorder the middleware, otherwise it might cause issues with translations in permission labels, which Ability to defer permission and utility registration for translations #7343 solved.

Copy link
Member

Choose a reason for hiding this comment

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

What's wrong with booting in there? It only happens when the closure is executed.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I was just thinking it was weird since we're already booting them in a middleware, and that trying to boot them multiple times would hurt performance?

Maybe I'm wrong though, and it's fine?

Copy link
Member

Choose a reason for hiding this comment

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

We could make Permissions::boot() return early and do nothing if it's already been booted once.

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.

2 participants