From e19df73642b896f4ba75bb8b414b610fffde7bc4 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sat, 15 May 2021 13:27:43 -0400 Subject: [PATCH] Add a command to import a user from LDAP --- tasks/management/commands/import_ldap_user.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tasks/management/commands/import_ldap_user.py diff --git a/tasks/management/commands/import_ldap_user.py b/tasks/management/commands/import_ldap_user.py new file mode 100644 index 0000000..c3c1551 --- /dev/null +++ b/tasks/management/commands/import_ldap_user.py @@ -0,0 +1,18 @@ +from django.core.management.base import BaseCommand + +from django_auth_ldap.backend import LDAPBackend + + +class Command(BaseCommand): + help = 'Import a user from LDAP by username' + + def add_arguments(self, parser): + parser.add_argument('username', type=str) + + def handle(self, *args, **options): + username = options['username'] + user = LDAPBackend().populate_user(username) + if user is None: + raise Exception(f"No user named {username}") + else: + print("Imported user:", user)