Skip to content

Commit

Permalink
ldap authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
coffindragger committed Oct 26, 2011
1 parent 1dea70a commit 25b475c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
19 changes: 19 additions & 0 deletions apps/mainsite/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django_auth_ldap.backend import LDAPBackend
from django.conf import settings
from django.contrib.auth.models import Group

class StaffLDAPBackend(LDAPBackend):

def get_or_create_user(self, username, ldap_user):
staff_group_name = getattr(settings, 'STAFF_GROUP_NAME', 'Staff')

user, created = super(StaffLDAPBackend, self).get_or_create_user(username, ldap_user)
if created:
try:
user.groups.add(Group.objects.get(name=staff_group_name))
except Group.DoesNotExist:
pass

user.is_staff = True
user.save()
return (user, created)
6 changes: 6 additions & 0 deletions apps/mainsite/local_settings.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ CACHES = {
# '127.0.0.1',
#)

AUTHENTICATION_BACKENDS = [
'mainsite.backends.StaffLDAPBackend',
'django.contrib.auth.backends.ModelBackend',
]
AUTH_LDAP_SERVER_URI = "ldap://ldap.example.com"
AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=people,dc=example,dc=com"
13 changes: 13 additions & 0 deletions apps/mainsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

from mainsite import TOP_DIR




INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
Expand Down Expand Up @@ -91,6 +94,11 @@
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'ldap_debug': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': os.path.join(os.path.dirname(TOP_DIR), 'logs', 'ldap_debug.log'),
}
},
'loggers': {
Expand All @@ -99,6 +107,11 @@
'level': 'ERROR',
'propagate': True,
},
'django_auth_ldap': {
'level': 'DEBUG',
'handlers': ['ldap_debug'],
'propagate': True,
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Django==1.3.1
PIL
PIL==1.1.7
django-auth-ldap==1.0.12
python-ldap==2.4.3

0 comments on commit 25b475c

Please sign in to comment.