Better handling of group based view restrictions #13177
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related issue
Fixes #4297: "Private pages with group restrictions result in infinite loop between login url and target page"
Rationale
Currently, an authenticated user failing to pass a page (or document) view restriction based on their Group is redirected to the frontend login page. As documented in #4297, this can cause an infinite redirect loop. The expected behaviour would be to raise a
PermissionDenied
(403) exception.However, just presenting an error screen is not always ideal - a better scenario might be to explain to users why they cannot access certain content (ie. "You must be a Stonecutter to access this page") - and so this PR also introduces a mechanism by which developers can specify a handler to deal with those requests.
Changes
WAGTAIL_UNAUTHENTICATED_GROUP_HANDLER
andWAGTAILDOCS_UNAUTHENTICATED_GROUP_HANDLER
as new settingswagtail.wagtail_hooks.check_view_restrictions
andwagtail.documents.wagtail_hooks.check_view_restrictions
to check for existence of above settings pointing to handler methods, and falls back to raisingdjango.core.exceptions.PermissionDenied
for unauthorised usersQuestions
check_view_restrictions
checks for the existence of the new settings, and falls back to aPermissionDenied
if they are not set. Does it make more sense for there to be default handler methods forWAGTAIL_UNAUTHENTICATED_GROUP_HANDLER
andWAGTAILDOCS_UNAUTHENTICATED_GROUP_HANDLER
in the Wagtail core (returning aPermissionDenied
by default)?WAGTAILDOCS_UNAUTHENTICATED_GROUP_HANDLER
)? If both hooks callWAGTAIL_UNAUTHENTICATED_GROUP_HANDLER
, users could just be advised to provision for their handler to accept either aPage
orDocument
as the first argument.message
to the request, for example. I guess this then raises the extra question of whether we should alllow for custom handlers forRestriction.LOGIN
too...Notes
The Docs CI seems to be failing based on an unrelated change in the 7.1 release notes:Merged with upstream, so docs are now compiling 🎉