2013-11-25 23:13:29 +01:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from argparse import ArgumentParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from typing import Any
|
2013-11-25 23:13:29 +01:00
|
|
|
|
2017-11-16 00:43:27 +01:00
|
|
|
from django.conf import settings
|
2017-11-02 10:19:24 +01:00
|
|
|
from django.contrib.auth import get_backends
|
2013-11-25 23:13:29 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
|
|
|
|
2015-10-13 22:22:27 +02:00
|
|
|
# Quick tool to test whether you're correctly authenticating to LDAP
|
2017-10-26 11:35:57 +02:00
|
|
|
def query_ldap(**options: str) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
email = options['email']
|
2013-11-25 23:13:29 +01:00
|
|
|
for backend in get_backends():
|
|
|
|
if isinstance(backend, LDAPBackend):
|
|
|
|
ldap_attrs = _LDAPUser(backend, backend.django_to_ldap_username(email)).attrs
|
|
|
|
if ldap_attrs is None:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("No such user found")
|
2013-11-25 23:13:29 +01:00
|
|
|
else:
|
|
|
|
for django_field, ldap_field in settings.AUTH_LDAP_USER_ATTR_MAP.items():
|
2015-11-01 17:11:06 +01:00
|
|
|
print("%s: %s" % (django_field, ldap_attrs[ldap_field]))
|
2013-11-25 23:13:29 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('email', metavar='<email>', type=str,
|
|
|
|
help="email of user to query")
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2015-08-21 02:10:41 +02:00
|
|
|
query_ldap(**options)
|