Beispiel #1
0
    def handle(self, *args, **options):
        username = options['username']
        if username is None:
            raise CommandError("Username not specified.")

        try:
            user_properties = get_ldap_user_properties(username)
            if user_properties is None:
                self.stderr.write('LDAP user "%s" properties incomplete'
                                  % username)
                return
        except ldap.NO_SUCH_OBJECT:
            self.stderr.write("User %s does not exist in LDAP" % username)
            return

        try:
            user = User.objects.get(username=username)
            self.stderr.write('User "%s" exists, not overwriting' % username)
        except User.DoesNotExist:
            user = User.objects.create(username=username)
            user.email = user_properties.get("mail", "")
            user.last_name = user_properties.get("sn", "")
            user.first_name = user_properties.get("given_name", "")
            superuser_msg = ""
            if options["superuser"]:
                user.is_staff = True
                user.is_superuser = True
                superuser_msg = "with superuser status"
            user.save()
            self.stdout.write('User "%s" added %s' % (username, superuser_msg))
Beispiel #2
0
    def handle(self, *args, **options):
        lava_user = old_lava_user = options["lava_user"]
        if lava_user is None:
            raise CommandError("LAVA username not specified.")

        ldap_user = options["ldap_user"]
        if ldap_user is None:
            raise CommandError("LDAP username not specified.")

        try:
            ldap_user = User.objects.get(username=ldap_user)
            if ldap_user:
                self.stderr.write("LAVA user with the same ldap username ie., "
                                  '"%s", already exists, hence cannot merge' %
                                  ldap_user)
                return
        except User.DoesNotExist:
            self.stdout.write('LDAP user "%s", unavailable in LAVA, proceeding'
                              " with the merge ..." % ldap_user)

        try:
            user_properties = get_ldap_user_properties(ldap_user)
            if user_properties is None:
                self.stderr.write('LDAP user "%s" properties incomplete' %
                                  ldap_user)
                return
        except ldap.NO_SUCH_OBJECT:
            self.stderr.write("User %s does not exist in LDAP" % ldap_user)
            return
        try:
            lava_user = User.objects.get(username=lava_user)
            lava_user.username = ldap_user
            lava_user.email = user_properties.get("mail", "")
            # Grab max_length and truncate first and last name.
            # For some users, the command fail as first or last name is too long.
            first_name_max_length = User._meta.get_field(
                "first_name").max_length
            last_name_max_length = User._meta.get_field("last_name").max_length
            lava_user.last_name = truncatechars(user_properties.get("sn", ""),
                                                last_name_max_length)
            lava_user.first_name = truncatechars(
                user_properties.get("given_name", ""), first_name_max_length)
            lava_user.save()
            self.stdout.write('User "%s" merged with "%s"' %
                              (ldap_user, old_lava_user))
        except User.DoesNotExist:
            self.stderr.write('User "%s" does not exist in LAVA' % lava_user)
Beispiel #3
0
    def handle(self, *args, **options):
        lava_user = old_lava_user = options['lava_user']
        if lava_user is None:
            raise CommandError("LAVA username not specified.")

        ldap_user = options['ldap_user']
        if ldap_user is None:
            raise CommandError("LDAP username not specified.")

        try:
            ldap_user = User.objects.get(username=ldap_user)
            if ldap_user:
                self.stderr.write('LAVA user with the same ldap username ie., '
                                  '"%s", already exists, hence cannot merge' %
                                  ldap_user)
                return
        except User.DoesNotExist:
            self.stdout.write('LDAP user "%s", unavailable in LAVA, proceeding'
                              ' with the merge ...' % ldap_user)

        try:
            user_properties = get_ldap_user_properties(ldap_user)
            if user_properties is None:
                self.stderr.write('LDAP user "%s" properties incomplete' %
                                  ldap_user)
                return
        except ldap.NO_SUCH_OBJECT:
            self.stderr.write("User %s does not exist in LDAP" % ldap_user)
            return
        try:
            lava_user = User.objects.get(username=lava_user)
            lava_user.username = ldap_user
            lava_user.email = user_properties.get("mail", "")
            lava_user.last_name = user_properties.get("sn", "")
            lava_user.first_name = user_properties.get("given_name", "")
            lava_user.save()
            self.stdout.write('User "%s" merged with "%s"' %
                              (ldap_user, old_lava_user))
        except User.DoesNotExist:
            self.stderr.write('User "%s" does not exist in LAVA' % lava_user)
Beispiel #4
0
    def handle(self, *args, **options):
        lava_user = old_lava_user = options['lava_user']
        if lava_user is None:
            raise CommandError("LAVA username not specified.")

        ldap_user = options['ldap_user']
        if ldap_user is None:
            raise CommandError("LDAP username not specified.")

        try:
            ldap_user = User.objects.get(username=ldap_user)
            if ldap_user:
                self.stderr.write('LAVA user with the same ldap username ie., '
                                  '"%s", already exists, hence cannot merge' %
                                  ldap_user)
                return
        except User.DoesNotExist:
            self.stdout.write('LDAP user "%s", unavailable in LAVA, proceeding'
                              ' with the merge ...' % ldap_user)

        try:
            user_properties = get_ldap_user_properties(ldap_user)
            if user_properties is None:
                self.stderr.write('LDAP user "%s" properties incomplete'
                                  % ldap_user)
                return
        except ldap.NO_SUCH_OBJECT:
            self.stderr.write("User %s does not exist in LDAP" % ldap_user)
            return
        try:
            lava_user = User.objects.get(username=lava_user)
            lava_user.username = ldap_user
            lava_user.email = user_properties.get("mail", "")
            lava_user.last_name = user_properties.get("sn", "")
            lava_user.first_name = user_properties.get("given_name", "")
            lava_user.save()
            self.stdout.write('User "%s" merged with "%s"' % (ldap_user,
                                                              old_lava_user))
        except User.DoesNotExist:
            self.stderr.write('User "%s" does not exist in LAVA' % lava_user)
Beispiel #5
0
    def handle(self, *args, **options):
        username = options["username"]
        if username is None:
            raise CommandError("Username not specified.")

        try:
            user_properties = get_ldap_user_properties(username)
            if user_properties is None:
                self.stderr.write('LDAP user "%s" properties incomplete' %
                                  username)
                return
        except ldap.NO_SUCH_OBJECT:
            self.stderr.write("User %s does not exist in LDAP" % username)
            return

        try:
            user = User.objects.get(username=username)
            self.stderr.write('User "%s" exists, not overwriting' % username)
        except User.DoesNotExist:
            user = User.objects.create(username=username)
            user.email = user_properties.get("mail", "")
            # Grab max_length and truncate first and last name.
            # For some users, the command fail as first or last name is too long.
            first_name_max_length = User._meta.get_field(
                "first_name").max_length
            last_name_max_length = User._meta.get_field("last_name").max_length
            user.last_name = truncatechars(user_properties.get("sn", ""),
                                           last_name_max_length)
            user.first_name = truncatechars(
                user_properties.get("given_name", ""), first_name_max_length)
            superuser_msg = ""
            if options["superuser"]:
                user.is_staff = True
                user.is_superuser = True
                superuser_msg = "with superuser status"
            user.save()
            self.stdout.write('User "%s" added %s' % (username, superuser_msg))