I had a similar problem where the user got redirected to the login page even though he was already logged in but just failed the permission check. I solved this in the following way.
I override the wagtail check_view_restrictions in my own wagtail_hooks.py file.
I simply added another elif statement to check if the user is authenticated and the page in question has group permissions and then redirect to my dashboard page where I handle the popup modal logic in my DashboardPage's serve method.
...
elif request.user.is_authenticated and restriction.restriction_type == PageViewRestriction.GROUPS:
return redirect(f'/dashboard/?next={request.get_full_path()}')
...
full function:
from django.conf import settings
from django.urls import reverse
from django.contrib.auth.views import redirect_to_login
from django.shortcuts import redirect
from wagtail.core.models import PageViewRestriction
hooks.register('before_serve_page')
def check_view_restrictions(page, request, serve_args, serve_kwargs):
"""
Overriding wagtail check_view_restrictions to redirect a user to the dashboard
when they are logged in and the page has groups restriction_type.
The dashboard serve view is handling the permission denied modal
"""
for restriction in page.get_view_restrictions():
if not restriction.accept_request(request):
if restriction.restriction_type == PageViewRestriction.PASSWORD:
from wagtail.core.forms import PasswordViewRestrictionForm
form = PasswordViewRestrictionForm(instance=restriction,
initial={'return_url': request.get_full_path()})
action_url = reverse('wagtailcore_authenticate_with_password', args=[
restriction.id, page.id])
return page.serve_password_required_response(request, form, action_url)
elif request.user.is_authenticated and restriction.restriction_type == PageViewRestriction.GROUPS:
return redirect(f'/dashboard/?next={request.get_full_path()}')
elif restriction.restriction_type == PageViewRestriction.LOGIN:
return require_wagtail_login(next=request.get_full_path())
I use the next parameter in the URL in the serve method to check if the user has the correct permissions to view that page. You can add your own required logic here