| 1 | from functools import wraps
|
|---|
| 2 | from urllib.parse import urlparse
|
|---|
| 3 |
|
|---|
| 4 | from django.conf import settings
|
|---|
| 5 | from django.contrib.auth import REDIRECT_FIELD_NAME
|
|---|
| 6 | from django.core.exceptions import PermissionDenied
|
|---|
| 7 | from django.shortcuts import resolve_url
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | def _passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME, field_to_test=None):
|
|---|
| 11 | """
|
|---|
| 12 | Decorator for views that checks that some object passes the given test,
|
|---|
| 13 | redirecting to the log-in page if necessary. The test should be a callable
|
|---|
| 14 | that takes the object and returns True if the object passes.
|
|---|
| 15 | """
|
|---|
| 16 | def decorator(view_func):
|
|---|
| 17 | @wraps(view_func)
|
|---|
| 18 | def _wrapped_view(request, *args, **kwargs):
|
|---|
| 19 | object_to_test = request if field_to_test is None else request[field_to_test]
|
|---|
| 20 | if test_func(object_to_test):
|
|---|
| 21 | return view_func(request, *args, **kwargs)
|
|---|
| 22 | path = request.build_absolute_uri()
|
|---|
| 23 | resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
|
|---|
| 24 | # If the login url is the same scheme and net location then just
|
|---|
| 25 | # use the path as the "next" url.
|
|---|
| 26 | login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
|
|---|
| 27 | current_scheme, current_netloc = urlparse(path)[:2]
|
|---|
| 28 | if ((not login_scheme or login_scheme == current_scheme) and
|
|---|
| 29 | (not login_netloc or login_netloc == current_netloc)):
|
|---|
| 30 | path = request.get_full_path()
|
|---|
| 31 | from django.contrib.auth.views import redirect_to_login
|
|---|
| 32 | return redirect_to_login(
|
|---|
| 33 | path, resolved_login_url, redirect_field_name)
|
|---|
| 34 | return _wrapped_view
|
|---|
| 35 | return decorator
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
|---|
| 39 | """
|
|---|
| 40 | Decorator for views that checks that the user passes the given test,
|
|---|
| 41 | redirecting to the log-in page if necessary. The test should be a callable
|
|---|
| 42 | that takes the user object and returns True if the user passes.
|
|---|
| 43 | """
|
|---|
| 44 | return _passes_test(test_func, login_url, redirect_field_name, field_to_test="user")
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | def request_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
|---|
| 48 | """
|
|---|
| 49 | Decorator for views that checks that the request passes the given test,
|
|---|
| 50 | redirecting to the log-in page if necessary. The test should be a callable
|
|---|
| 51 | that takes the request object and returns True if the request passes.
|
|---|
| 52 | """
|
|---|
| 53 | return _passes_test(test_func, login_url, redirect_field_name, field_to_test=None)
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 | def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
|
|---|
| 57 | """
|
|---|
| 58 | Decorator for views that checks that the user is logged in, redirecting
|
|---|
| 59 | to the log-in page if necessary.
|
|---|
| 60 | """
|
|---|
| 61 | actual_decorator = user_passes_test(
|
|---|
| 62 | lambda u: u.is_authenticated,
|
|---|
| 63 | login_url=login_url,
|
|---|
| 64 | redirect_field_name=redirect_field_name
|
|---|
| 65 | )
|
|---|
| 66 | if function:
|
|---|
| 67 | return actual_decorator(function)
|
|---|
| 68 | return actual_decorator
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | def permission_required(perm, login_url=None, raise_exception=False):
|
|---|
| 72 | """
|
|---|
| 73 | Decorator for views that checks whether a user has a particular permission
|
|---|
| 74 | enabled, redirecting to the log-in page if necessary.
|
|---|
| 75 | If the raise_exception parameter is given the PermissionDenied exception
|
|---|
| 76 | is raised.
|
|---|
| 77 | """
|
|---|
| 78 | def check_perms(user):
|
|---|
| 79 | if isinstance(perm, str):
|
|---|
| 80 | perms = (perm,)
|
|---|
| 81 | else:
|
|---|
| 82 | perms = perm
|
|---|
| 83 | # First check if the user has the permission (even anon users)
|
|---|
| 84 | if user.has_perms(perms):
|
|---|
| 85 | return True
|
|---|
| 86 | # In case the 403 handler should be called raise the exception
|
|---|
| 87 | if raise_exception:
|
|---|
| 88 | raise PermissionDenied
|
|---|
| 89 | # As the last resort, show the login form
|
|---|
| 90 | return False
|
|---|
| 91 | return user_passes_test(check_perms, login_url=login_url)
|
|---|