Browse Source

Included LDAP authentication via django-auth-ldap with basic configuration

master
Niklas Brachmann 11 years ago
parent
commit
6ce16de9ff
  1. 14
      cash/models.py
  2. 16
      lugcash2/settings.py

14
cash/models.py

@ -2,7 +2,7 @@ from django.db import models @@ -2,7 +2,7 @@ from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django_auth_ldap.backend import populate_user
class Account(models.Model):
user = models.OneToOneField(User)
@ -16,10 +16,16 @@ class Account(models.Model): @@ -16,10 +16,16 @@ class Account(models.Model):
return self.user.username
@receiver(post_save, sender=User)
def user_post_save_handler(sender, **kwargs):
if kwargs['created']:
account = Account(user=kwargs['instance'])
def user_post_save_handler(sender, instance, created, **kwargs):
if created:
# We don't have ldap_user on creation, so just add the account
account = Account(user=instance)
account.save()
else:
# When we alraedy have an account, we can add the number form LDAP (mongo shit)
if hasattr(instance, 'ldap_user') and instance.ldap_user.attrs.has_key('employeenumber'):
instance.account.card_number = instance.ldap_user.attrs['employeenumber'][0]
instance.account.save()
def buy_products(self, products):
# TODO place it somewhere else

16
lugcash2/settings.py

@ -1,4 +1,6 @@ @@ -1,4 +1,6 @@
from django.conf import global_settings
from django_auth_ldap.config import LDAPSearch, PosixGroupType
import ldap
# Django settings for lugcash2 project.
@ -172,3 +174,17 @@ LOGIN_REDIRECT_URL = '/' @@ -172,3 +174,17 @@ LOGIN_REDIRECT_URL = '/'
# Add custom context processors
#TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ('cash.views.user_context_processor',)
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
)
AUTH_LDAP_SERVER_URI = "ldap://seraph"
AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=People,dc=lug-saar,dc=spc"
AUTH_LDAP_USER_ATTR_MAP = {"first_name": "givenName", "last_name": "sn", "email": "mail"}
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=group,dc=lug-saar,dc=spc", ldap.SCOPE_SUBTREE)
AUTH_LDAP_GROUP_TYPE = PosixGroupType()
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
"is_staff": "cn=cashstaff,ou=group,dc=lug-saar,dc=spc"
}

Loading…
Cancel
Save