Merge branch 'auth'

This commit is contained in:
Correl Roush 2010-12-08 18:39:40 -05:00
commit 5864556b9e
11 changed files with 113 additions and 14 deletions

View file

@ -1,6 +1,8 @@
import os import os
from django.http import Http404 from django.http import Http404
from django.shortcuts import render_to_response 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.dashboard.models import Repository
from codereview.browser import vcs from codereview.browser import vcs
@ -37,12 +39,16 @@ def _log_data(request, repo, ref, path=None):
'newer': newer, 'newer': newer,
'older': older, 'older': older,
} }
@permission_required('dashboard.browse')
def log(request, repository, path=None): def log(request, repository, path=None):
repo, ref = _repo(request, repository) repo, ref = _repo(request, repository)
data = {'repository': repository} data = RequestContext(request, {
'repository': repository
})
data.update(_log_data(request, repo, ref, path)) data.update(_log_data(request, repo, ref, path))
data.update(_nav_data(request, repo, ref, path)) data.update(_nav_data(request, repo, ref, path))
return render_to_response('browser/log.html', data) return render_to_response('browser/log.html', data)
@permission_required('dashboard.browse')
def commit(request, repository, ref): def commit(request, repository, ref):
try: try:
repository = Repository.objects.get(name=repository) repository = Repository.objects.get(name=repository)
@ -52,20 +58,21 @@ def commit(request, repository, ref):
commit = repo.commit(ref) commit = repo.commit(ref)
diffs = repo.diff(ref) diffs = repo.diff(ref)
return render_to_response('browser/view.html', data = RequestContext(request, {
{
'repository': repository, 'repository': repository,
'repo': repo, 'repo': repo,
'ref': ref, 'ref': ref,
'commit': commit, 'commit': commit,
'diffs': diffs, 'diffs': diffs,
}) })
return render_to_response('browser/view.html', data)
@permission_required('dashboard.browse')
def blob(request, repository, path): def blob(request, repository, path):
repo, ref = _repo(request, repository) repo, ref = _repo(request, repository)
data = { data = RequestContext(request, {
'repository': repository, 'repository': repository,
'blob': repo.blob(ref, path), 'blob': repo.blob(ref, path),
} })
data.update(_log_data(request, repo, ref, path)) data.update(_log_data(request, repo, ref, path))
data.update(_nav_data(request, repo, ref, os.path.dirname(path))) data.update(_nav_data(request, repo, ref, os.path.dirname(path)))
return render_to_response('browser/blob.html', data) return render_to_response('browser/blob.html', data)

21
dashboard/auth.py Normal file
View file

@ -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

View file

@ -0,0 +1,12 @@
[
{
"pk": 1,
"model": "auth.group",
"fields": {
"name": "Users",
"permissions": [
["browse", "dashboard", "repository"]
]
}
}
]

View file

@ -5,5 +5,10 @@ class Repository(models.Model):
path = models.CharField(max_length=255) path = models.CharField(max_length=255)
type = models.IntegerField(default=0) type = models.IntegerField(default=0)
class Meta:
permissions = (
("browse", "Browse repositories"),
)
def __unicode__(self): def __unicode__(self):
return self.name return self.name

6
dashboard/urls.py Normal file
View file

@ -0,0 +1,6 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^logout/$', 'django.contrib.auth.views.logout'),
)

View file

@ -1,9 +1,14 @@
from django.shortcuts import render_to_response 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.dashboard.models import Repository
@permission_required('dashboard.browse')
def index(request): def index(request):
""" List available repositories """ List available repositories
""" """
repositories = Repository.objects.all() repositories = Repository.objects.all()
return render_to_response('dashboard/index.html', data = RequestContext(request, {
{'repositories': repositories}) 'repositories': repositories,
})
return render_to_response('dashboard/index.html', data)

View file

@ -76,6 +76,13 @@ MIDDLEWARE_CLASSES = (
) )
ROOT_URLCONF = 'codereview.urls' ROOT_URLCONF = 'codereview.urls'
LOGIN_URL = '/dashboard/login/'
LOGIN_REDIRECT_URL = '/'
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'codereview.dashboard.auth.PAMBackend',
)
TEMPLATE_DIRS = ( TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".

View file

@ -11,7 +11,13 @@
<body> <body>
<div id="container"> <div id="container">
<div class="header"> <div class="header">
<h1><a href="{% url codereview.dashboard.views.index %}">CodeReview</a></title> <h1><a href="{% url codereview.dashboard.views.index %}">CodeReview</a></title></h1>
{% if user and user.is_authenticated %}
Currently logged in as
{{ user }}
<a href="{% url django.contrib.auth.views.logout %}">Log Out</a>
{% endif %}
</div> </div>
<div class="navigation"> <div class="navigation">
{% block navigation %} {% block navigation %}

View file

@ -0,0 +1,5 @@
{% extends "layouts/default.html" %}
{% block content %}
<p>You have been logged out.</p>
<a href="{% url django.contrib.auth.views.login %}">Log back in</a>
{% endblock %}

View file

@ -0,0 +1,23 @@
{% extends "layouts/default.html" %}
{% block content %}
{% if form.errors %}
<p>Username or password is incorrect</p>
{% endif %}
<form method="post" action="{% url django.contrib.auth.views.login %}">
{% csrf_token %}
<fieldset>
<dl>
<dt>{{ form.username.label_tag }}</dt>
<dd>{{ form.username }}</dd>
<dt>{{ form.password.label_tag }}</dt>
<dd>{{ form.password }}</dd>
</dl>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</fieldset>
</form>
{% endblock %}

View file

@ -10,6 +10,8 @@ urlpatterns = patterns('',
(r'^$', 'codereview.dashboard.views.index'), (r'^$', 'codereview.dashboard.views.index'),
(r'^browser/', include('codereview.browser.urls')), (r'^browser/', include('codereview.browser.urls')),
(r'^dashboard/', include('codereview.dashboard.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs' # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation: # to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')), # (r'^admin/doc/', include('django.contrib.admindocs.urls')),