Disable the Django admin UI by default

This commit is contained in:
Jeremy Stretch 2024-02-01 13:09:36 -05:00
parent 93b77cb4f0
commit f447b21cd4
7 changed files with 30 additions and 16 deletions

View File

@ -99,6 +99,14 @@ The maximum size (in bytes) of an incoming HTTP request (i.e. `GET` or `POST` da
---
## DJANGO_ADMIN_ENABLED
Default: False
Setting this to True installs the `django.contrib.admin` app and enables the [Django admin UI](https://docs.djangoproject.com/en/5.0/ref/contrib/admin/). This may be necessary to support older plugins which do not integrate with the native NetBox interface.
---
## ENFORCE_GLOBAL_UNIQUE
!!! tip "Dynamic Configuration Parameter"

View File

@ -115,6 +115,7 @@ DEFAULT_PERMISSIONS = getattr(configuration, 'DEFAULT_PERMISSIONS', {
'users.delete_token': ({'user': '$user'},),
})
DEVELOPER = getattr(configuration, 'DEVELOPER', False)
DJANGO_ADMIN_ENABLED = getattr(configuration, 'DJANGO_ADMIN_ENABLED', False)
DOCS_ROOT = getattr(configuration, 'DOCS_ROOT', os.path.join(os.path.dirname(BASE_DIR), 'docs'))
EMAIL = getattr(configuration, 'EMAIL', {})
EVENTS_PIPELINE = getattr(configuration, 'EVENTS_PIPELINE', (
@ -355,7 +356,6 @@ SERVER_EMAIL = EMAIL.get('FROM_EMAIL')
#
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
@ -393,6 +393,9 @@ INSTALLED_APPS = [
'drf_spectacular_sidecar',
]
if DJANGO_ADMIN_ENABLED:
INSTALLED_APPS.insert(0, 'django.contrib.admin')
# Middleware
MIDDLEWARE = [
'graphiql_debug_toolbar.middleware.DebugToolbarMiddleware',

View File

@ -32,6 +32,7 @@ class PluginTest(TestCase):
instance.delete()
self.assertIsNone(instance.pk)
@override_settings(DJANGO_ADMIN_ENABLED=True)
def test_admin(self):
# Test admin view URL resolution

View File

@ -11,7 +11,6 @@ from netbox.graphql.schema import schema
from netbox.graphql.views import GraphQLView
from netbox.plugins.urls import plugin_patterns, plugin_api_patterns
from netbox.views import HomeView, StaticMediaFailureView, SearchView, htmx
from .admin import admin_site
_patterns = [
@ -70,26 +69,25 @@ _patterns = [
# Plugins
path('plugins/', include((plugin_patterns, 'plugins'))),
path('api/plugins/', include((plugin_api_patterns, 'plugins-api'))),
# Admin
path('admin/', admin_site.urls),
]
# Django admin UI
if settings.DJANGO_ADMIN_ENABLED:
from .admin import admin_site
_patterns.append(path('admin/', admin_site.urls))
# django-debug-toolbar
if settings.DEBUG:
import debug_toolbar
_patterns += [
path('__debug__/', include(debug_toolbar.urls)),
]
_patterns.append(path('__debug__/', include(debug_toolbar.urls)))
# Prometheus metrics
if settings.METRICS_ENABLED:
_patterns += [
path('', include('django_prometheus.urls')),
]
_patterns.append(path('', include('django_prometheus.urls')))
# Prepend BASE_PATH
urlpatterns = [
path('{}'.format(settings.BASE_PATH), include(_patterns))
path(settings.BASE_PATH, include(_patterns))
]
handler404 = 'netbox.views.errors.handler_404'

View File

@ -38,7 +38,7 @@
<td>{% checkmark request.user.is_superuser %}</td>
</tr>
<tr>
<th scope="row">{% trans "Admin Access" %}</th>
<th scope="row">{% trans "Staff" %}</th>
<td>{% checkmark request.user.is_staff %}</td>
</tr>
</table>

View File

@ -45,7 +45,11 @@
<td>{{ param }}</td>
<td>{{ current }}</td>
<td>{{ new }}</td>
<td>{% if current != new %}<img src="{% static 'admin/img/icon-changelink.svg' %}" alt="*" title="{% trans "Changed" %}">{% endif %}</td>
<td>
{% if current != new %}
<i class="mdi mdi-pencil text-warning" title="{% trans "Changed" %}"></i>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>

View File

@ -9,9 +9,9 @@
</div>
</a>
<div class="dropdown-menu dropdown-menu-end dropdown-menu-arrow">
{% if request.user.is_staff %}
{% if config.DJANGO_ADMIN_ENABLED and request.user.is_staff %}
<a class="dropdown-item" href="{% url 'admin:index' %}">
<i class="mdi mdi-cog"></i> {% trans "Admin" %}
<i class="mdi mdi-cog"></i> {% trans "Django Admin" %}
</a>
{% endif %}
<a href="{% url 'account:profile' %}" class="dropdown-item">