Skip to content

Add superuser_required decorator #47

@DmytroLitvinov

Description

@DmytroLitvinov

Code of Conduct

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions