2020-04-09 19:04:49 +08:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2015-06-12 00:08:48 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
|
|
|
from django.contrib.auth.views import redirect_to_login
|
|
|
|
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
|
2020-04-09 19:04:49 +08:00
|
|
|
from django.shortcuts import resolve_url
|
2015-06-12 00:08:48 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class AccessMixin:
|
2015-06-12 00:08:48 +08:00
|
|
|
"""
|
|
|
|
Abstract CBV mixin that gives access mixins the same customizable
|
|
|
|
functionality.
|
|
|
|
"""
|
|
|
|
login_url = None
|
|
|
|
permission_denied_message = ''
|
|
|
|
raise_exception = False
|
|
|
|
redirect_field_name = REDIRECT_FIELD_NAME
|
|
|
|
|
|
|
|
def get_login_url(self):
|
|
|
|
"""
|
|
|
|
Override this method to override the login_url attribute.
|
|
|
|
"""
|
|
|
|
login_url = self.login_url or settings.LOGIN_URL
|
|
|
|
if not login_url:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'{0} is missing the login_url attribute. Define {0}.login_url, settings.LOGIN_URL, or override '
|
|
|
|
'{0}.get_login_url().'.format(self.__class__.__name__)
|
|
|
|
)
|
2017-04-22 01:52:26 +08:00
|
|
|
return str(login_url)
|
2015-06-12 00:08:48 +08:00
|
|
|
|
|
|
|
def get_permission_denied_message(self):
|
|
|
|
"""
|
|
|
|
Override this method to override the permission_denied_message attribute.
|
|
|
|
"""
|
|
|
|
return self.permission_denied_message
|
|
|
|
|
|
|
|
def get_redirect_field_name(self):
|
|
|
|
"""
|
|
|
|
Override this method to override the redirect_field_name attribute.
|
|
|
|
"""
|
|
|
|
return self.redirect_field_name
|
|
|
|
|
|
|
|
def handle_no_permission(self):
|
2017-07-12 18:07:06 +08:00
|
|
|
if self.raise_exception or self.request.user.is_authenticated:
|
2015-06-12 00:08:48 +08:00
|
|
|
raise PermissionDenied(self.get_permission_denied_message())
|
2020-04-09 19:04:49 +08:00
|
|
|
|
|
|
|
path = self.request.build_absolute_uri()
|
|
|
|
resolved_login_url = resolve_url(self.get_login_url())
|
|
|
|
# If the login url is the same scheme and net location then use the
|
|
|
|
# path as the "next" url.
|
|
|
|
login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
|
|
|
|
current_scheme, current_netloc = urlparse(path)[:2]
|
|
|
|
if (
|
|
|
|
(not login_scheme or login_scheme == current_scheme) and
|
|
|
|
(not login_netloc or login_netloc == current_netloc)
|
|
|
|
):
|
|
|
|
path = self.request.get_full_path()
|
|
|
|
return redirect_to_login(
|
|
|
|
path,
|
|
|
|
resolved_login_url,
|
|
|
|
self.get_redirect_field_name(),
|
|
|
|
)
|
2015-06-12 00:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LoginRequiredMixin(AccessMixin):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Verify that the current user is authenticated."""
|
2015-06-12 00:08:48 +08:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2016-04-02 19:18:26 +08:00
|
|
|
if not request.user.is_authenticated:
|
2015-06-12 00:08:48 +08:00
|
|
|
return self.handle_no_permission()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2015-06-12 00:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PermissionRequiredMixin(AccessMixin):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Verify that the current user has all specified permissions."""
|
2015-06-12 00:08:48 +08:00
|
|
|
permission_required = None
|
|
|
|
|
|
|
|
def get_permission_required(self):
|
|
|
|
"""
|
|
|
|
Override this method to override the permission_required attribute.
|
|
|
|
Must return an iterable.
|
|
|
|
"""
|
|
|
|
if self.permission_required is None:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'{0} is missing the permission_required attribute. Define {0}.permission_required, or override '
|
|
|
|
'{0}.get_permission_required().'.format(self.__class__.__name__)
|
|
|
|
)
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(self.permission_required, str):
|
2017-12-29 04:07:29 +08:00
|
|
|
perms = (self.permission_required,)
|
2015-06-12 00:08:48 +08:00
|
|
|
else:
|
|
|
|
perms = self.permission_required
|
|
|
|
return perms
|
|
|
|
|
2015-07-25 16:36:28 +08:00
|
|
|
def has_permission(self):
|
|
|
|
"""
|
|
|
|
Override this method to customize the way permissions are checked.
|
|
|
|
"""
|
2015-06-12 00:08:48 +08:00
|
|
|
perms = self.get_permission_required()
|
2015-07-25 16:36:28 +08:00
|
|
|
return self.request.user.has_perms(perms)
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
if not self.has_permission():
|
2015-06-12 00:08:48 +08:00
|
|
|
return self.handle_no_permission()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|
2015-06-12 00:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class UserPassesTestMixin(AccessMixin):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Deny a request with a permission error if the test_func() method returns
|
|
|
|
False.
|
2015-06-12 00:08:48 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
raise NotImplementedError(
|
2019-11-19 15:29:47 +08:00
|
|
|
'{} is missing the implementation of the test_func() method.'.format(self.__class__.__name__)
|
2015-06-12 00:08:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_test_func(self):
|
|
|
|
"""
|
|
|
|
Override this method to use a different test_func method.
|
|
|
|
"""
|
|
|
|
return self.test_func
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
user_test_result = self.get_test_func()()
|
|
|
|
if not user_test_result:
|
|
|
|
return self.handle_no_permission()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().dispatch(request, *args, **kwargs)
|