diff --git a/apiserver/apiserver/api/utils_auth.py b/apiserver/apiserver/api/utils_auth.py index ae9d392..f0c12e9 100644 --- a/apiserver/apiserver/api/utils_auth.py +++ b/apiserver/apiserver/api/utils_auth.py @@ -23,6 +23,8 @@ def auth_api(url, data=None, json=None): logger.info('Auth {} - {} - {}'.format(url, e.__class__.__name__, str(e))) logger.info('Auth timeout occured, assuming it worked and returning 200.') return 200 + except KeyboardInterrupt: + raise except BaseException as e: logger.error('Auth {} - {} - {}'.format(url, e.__class__.__name__, str(e))) return None diff --git a/apiserver/apiserver/api/utils_ldap.py b/apiserver/apiserver/api/utils_ldap.py index f3ceadc..3362c2e 100644 --- a/apiserver/apiserver/api/utils_ldap.py +++ b/apiserver/apiserver/api/utils_ldap.py @@ -16,6 +16,8 @@ def ldap_api(route, data): url = secrets.LDAP_API_URL + route r = requests.post(url, data=data, headers=headers, timeout=10) return r.status_code + except KeyboardInterrupt: + raise except BaseException as e: logger.error('LDAP {} - {} - {}'.format(url, e.__class__.__name__, str(e))) return None diff --git a/apiserver/scripts/generate_ldap_users.py b/apiserver/scripts/generate_ldap_users.py new file mode 100755 index 0000000..b1ded46 --- /dev/null +++ b/apiserver/scripts/generate_ldap_users.py @@ -0,0 +1,43 @@ +# Generates missing LDAP users from Spaceport members +# this fixes an issue when a very old member resets their password +# and their LDAP user can't be found in the system. +# +# Assigns a random password to the user. + + +import django, sys, os +os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings' +django.setup() + +from apiserver.api import models, utils, utils_ldap +from uuid import uuid4 +import re + +random_password = lambda: str(uuid4())[:23] + +members = models.Member.objects.all() + +for member in members: + print() + + username = member.user.username + print('Checking LDAP for member id:', member.id, 'username:', username) + + if utils_ldap.is_configured(): + result = utils_ldap.find_user(member.user.username) + if result == 200: + print(' username found, skipping') + continue + + print(' generating LDAP user...') + + data = dict( + first_name=member.first_name, + last_name=member.last_name, + username=username, + email=member.user.email, + password1=random_password(), + ) + result = utils_ldap.create_user(data) + + print(' result:', result)