Skip to content

Commit ee6753f

Browse files
author
Rafi Blecher
committed
Merge dev to master, with LDAP working. v0.2.0
2 parents c4bf84b + ded8a31 commit ee6753f

File tree

800 files changed

+78164
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

800 files changed

+78164
-21
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
*.pyc
22
*.pyo
3-
*.db
43
.DS_Store
54
.coverage
65
local_settings.py
7-
/static

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Lets remake the csesoc website
66
## Developer setup for Ubuntu
77
###Core packages
88
```
9-
$ sudo apt-get install git pip sqlite3
9+
$ sudo apt-get install git pip sqlite3 python-ldap
1010
```
1111

1212
###Setting up virtualenv

auth/__init__.py

Whitespace-only changes.

auth/ldap_auth.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import ldap
2+
from django.conf import settings
3+
from django.contrib.auth.models import User
4+
5+
#DJANGO_SETTINGS_MODULE
6+
class ldapBackend():
7+
8+
def authenticate(self,username,password):
9+
# Login as fakeroot if in development
10+
if settings.DEBUG:
11+
try:
12+
user = User.objects.get(username='fakeroot')
13+
return user
14+
except User.DoesNotExist:
15+
user = User(username='fakeroot', password='nil')
16+
user.is_staff = True
17+
user.is_superuser = True
18+
user.first_name = 'fakeroot'
19+
user.email = '[email protected]'
20+
user.save()
21+
return user
22+
else:
23+
try:
24+
l = ldap.open("ad.unsw.edu.au")
25+
l.protocol_version = ldap.VERSION3
26+
27+
upn = username + '@ad.unsw.edu.au'
28+
29+
l.bind_s(upn, password)
30+
31+
baseDN = "OU=IDM_People,OU=IDM,DC=ad,DC=unsw,DC=edu,DC=au"
32+
searchScope = ldap.SCOPE_SUBTREE
33+
retrieveAttributes = ['cn', 'displayNamePrintable', 'givenName', 'sn', 'mail']
34+
searchFilter = "cn=" + username
35+
36+
ldap_result = l.search(baseDN, searchScope, searchFilter, retrieveAttributes)
37+
result_type, result_data = l.result(ldap_result, 0)
38+
39+
user_dn,attr_results = result_data[0]
40+
41+
try:
42+
user = User.objects.get(username=attr_results['cn'][0])
43+
return user
44+
except User.DoesNotExist:
45+
user = User(username=username, password='get from unsw ad')
46+
user.is_staff = False
47+
user.is_superuser = False
48+
user.first_name = attr_results['givenName'][0]
49+
user.last_name = attr_results['sn'][0]
50+
user.email = attr_results['mail'][0]
51+
user.save()
52+
return user
53+
54+
except ldap.LDAPError, e:
55+
print e
56+
return None
57+
58+
def get_user(self, user_id):
59+
try:
60+
return User.objects.get(pk=user_id)
61+
except User.DoesNotExist:
62+
return None

auth/views.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django.shortcuts import get_object_or_404, render_to_response, redirect
2+
from django.template import RequestContext
3+
from django.http import HttpResponse
4+
from django.conf import settings
5+
from django.contrib.auth import authenticate, login, logout
6+
from django.contrib import messages
7+
8+
def signin(request):
9+
redirect_path = (request.REQUEST.get('redirect', '/'))
10+
if request.user.is_authenticated():
11+
return redirect(redirect_path)
12+
if request.method == 'POST':
13+
student_number = request.REQUEST['zID']
14+
user = authenticate(username=student_number, password=request.REQUEST['zPass'])
15+
16+
if user != None:
17+
login(request, user)
18+
messages.success(request, user.get_full_name() + " you're now logged in.")
19+
return redirect(redirect_path)
20+
else:
21+
messages.error(request, "Invalid login.")
22+
return render_to_response('auth/login.html', {'redirect_path': redirect_path}, context_instance=RequestContext(request), user=user)
23+
else:
24+
return render_to_response('auth/login.html', {'redirect_path': redirect_path}, context_instance=RequestContext(request), user=user)
25+
26+
def signout(request):
27+
logout(request)
28+
request.session.flush()
29+
messages.success(request, "You have successfully been logged out.")
30+
return redirect('/')

settings.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@
143143
"django.template.loaders.app_directories.Loader",
144144
)
145145

146-
AUTHENTICATION_BACKENDS = ("mezzanine.core.auth_backends.MezzanineBackend",)
146+
AUTHENTICATION_BACKENDS = (
147+
#"mezzanine.core.auth_backends.MezzanineBackend",
148+
'auth.ldap_auth.ldapBackend',
149+
)
147150

148151
# List of finder classes that know how to find static files in
149152
# various locations.
@@ -165,9 +168,9 @@
165168
DATABASES = {
166169
"default": {
167170
# Add "postgresql_psycopg2", "mysql", "sqlite3" or "oracle".
168-
"ENGINE": "django.db.backends.",
171+
"ENGINE": "django.db.backends.sqlite3",
169172
# DB name or path to database file if using sqlite3.
170-
"NAME": "",
173+
"NAME": "soc-website.db",
171174
# Not used with sqlite3.
172175
"USER": "",
173176
# Not used with sqlite3.
@@ -197,6 +200,11 @@
197200
# project specific.
198201
CACHE_MIDDLEWARE_KEY_PREFIX = PROJECT_DIRNAME
199202

203+
# Make these unique, and don't share it with anybody.
204+
SECRET_KEY = "bb144f82-39a0-4637-ab9c-c60c80a8d294b45fb1cc-bbb8-4c9e-a87a-3f6ad80acbdfc1841843-5187-44c6-b9a9-c5a05927e21b"
205+
NEVERCACHE_KEY = "f57f7b5c-bb1c-4302-bef5-26b6b3b43f2de9495caf-1f02-47ef-addb-48ad59feab612166798a-8399-4b7a-9ada-a1eb07eebb6f"
206+
207+
200208
# URL prefix for static files.
201209
# Example: "http://media.lawrence.com/static/"
202210
STATIC_URL = "/static/"
@@ -248,6 +256,7 @@
248256
"mezzanine.pages",
249257
"mezzanine.galleries",
250258
"mezzanine.twitter",
259+
'auth',
251260
#"mezzanine.accounts",
252261
#"mezzanine.mobile",
253262
)

soc-website.db

118 KB
Binary file not shown.

static.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
path=`readlink -m "$0" | sed 's/\/static.sh$//'`
4+
if [[ `grep -Fq "STATIC_ROOT" "$path/local_settings.py" 2> /dev/null` ]]
5+
then
6+
echo "STATIC_ROOT already in local_settings.py. Please edit manually."
7+
else
8+
echo "STATIC_ROOT = '$path/static'" >> $path/local_settings.py;
9+
fi

0 commit comments

Comments
 (0)