diff --git a/binder/middlewares.py b/binder/middlewares.py
new file mode 100644
index 0000000..f7a0a6f
--- /dev/null
+++ b/binder/middlewares.py
@@ -0,0 +1,23 @@
+from django.conf import settings
+from django.contrib.auth import REDIRECT_FIELD_NAME
+from django.http import HttpResponseRedirect
+
+class LoginRequiredMiddleware(object):
+ """Middleware to redirect to the login page if the user isn't authenticated
+
+ After successful authentication the user is redirected back to the page he
+ initially wanted to access.
+ """
+ def process_request(self, request):
+ # allow access to the login url
+ if request.path == settings.LOGIN_URL:
+ return
+ # redirect to the login url if the user isn't authenticated
+ if not request.user.is_authenticated():
+ if request.path not in (settings.LOGIN_URL,
+ settings.LOGIN_REDIRECT_URL):
+ return HttpResponseRedirect('%s?%s=%s' % (settings.LOGIN_URL,
+ REDIRECT_FIELD_NAME,
+ request.path))
+ else:
+ return HttpResponseRedirect(settings.LOGIN_URL)
diff --git a/binder/settings.py b/binder/settings.py
index b88d131..a23da1a 100644
--- a/binder/settings.py
+++ b/binder/settings.py
@@ -67,6 +67,7 @@ MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
+ 'binder.middlewares.LoginRequiredMiddleware',
)
ROOT_URLCONF = 'binder.urls'
@@ -96,6 +97,8 @@ TTL_CHOICES = ((300, "5 minutes"),
RECORD_TYPE_CHOICES = (("A", "A"),
("AAAA", "AAAA"))
+LOGIN_REDIRECT_URL = '/'
+
try:
from local_settings import *
except ImportError:
diff --git a/binder/templates/base.html b/binder/templates/base.html
index 73e7194..963e1d1 100644
--- a/binder/templates/base.html
+++ b/binder/templates/base.html
@@ -23,6 +23,9 @@
Actions
Home
Server List
+ {% if user.is_authenticated %}
+ Logout
+ {% endif %}
{% endblock navigation %}
diff --git a/binder/templates/registration/login.html b/binder/templates/registration/login.html
new file mode 100644
index 0000000..525db3a
--- /dev/null
+++ b/binder/templates/registration/login.html
@@ -0,0 +1,44 @@
+
+
+
+Binder DNS Admin – Login
+
+
+
+
+
+
+ {% if form.errors %}
+
+
+
Wrong username or password! Please try again.
+
+ {% endif %}
+
+
+
+
+
\ No newline at end of file
diff --git a/binder/tests/testViews.py b/binder/tests/testViews.py
index 3513749..5707666 100644
--- a/binder/tests/testViews.py
+++ b/binder/tests/testViews.py
@@ -1,5 +1,6 @@
from django.test import TestCase
from django.test.client import Client
+from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from binder import models
@@ -11,6 +12,12 @@ class GetTests(TestCase):
def setUp(self):
self.client = Client()
+ user = User.objects.create_user('testuser',
+ 'testuser@example.com',
+ 'testpassword')
+ response = self.client.login(username='testuser',
+ password='testpassword')
+
def test_GetIndex(self):
response = self.client.get(reverse("index"))
@@ -49,6 +56,12 @@ class PostTests(TestCase):
models.BindServer(hostname="testserver.test.net",
statistics_port=1234).save()
+ user = User.objects.create_user('testuser',
+ 'testuser@example.com',
+ 'testpassword')
+ response = self.client.login(username='testuser',
+ password='testpassword')
+
def test_DeleteRecordInitial_Empty(self):
"""Ensure the initial deletion form works as expected with no RR list."""
response = self.client.post(reverse("delete_record"),
diff --git a/binder/urls.py b/binder/urls.py
index ad1994b..7e08d4e 100644
--- a/binder/urls.py
+++ b/binder/urls.py
@@ -4,6 +4,10 @@ admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
+
+ url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login'),
+ url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login', name='logout'),
+
url(r'^$', 'binder.views.home_index', name="index"),
url(r'^server_list/$', 'binder.views.view_server_list', name="server_list"),