django / django/new-features

Add `superuser_required` decorator

Open
#47 6 comments 30 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

This is a minor enhancement to add `superuser_required` to `django.contrib.auth.decorators`. Since superuser (aka admin user) is core of Django I see it would be useful to have it as "battery included" in core of Django.

### Problem

That would resolve a problem that we create from project to project, that decorator which for my point of view, suits great at Django core (since we have a as a native `is_superuser` field on which rely `django.contrib.admin` and other custom parts of project which we develop.

### Request or proposal

proposal

### Additional Details

_No response_

### Implementation Suggestions

```
def superuser_required(
function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None
):
"""
Decorator for views that checks that the user is logged in and is a superuser, redirecting
to the log-in page if necessary.
"""
actual_decorator = user_passes_test(
lambda u: u.is_authenticated and u.is_superuser,
login_url=login_url,
redirect_field_name=redirect_field_name,
)
if function:
return actual_decorator(function)
return actual_decorator
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.