|
@@ -0,0 +1,55 @@
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+from __future__ import unicode_literals
|
|
|
+
|
|
|
+from optparse import make_option
|
|
|
+
|
|
|
+from django.core.management.base import BaseCommand, CommandError
|
|
|
+
|
|
|
+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 caution, as you will lose any user that was present in the
|
|
|
+ LDAP database but not in the local database."""
|
|
|
+
|
|
|
+ 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 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))
|
|
|
+ # Try deleting the LDAP user first, so that we can recreate it
|
|
|
+ try:
|
|
|
+ LdapUser.objects.get(pk=login).delete()
|
|
|
+ except LdapUser.DoesNotExist:
|
|
|
+ pass
|
|
|
+ # Create the LDAP user
|
|
|
+ m.sync_to_ldap(creation=True, update_fields=None)
|
|
|
+ self.stdout.write("Done")
|