Created
March 30, 2011 22:21
-
-
Save chronossc/895432 to your computer and use it in GitHub Desktop.
ldap_connector.backends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import ldap | |
from django_auth_ldap.backend import LDAPBackend as _LDAPBackend, _LDAPUser,\ | |
populate_user, populate_user_profile, ldap_settings, User | |
from rh.models import User, Colaborador | |
from django.db.models import ObjectDoesNotExist | |
# The custom backend | |
class LDAPBackend(_LDAPBackend): | |
def get_or_create_user(self, username, ldap_user): | |
""" Get user but supports different name for username attribute. """ | |
username = ldap_user.attrs.get( | |
ldap_settings.AUTH_LDAP_USER_ATTR_MAP.get('username','username'), | |
[username])[0] | |
user,created = User.objects.get_or_create(username__iexact=username, defaults={'username': username.lower()}) | |
return user,created | |
# custom signals that fill user and user profile classes | |
def _populate_user(sender,user,ldap_user,**kwargs): | |
""" | |
This method populate User and create a User Profile, a Colaborador class | |
""" | |
print 'populate user' | |
print sender,user,ldap_user | |
try: | |
user.get_profile() | |
except ObjectDoesNotExist: | |
# the ideal in a generic lib should get AUTH_PROFILE_MODULE from settings, but in my case isn't a generic lib/module :) | |
Colaborador(user=user,nome=user.get_full_name()).save() | |
user.get_profile() | |
return user | |
populate_user.connect(_populate_user) | |
def _populate_user_profile(sender,profile,ldap_user,**kwargs): | |
print 'populate profile' | |
print sender,profile,ldap_user,kwargs | |
profile.nome = ldap_user._user.get_full_name() | |
return profile | |
populate_user_profile.connect(_populate_user_profile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment