Commit e244f961 authored by mherrb's avatar mherrb Committed by Élie Bouttier
Browse files

Add command to remove inactive / deleted entries from LDAP

parent 19033104
from import BaseCommand, CommandParser, CommandError
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.conf import settings
from adhesions.models import Adhesion, Corporation
from ldap3 import Server, Connection, ALL
from ldap3 import BASE, LEVEL, SUBTREE
from ldap3 import ALL_ATTRIBUTES
import ldap3
class Command(BaseCommand):
help = 'Supprimer de la base LDAP les comptes inactifs ou supprimés de Djadhère.'
def add_arguments(self, parser):
def handle(self, *args, **options):
ldap3.set_config_parameter('DEFAULT_SERVER_ENCODING', 'utf-8')
ldap3.set_config_parameter('DEFAULT_CLIENT_ENCODING', 'utf-8')
server = Server(settings.LDAP_HOST, use_ssl=True, get_info=ALL)
with Connection(server, user=settings.LDAP_MANAGER, password=settings.LDAP_PASSWORD,
auto_bind=ldap3.AUTO_BIND_TLS_BEFORE_BIND) as conn:,
self.stdout.write('{} users founnd in LDAP.'.format(len(conn.entries))))
for ldap_user in conn.entries:
delete = False;
# print(ldap_user.uid.value)
adh = Adhesion.objects.get(pk=ldap_user.uid.value)
except Adhesion.DoesNotExist:
self.stdout.write("Adhesion ADT{} inexistante"\
delete = True
#if is None:
# self.stdout.write("Adhesion ADT{} {} unknown"\
# .format(,
# delete = False
if == False:
self.stdout.write("Adhesion ADT{} {} inactive"\
delete = True
if delete:
if conn.result['result'] != 0:
self.stdout.write("{}: {}"\
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment