Commit 1a7a7e81 authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

Add management commands for adding users without password/mail prompt

The `email` attribute is being synced from LDAP anyway and a password
prompt is usually not required as LDAP users normally use a custom
authentication backend.
parent abdf3988
Pipeline #3834 passed with stage
in 35 seconds
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from .ldap_createsuperuser import Command as SuperUserCommand
class Command(SuperUserCommand):
help = "Used to create a staff user without a usable password."
GRANT_SUPERUSER_PRIVILEGES = False
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import getpass
import sys
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
class Command(BaseCommand):
help = "Used to create a superuser without a usable password."
# This command is reused for creating staff-only users (ldap_createstaffuser),
# so this flag controls wether the superuser privilege is granted or not.
# The `is_staff` privilege is always granted.
GRANT_SUPERUSER_PRIVILEGES = True
def add_arguments(self, parser):
parser.add_argument('--username', dest='username', metavar='USERNAME', nargs='?', help='Specifies the login for the superuser.')
def handle(self, **options):
User = get_user_model()
#verbosity = options.get()
username = options.get('username')
if not username:
current_user = getpass.getuser()
sys.stdout.write('Username [%s]: ' %current_user)
input_user = sys.stdin.readline().strip()
if not input_user:
username = current_user
else:
username = input_user
obj, created = User.objects.get_or_create(username=username)
obj.is_staff = True
if self.GRANT_SUPERUSER_PRIVILEGES:
privilege = "superuser"
obj.is_superuser = True
else:
privilege = "staff"
if created:
obj.set_unusable_password()
obj.save()
self.stdout.write("Created user %s with %s privileges." % (username, privilege))
else:
obj.save()
self.stdout.write("Granted %s privileges to user %s." % (privilege, username))
Markdown is supported
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