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

Updated documentation for adding reports to cover permission restrict… #9859

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion docs/extending/adding_reports.md
Expand Up @@ -178,15 +178,28 @@ def register_unpublished_changes_report_url():

Here, we use the `AdminOnlyMenuItem` class to ensure our report icon is only shown to superusers. To make the report visible to all users, you could replace this with `MenuItem`.

## Setting up permission restriction

Even with the menu item hidden, it would still be possible for any user to visit the report's URL directly, and so it is necessary to set up a permission restriction on the report view itself. This can be done by adding a `dispatch` method to the existing `UnpublishedChangesReportView` view:

```python

# add the below dispatch method to the existing UnpublishedChangesReportView view
def dispatch(self, request, *args, **kwargs):
if not self.request.user.is_superuser:
return permission_denied(request)
return super().dispatch(request, *args, **kwargs)
```

## The full code

```python
# <project>/views.py

from wagtail.admin.auth import permission_denied
from wagtail.admin.views.reports import PageReportView
from wagtail.models import Page


class UnpublishedChangesReportView(PageReportView):

header_icon = 'doc-empty-inverse'
Expand All @@ -198,6 +211,11 @@ class UnpublishedChangesReportView(PageReportView):

def get_queryset(self):
return Page.objects.filter(has_unpublished_changes=True)

def dispatch(self, request, *args, **kwargs):
if not self.request.user.is_superuser:
return permission_denied(request)
return super().dispatch(request, *args, **kwargs)
```

```python
Expand Down