Example #1
0
    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
Example #2
0
    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: