| 1 |
from functools import wraps |
| 2 |
from urllib.parse import urlsplit |
| 3 |
|
| 4 |
from asgiref.sync import async_to_sync, iscoroutinefunction, sync_to_async |
| 5 |
|
| 6 |
from django.conf import settings |
| 7 |
from django.contrib.auth import REDIRECT_FIELD_NAME |
| 8 |
from django.core.exceptions import PermissionDenied |
| 9 |
from django.shortcuts import resolve_url |
| 10 |
|
| 11 |
|
| 12 |
def user_passes_test( |
| 13 |
test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME |
| 14 |
): |
| 15 |
""" |
| 16 |
Decorator for views that checks that the user passes the given test, |
| 17 |
redirecting to the log-in page if necessary. The test should be a callable |
| 18 |
that takes the user object and returns True if the user passes. |
| 19 |
""" |
| 20 |
|
| 21 |
def decorator(view_func): |
| 22 |
def _redirect_to_login(request): |
| 23 |
path = request.build_absolute_uri() |
| 24 |
resolved_login_url = resolve_url(login_url or settings.LOGIN_URL) |
| 25 |
# If the login url is the same scheme and net location then just |
| 26 |
# use the path as the "next" url. |
| 27 |
login_scheme, login_netloc = urlsplit(resolved_login_url)[:2] |
| 28 |
current_scheme, current_netloc = urlsplit(path)[:2] |
| 29 |
if (not login_scheme or login_scheme == current_scheme) and ( |
| 30 |
not login_netloc or login_netloc == current_netloc |
| 31 |
): |
| 32 |
path = request.get_full_path() |
| 33 |
from django.contrib.auth.views import redirect_to_login |
| 34 |
|
| 35 |
return redirect_to_login(path, resolved_login_url, redirect_field_name) |
| 36 |
|
| 37 |
if iscoroutinefunction(view_func): |
| 38 |
|
| 39 |
async def _view_wrapper(request, *args, **kwargs): |
| 40 |
auser = await request.auser() |
| 41 |
if iscoroutinefunction(test_func): |
| 42 |
test_pass = await test_func(auser) |
| 43 |
else: |
| 44 |
test_pass = await sync_to_async(test_func)(auser) |
| 45 |
|
| 46 |
if test_pass: |
| 47 |
return await view_func(request, *args, **kwargs) |
| 48 |
return _redirect_to_login(request) |
| 49 |
|
| 50 |
else: |
| 51 |
|
| 52 |
def _view_wrapper(request, *args, **kwargs): |
| 53 |
if iscoroutinefunction(test_func): |
| 54 |
test_pass = async_to_sync(test_func)(request.user) |
| 55 |
else: |
| 56 |
test_pass = test_func(request.user) |
| 57 |
|
| 58 |
if test_pass: |
| 59 |
return view_func(request, *args, **kwargs) |
| 60 |
return _redirect_to_login(request) |
| 61 |
|
| 62 |
# Attributes used by LoginRequiredMiddleware. |
| 63 |
_view_wrapper.login_url = login_url |
| 64 |
_view_wrapper.redirect_field_name = redirect_field_name |
| 65 |
|
| 66 |
return wraps(view_func)(_view_wrapper) |
| 67 |
|
| 68 |
return decorator |
| 69 |
|
| 70 |
|
| 71 |
def login_required( |
| 72 |
function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None |
| 73 |
): |
| 74 |
""" |
| 75 |
Decorator for views that checks that the user is logged in, redirecting |
| 76 |
to the log-in page if necessary. |
| 77 |
""" |
| 78 |
actual_decorator = user_passes_test( |
| 79 |
lambda u: u.is_authenticated, |
| 80 |
login_url=login_url, |
| 81 |
redirect_field_name=redirect_field_name, |
| 82 |
) |
| 83 |
if function: |
| 84 |
return actual_decorator(function) |
| 85 |
return actual_decorator |
| 86 |
|
| 87 |
|
| 88 |
def login_not_required(view_func): |
| 89 |
""" |
| 90 |
Decorator for views that allows access to unauthenticated requests. |
| 91 |
""" |
| 92 |
view_func.login_required = False |
| 93 |
return view_func |
| 94 |
|
| 95 |
|
| 96 |
def permission_required(perm, login_url=None, raise_exception=False): |
| 97 |
""" |
| 98 |
Decorator for views that checks whether a user has a particular permission |
| 99 |
enabled, redirecting to the log-in page if necessary. |
| 100 |
If the raise_exception parameter is given the PermissionDenied exception |
| 101 |
is raised. |
| 102 |
""" |
| 103 |
if isinstance(perm, str): |
| 104 |
perms = (perm,) |
| 105 |
else: |
| 106 |
perms = perm |
| 107 |
|
| 108 |
def decorator(view_func): |
| 109 |
if iscoroutinefunction(view_func): |
| 110 |
|
| 111 |
async def check_perms(user): |
| 112 |
# First check if the user has the permission (even anon users). |
| 113 |
if await user.ahas_perms(perms): |
| 114 |
return True |
| 115 |
# In case the 403 handler should be called raise the exception. |
| 116 |
if raise_exception: |
| 117 |
raise PermissionDenied |
| 118 |
# As the last resort, show the login form. |
| 119 |
return False |
| 120 |
|
| 121 |
else: |
| 122 |
|
| 123 |
def check_perms(user): |
| 124 |
# First check if the user has the permission (even anon users). |
| 125 |
if user.has_perms(perms): |
| 126 |
return True |
| 127 |
# In case the 403 handler should be called raise the exception. |
| 128 |
if raise_exception: |
| 129 |
raise PermissionDenied |
| 130 |
# As the last resort, show the login form. |
| 131 |
return False |
| 132 |
|
| 133 |
return user_passes_test(check_perms, login_url=login_url)(view_func) |
| 134 |
|
| 135 |
return decorator |