From 44bab4989a812ae0d32199debb8b3d436947ae7b Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 8 Dec 2010 08:28:57 -0500 Subject: [PATCH 1/7] Initial authentication work --- browser/views.py | 4 ++++ dashboard/models.py | 5 +++++ dashboard/urls.py | 5 +++++ templates/registration/login.html | 23 +++++++++++++++++++++++ urls.py | 2 ++ 5 files changed, 39 insertions(+) create mode 100644 dashboard/urls.py create mode 100644 templates/registration/login.html diff --git a/browser/views.py b/browser/views.py index 7c1d55a..307dbfe 100755 --- a/browser/views.py +++ b/browser/views.py @@ -1,6 +1,7 @@ import os from django.http import Http404 from django.shortcuts import render_to_response +from django.contrib.auth.decorators import permission_required from codereview.dashboard.models import Repository from codereview.browser import vcs @@ -37,12 +38,14 @@ def _log_data(request, repo, ref, path=None): 'newer': newer, 'older': older, } +@permission_required('dashboard.browse') def log(request, repository, path=None): repo, ref = _repo(request, repository) data = {'repository': repository} data.update(_log_data(request, repo, ref, path)) data.update(_nav_data(request, repo, ref, path)) return render_to_response('browser/log.html', data) +@permission_required('dashboard.browse') def commit(request, repository, ref): try: repository = Repository.objects.get(name=repository) @@ -60,6 +63,7 @@ def commit(request, repository, ref): 'commit': commit, 'diffs': diffs, }) +@permission_required('dashboard.browse') def blob(request, repository, path): repo, ref = _repo(request, repository) data = { diff --git a/dashboard/models.py b/dashboard/models.py index 0826503..077b366 100755 --- a/dashboard/models.py +++ b/dashboard/models.py @@ -5,5 +5,10 @@ class Repository(models.Model): path = models.CharField(max_length=255) type = models.IntegerField(default=0) + class Meta: + permissions = ( + ("browse", "Browse repositories"), + ) + def __unicode__(self): return self.name diff --git a/dashboard/urls.py b/dashboard/urls.py new file mode 100644 index 0000000..ed2bbb1 --- /dev/null +++ b/dashboard/urls.py @@ -0,0 +1,5 @@ +from django.conf.urls.defaults import * + +urlpatterns = patterns('', + (r'^login/$', 'django.contrib.auth.views.login'), +) diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 0000000..1d107a2 --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,23 @@ +{% extends "layouts/default.html" %} + +{% block content %} + +{% if form.errors %} +

Username or password is incorrect

+{% endif %} + +
+ {% csrf_token %} +
+
+
{{ form.username.label_tag }}
+
{{ form.username }}
+
{{ form.password.label_tag }}
+
{{ form.password }}
+
+ + +
+
+ +{% endblock %} diff --git a/urls.py b/urls.py index 5411d6f..666995f 100755 --- a/urls.py +++ b/urls.py @@ -10,6 +10,8 @@ urlpatterns = patterns('', (r'^$', 'codereview.dashboard.views.index'), (r'^browser/', include('codereview.browser.urls')), + (r'^dashboard/', include('codereview.dashboard.urls')), + # Uncomment the admin/doc line below and add 'django.contrib.admindocs' # to INSTALLED_APPS to enable admin documentation: # (r'^admin/doc/', include('django.contrib.admindocs.urls')), From a71e07944a5abf2fc007ee9e87ba24a87fafe453 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 8 Dec 2010 14:03:15 -0500 Subject: [PATCH 2/7] Update login url --- settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/settings.py b/settings.py index b10e1be..391d730 100755 --- a/settings.py +++ b/settings.py @@ -76,6 +76,7 @@ MIDDLEWARE_CLASSES = ( ) ROOT_URLCONF = 'codereview.urls' +LOGIN_URL = '/dashboard/login/' TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". From be02342af532039132597f1c067ee3e91bdca03f Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 8 Dec 2010 16:15:52 -0500 Subject: [PATCH 3/7] Added a fixture for default groups and permissions --- dashboard/fixtures/groups.json | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 dashboard/fixtures/groups.json diff --git a/dashboard/fixtures/groups.json b/dashboard/fixtures/groups.json new file mode 100644 index 0000000..829b37c --- /dev/null +++ b/dashboard/fixtures/groups.json @@ -0,0 +1,12 @@ +[ + { + "pk": 1, + "model": "auth.group", + "fields": { + "name": "Users", + "permissions": [ + ["browse", "dashboard", "repository"] + ] + } + } +] From 3aebdc0cd06c58c471f8a44ad9caa82a8d787436 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 8 Dec 2010 16:16:35 -0500 Subject: [PATCH 4/7] Added a PAM authentication backend --- dashboard/auth.py | 21 +++++++++++++++++++++ settings.py | 5 +++++ 2 files changed, 26 insertions(+) create mode 100644 dashboard/auth.py diff --git a/dashboard/auth.py b/dashboard/auth.py new file mode 100644 index 0000000..c45e809 --- /dev/null +++ b/dashboard/auth.py @@ -0,0 +1,21 @@ +import pam +from django.contrib.auth.models import User, Group + +class PAMBackend: + def authenticate(self, username=None, password=None): + if pam.authenticate(username, password, service='login'): + try: + return User.objects.get(username=username) + except User.DoesNotExist: + # Create new django user + user = User(username=username) + user.set_password(password) + user.save() + user.groups.add(Group.objects.get(name='Users')) + return user + return None + def get_user(self, user_id): + try: + return User.objects.get(pk=user_id) + except User.DoesNotExist: + return None diff --git a/settings.py b/settings.py index 391d730..95ef36a 100755 --- a/settings.py +++ b/settings.py @@ -78,6 +78,11 @@ MIDDLEWARE_CLASSES = ( ROOT_URLCONF = 'codereview.urls' LOGIN_URL = '/dashboard/login/' +AUTHENTICATION_BACKENDS = ( + 'django.contrib.auth.backends.ModelBackend', + 'codereview.dashboard.auth.PAMBackend', +) + TEMPLATE_DIRS = ( # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. From a364b878b233b000a26ab1eedad1e14cf91c58a8 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 8 Dec 2010 17:01:09 -0500 Subject: [PATCH 5/7] Created a logout link --- browser/views.py | 25 ++++++++++++++----------- dashboard/urls.py | 1 + dashboard/views.py | 7 +++++-- templates/layouts/default.html | 8 +++++++- templates/registration/logged_out.html | 4 ++++ 5 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 templates/registration/logged_out.html diff --git a/browser/views.py b/browser/views.py index 307dbfe..dcb3bd3 100755 --- a/browser/views.py +++ b/browser/views.py @@ -1,6 +1,7 @@ import os from django.http import Http404 from django.shortcuts import render_to_response +from django.template import RequestContext from django.contrib.auth.decorators import permission_required from codereview.dashboard.models import Repository from codereview.browser import vcs @@ -41,7 +42,9 @@ def _log_data(request, repo, ref, path=None): @permission_required('dashboard.browse') def log(request, repository, path=None): repo, ref = _repo(request, repository) - data = {'repository': repository} + data = RequestContext(request, { + 'repository': repository + }) data.update(_log_data(request, repo, ref, path)) data.update(_nav_data(request, repo, ref, path)) return render_to_response('browser/log.html', data) @@ -55,21 +58,21 @@ def commit(request, repository, ref): commit = repo.commit(ref) diffs = repo.diff(ref) - return render_to_response('browser/view.html', - { - 'repository': repository, - 'repo': repo, - 'ref': ref, - 'commit': commit, - 'diffs': diffs, - }) + data = RequestContext(request, { + 'repository': repository, + 'repo': repo, + 'ref': ref, + 'commit': commit, + 'diffs': diffs, + }) + return render_to_response('browser/view.html', data) @permission_required('dashboard.browse') def blob(request, repository, path): repo, ref = _repo(request, repository) - data = { + data = RequestContext(request, { 'repository': repository, 'blob': repo.blob(ref, path), - } + }) data.update(_log_data(request, repo, ref, path)) data.update(_nav_data(request, repo, ref, os.path.dirname(path))) return render_to_response('browser/blob.html', data) diff --git a/dashboard/urls.py b/dashboard/urls.py index ed2bbb1..7429b4c 100644 --- a/dashboard/urls.py +++ b/dashboard/urls.py @@ -2,4 +2,5 @@ from django.conf.urls.defaults import * urlpatterns = patterns('', (r'^login/$', 'django.contrib.auth.views.login'), + (r'^logout/$', 'django.contrib.auth.views.logout'), ) diff --git a/dashboard/views.py b/dashboard/views.py index 36c16d6..eecc973 100755 --- a/dashboard/views.py +++ b/dashboard/views.py @@ -1,9 +1,12 @@ from django.shortcuts import render_to_response +from django.template import RequestContext from codereview.dashboard.models import Repository def index(request): """ List available repositories """ repositories = Repository.objects.all() - return render_to_response('dashboard/index.html', - {'repositories': repositories}) + data = RequestContext(request, { + 'repositories': repositories, + }) + return render_to_response('dashboard/index.html', data) diff --git a/templates/layouts/default.html b/templates/layouts/default.html index 0f7b7b2..67bc06c 100644 --- a/templates/layouts/default.html +++ b/templates/layouts/default.html @@ -11,7 +11,13 @@
-

CodeReview +

CodeReview

+ {% if user and user.is_authenticated %} + Currently logged in as + {{ user }} + + Log Out + {% endif %}