2013-11-25 23:13:29 +01:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-11-25 23:13:29 +01:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-11-25 23:13:29 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.contrib.auth import authenticate, login, get_backends
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
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
|
2015-08-21 02:10:41 +02:00
|
|
|
def query_ldap(**options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (**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):
|
2015-08-21 02:10:41 +02:00
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('email', metavar='<email>', type=str,
|
|
|
|
help="email of user to query")
|
|
|
|
|
2013-11-25 23:13:29 +01:00
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
query_ldap(**options)
|