Skip to content

Commit 7d9ccc6

Browse files
committed
disable django admin panel by default
1 parent 21573bc commit 7d9ccc6

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

dojo/settings/settings.dist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
DD_DJANGO_METRICS_ENABLED=(bool, False),
5252
DD_LOGIN_REDIRECT_URL=(str, "/"),
5353
DD_LOGIN_URL=(str, "/login"),
54-
DD_DJANGO_ADMIN_ENABLED=(bool, True),
54+
DD_DJANGO_ADMIN_ENABLED=(bool, False),
5555
DD_SESSION_COOKIE_HTTPONLY=(bool, True),
5656
DD_CSRF_COOKIE_HTTPONLY=(bool, True),
5757
DD_SECURE_SSL_REDIRECT=(bool, False),

dojo/settings/template-env

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Django Debug, don't enable on production!
22
DD_DEBUG=False
33

4-
# Enables Django Admin
5-
DD_DJANGO_ADMIN_ENABLED=True
4+
# Enables the Django admin interface at /admin/. Off by default; uncomment
5+
# this line to expose it. Access is restricted to superusers regardless.
6+
# DD_DJANGO_ADMIN_ENABLED=True
67

78
# A secret key for a particular Django installation.
89
DD_SECRET_KEY=#DD_SECRET_KEY#

unittests/test_adminsite.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import django.apps
22
from django.contrib import admin
33
from django.contrib.auth import get_user_model
4-
from django.urls import reverse
4+
from django.contrib.auth.models import AnonymousUser
5+
from django.http import HttpRequest
56

67
from .dojo_test_case import DojoTestCase
78

@@ -30,37 +31,48 @@ class AdminAccessGate(DojoTestCase):
3031
is_staff is a near-superuser bypass under the legacy OS auth model, so
3132
/admin/ must require is_superuser. Django's default UserAdmin change
3233
form would otherwise let any is_staff user with auth.change_user tick
33-
is_superuser on themselves.
34+
is_superuser on themselves. Tested at the gate-function level so the
35+
assertions hold regardless of whether DD_DJANGO_ADMIN_ENABLED mounts
36+
the admin URLConf in the current environment.
3437
"""
3538

36-
def test_staff_non_superuser_blocked_from_admin(self):
39+
@staticmethod
40+
def _request_for(user):
41+
req = HttpRequest()
42+
req.user = user
43+
return req
44+
45+
def test_staff_non_superuser_denied(self):
3746
User = get_user_model()
3847
password = "testTEST1234!@#$"
3948
staff = User.objects.create_user(
4049
username="staff-no-root", password=password, is_staff=True,
4150
)
42-
self.client.force_login(staff)
51+
self.assertFalse(admin.site.has_permission(self._request_for(staff)))
4352

44-
for url in (
45-
reverse("admin:index"),
46-
reverse("admin:auth_user_changelist"),
47-
reverse("admin:auth_user_change", args=[staff.id]),
48-
):
49-
with self.subTest(url=url):
50-
resp = self.client.get(url)
51-
self.assertEqual(resp.status_code, 302, url)
52-
self.assertIn("/admin/login/", resp["Location"], url)
53+
def test_non_staff_non_superuser_denied(self):
54+
User = get_user_model()
55+
password = "testTEST1234!@#$"
56+
plain = User.objects.create_user(username="plain-user", password=password)
57+
self.assertFalse(admin.site.has_permission(self._request_for(plain)))
5358

54-
def test_superuser_can_reach_admin(self):
59+
def test_anonymous_denied(self):
60+
self.assertFalse(admin.site.has_permission(self._request_for(AnonymousUser())))
61+
62+
def test_inactive_superuser_denied(self):
5563
User = get_user_model()
5664
password = "testTEST1234!@#$"
5765
root = User.objects.create_superuser(
58-
username="root-test", email="r@example.com", password=password,
66+
username="inactive-root", email="i@example.com", password=password,
5967
)
60-
self.client.force_login(root)
68+
root.is_active = False
69+
root.save(update_fields=["is_active"])
70+
self.assertFalse(admin.site.has_permission(self._request_for(root)))
6171

62-
self.assertEqual(self.client.get(reverse("admin:index")).status_code, 200)
63-
self.assertEqual(
64-
self.client.get(reverse("admin:auth_user_changelist")).status_code,
65-
200,
72+
def test_active_superuser_allowed(self):
73+
User = get_user_model()
74+
password = "testTEST1234!@#$"
75+
root = User.objects.create_superuser(
76+
username="root-test", email="r@example.com", password=password,
6677
)
78+
self.assertTrue(admin.site.has_permission(self._request_for(root)))

0 commit comments

Comments
 (0)