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():
|
2018-12-12 19:46:37 +01:00
|
|
|
value = ldap_attrs[ldap_field]
|
|
|
|
if django_field == "avatar":
|
|
|
|
if isinstance(value[0], bytes):
|
|
|
|
value = "(An avatar image file)"
|
|
|
|
print("%s: %s" % (django_field, value))
|
2018-05-31 22:47:26 +02:00
|
|
|
if settings.LDAP_EMAIL_ATTR is not None:
|
|
|
|
print("%s: %s" % ('email', ldap_attrs[settings.LDAP_EMAIL_ATTR]))
|
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)
|