def get_create_user(self,username): if username in MISSING_USERNAMES: return None try: user = User.objects.get(username=username) logging.info('User %s found in new system.' % username) return user except User.DoesNotExist: logging.info('User %s does not exist in new system. Looking up in the NET domain.' % username) try: time.sleep(5) ldap_user = LDAPHelper.search_single(ldap.connection,username) except LDAPHelper.NoUsersFound: logging.error('User %s does not exist in the NET domain.' % username) MISSING_USERNAMES.append(username) except Exception, e: logging.error(str(e) + ' ' + username) else: try: guid = LDAPHelper.extract_guid(ldap_user) except LDAPHelper.MissingAttribute: logging.error('User %s does not have a GUID in the NET domain' % username) else: user = User(username=username) # Try to extract some other details try: user.first_name = LDAPHelper.extract_firstname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.last_name = LDAPHelper.extract_lastname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.email = LDAPHelper.extract_email(ldap_user) except LDAPHelper.MissingAttribute: logging.error('User %s does not have a email' % username) try: user.save() user.profile.guid = guid user.profile.save() except Exception, e: logging.error('Unable to save user `%s`: %s' % (username,str(e))) else:
def authenticate(self, username=None, password=None): try: ldap_helper = LDAPHelper() LDAPHelper.bind(ldap_helper.connection, username, password) ldap_user = LDAPHelper.search_single(ldap_helper.connection, username) except LDAPHelper.LDAPHelperException: return None else: # Extract the GUID try: guid = LDAPHelper.extract_guid(ldap_user) user = User.objects.get(profile__guid=guid) if user.username != username: try: user.username = username user.save() except Exception, e: logging.error('Unable to save user `%s`: %s' % (username,str(e))) return None except LDAPHelper.MissingAttribute: return None except User.DoesNotExist: # Must use create_user() here instead of initiating a new # User object directly so that password hashing is handled # properly user = User.objects.create_user(username=username) # Try to extract some other details try: user.first_name = LDAPHelper.extract_firstname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.last_name = LDAPHelper.extract_lastname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.email = LDAPHelper.extract_email(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.save() user.profile.guid = guid user.profile.save() except Exception, e: logging.error('Unable to save user `%s`: %s' % (username,str(e))) return None
def clean(self, guids_usernames): """ The users submitted by this field my not be users in our system yet. Check to see if they exist. If they don't, create them. Pass all the PKs to the super. """ users = [] if guids_usernames is not None and len(guids_usernames) > 0: try: ldap = LDAPHelper() LDAPHelper.bind(ldap.connection, settings.LDAP_NET_SEARCH_USER, settings.LDAP_NET_SEARCH_PASS) except Exception, e: logging.error(str(e)) raise ValidationError("Unable to connect to LDAP") else: if isinstance(guids_usernames, basestring): guids_usernames = (guids_usernames,) for guid, username in (tuple(gu.split("|")) for gu in guids_usernames): try: user = User.objects.get(profile__guid=guid) except User.DoesNotExist: try: ldap_user = LDAPHelper.search_single(ldap.connection, username) except Exception, e: logging.error(str(e)) raise ValidationError("Looking up `%s` in LDAP failed." % username) else: user = User(username=username) try: user.first_name = LDAPHelper.extract_firstname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.last_name = LDAPHelper.extract_lastname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.email = LDAPHelper.extract_email(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.save() user.profile.guid = guid user.profile.save() except Exception, e: logging.error(str(e)) raise ValidationError("Saving user `%s` failed." % username) users.append(user)
def authenticate(self, request, username=None, password=None): if username not in settings.MANAGERS: return None try: ldap_helper = LDAPHelper() LDAPHelper.bind(ldap_helper.connection, username, password) ldap_user = LDAPHelper.search_single(ldap_helper.connection, username) except LDAPHelper.LDAPHelperException as e: logging.error('LDAP Error: %s' % e) return None else: try: user = User.objects.get(username=username) except User.DoesNotExist: user = User(username=username) # Try to extract some other details try: user.first_name = LDAPHelper.extract_firstname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.last_name = LDAPHelper.extract_lastname(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.email = LDAPHelper.extract_email(ldap_user) except LDAPHelper.MissingAttribute: pass try: user.save() except Exception as e: logging.error('Unable to save user `%s`: %s' % (username, str(e))) return None return user
from unlevents.models import UNLEventtype from unlevents.models import UNLSubscription from unlevents.models import UNLUserHasPermission from util import LDAPHelper from events.functions import remove_html from events.models import Calendar from events.models import Category from events.models import Event from events.models import EventInstance from events.models import Location from events.models import State # Connect to LDAP and bind for searching later ldap = LDAPHelper() LDAPHelper.bind(ldap.connection,settings.LDAP_NET_SEARCH_USER,settings.LDAP_NET_SEARCH_PASS) MISSING_USERNAMES = [] class Command(BaseCommand): def handle(self, *args, **options): self.create_categories() self.create_locations() old_calendars = UNLCalendar.objects.all() for old_calendar in old_calendars: # Check if the old calendar creator exists in our DB calendar_creator = self.get_create_user(str(old_calendar.uidcreated))