[Django]-Restricting whole django app to the normal users

1๐Ÿ‘

โœ…

You can write a custom middleware to achieve this.

from django.urls import reverse
from django.shortcuts import redirect


class RestrictUserMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if request.path.find("/admin/") > -1:  # restricted admin url for custom admin site
           if not request.user.is_superuser:
              return redirect(reverse('login_url_name'))
        response = self.get_response(request)
        return response

And use that middleware in SETTINGS file:

MIDDLEWARE = [
    # Other middlewares
    'path.to.RestrictUserMiddleware',
]
๐Ÿ‘คruddra

2๐Ÿ‘

custom decorator

decorators.py

from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.decorators import user_passes_test


def superuser_only(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME,
                          login_url='login'):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, redirecting to the login page if necessary.
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_active and u.is_superuser,
        login_url=login_url,
        redirect_field_name=redirect_field_name
    )
    if view_func:
        return actual_decorator(view_func)
    return actual_decorator

To apply decorator to all of your urls

urls.py

def dec_patterns(patterns):
    decorated_patterns = []
    for pattern in patterns:
        callback = pattern.callback
        pattern.callback = superuser_only(callback)
        pattern._callback = superuser_only(callback)
        decorated_patterns.append(pattern)
    return decorated_patterns

url_patterns = [
    path("my-path/", views.my_view),
]
url_patterns = dec_patterns(url_patterns)

0๐Ÿ‘

You can use user_passes_test() decorator with a lambda function.

from django.contrib.auth.decorators import user_passes_test
from django.http import JsonResponse

# first solution

@user_passes_test(lambda user: user.is_superuser)
def test_func(request):
    return JsonResponse(data={})

# second solution

def check_user(user):
    # you can do more actions here
    return user.is_superuser

@user_passes_test(check_user)
def test_func(request):
    return JsonResponse(data={})

According to your question, simply you can add this @user_passes_test(lambda user: user.is_superuser) line which is the first solution of my answer on top of your functions or can go to second solution if you need to do more things.

Leave a comment