123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- # -*- coding: utf-8 -*-
- from __future__ import unicode_literals
- from optparse import make_option
- from django.core.management.base import BaseCommand, CommandError
- from django.conf import settings
- from coin.members.models import Member, LdapUser
- class Command(BaseCommand):
- args = '[login1 login2 ...]'
- help = """Regenerate user objects in the LDAP backend. This is useful if you
- have added or modified an LDAP attribute and want to apply the change
- to all existing members. By default, all members from the local
- database are regenerated this way, but you can restrict the query by
- passing a list of user logins as argument.
- If --erase-all is passed, then the LDAP database is cleared of all its
- users before regenerating users from the local database. Use this
- option with a lot of caution, as you will lose any user that was
- present in the LDAP database but not in the local database.
- Additionally, Unix UIDs (attribute "uidNumber" in LDAP) are currently
- generated when saving a new user, so --erase-all might lead to
- different UIDs after the regeneration. This is certainly a bad idea
- if your Unix users are based on LDAP."""
- option_list = BaseCommand.option_list + (
- make_option('--erase-all',
- action='store_true',
- dest='erase',
- default=False,
- help='Erase all LDAP users before proceeding with the regeneration'),
- )
- def handle(self, *args, **options):
- if not settings.LDAP_ACTIVATE:
- self.stdout.write("LDAP disabled, not doing anything (check LDAP_ACTIVATE in settings.py).")
- return
- if len(args) == 0:
- members = Member.objects.all()
- else:
- members = Member.objects.filter(username__in=args)
- self.stdout.write("Regeneration of {count} members.".format(
- count=len(members)))
- # Delete every user is asked to do so
- if options['erase']:
- self.stdout.write("First erasing all existing LDAP users...")
- LdapUser.objects.all().delete()
- # Regenerate users
- for m in members:
- login = m.username
- if options['verbosity'] >= 2:
- self.stdout.write("Regenerating user {login}...".format(login=login))
- # The user might not exist in LDAP (maybe it was deleted or something)
- try:
- LdapUser.objects.get(pk=login)
- m.sync_to_ldap(creation=False, update_fields=None)
- except LdapUser.DoesNotExist:
- # Create the LDAP user
- self.stderr.write("WARNING: user {login} not found in LDAP, "
- "creating it (look at the resulting Unix "
- "uidNumber to see if it's ok).".format(login=login))
- m.sync_to_ldap(creation=True, update_fields=None)
- self.stdout.write("Done")
|