34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
from django.contrib.admin import AdminSite
|
|
from django.contrib.admin.apps import AdminConfig
|
|
|
|
from .hosting.access import is_tenant
|
|
|
|
|
|
class CoulobreAdminSite(AdminSite):
|
|
def has_permission(self, request):
|
|
return super().has_permission(request) and not is_tenant(request.user)
|
|
|
|
def login(self, request, extra_context=None):
|
|
from django.contrib.auth.views import redirect_to_login
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.shortcuts import redirect
|
|
from django.urls import reverse
|
|
from django.utils.http import url_has_allowed_host_and_scheme
|
|
|
|
destination = request.GET.get("next")
|
|
if not url_has_allowed_host_and_scheme(
|
|
destination,
|
|
{request.get_host()},
|
|
require_https=request.is_secure(),
|
|
):
|
|
destination = reverse("admin:index")
|
|
if request.user.is_authenticated:
|
|
if not self.has_permission(request):
|
|
raise PermissionDenied
|
|
return redirect(destination)
|
|
return redirect_to_login(destination, login_url="panel:login")
|
|
|
|
|
|
class CoulobreAdminConfig(AdminConfig):
|
|
default_site = "coulobre.admin.CoulobreAdminSite"
|