Create initial user command

This commit is contained in:
Finn Stutzenstein 2020-09-07 12:17:46 +02:00
parent 266f9b73e9
commit 0956153ea4
No known key found for this signature in database
GPG Key ID: 9042F605C6324654
4 changed files with 52 additions and 9 deletions

View File

@ -69,8 +69,7 @@ if [[ -f /run/secrets/os_user ]]; then
# first_name, last_name, username, password, groups_id
# email is optional
# userid forces to to only create a user with this id, if it not exists before.
python manage.py createopenslidesuser \
--userid 2 \
python manage.py createinitialuser \
--email "${OPENSLIDES_USER_EMAIL:-}" \
"${OPENSLIDES_USER_FIRSTNAME}" \
"${OPENSLIDES_USER_LASTNAME}" \

View File

@ -0,0 +1,36 @@
from django.core.management.base import BaseCommand
from django.db import connection
from .createopenslidesuser import Command as CreateOpenslidesUser
class Command(BaseCommand):
"""
Command to create an OpenSlides user.
"""
help = "Creates an OpenSlides user with id=2 if no other user than the administrator were created before."
def add_arguments(self, parser):
parser.add_argument("first_name", help="The first name of the new user.")
parser.add_argument("last_name", help="The last name of the new user.")
parser.add_argument("username", help="The username of the new user.")
parser.add_argument("password", help="The password of the new user.")
parser.add_argument("groups_id", help="The group id of the new user.")
parser.add_argument("--email", help="The email address of the new user.")
def handle(self, *args, **options):
options["userid"] = 2
with connection.cursor() as cursor:
cursor.execute("SELECT last_value FROM users_user_id_seq;")
last_id = cursor.fetchone()[0]
if last_id > 1:
self.stdout.write(
self.style.NOTICE(
"There have users been created before. Do nothing."
)
)
return
CreateOpenslidesUser().handle(**options)

View File

@ -1,6 +1,7 @@
from django.core.management.base import BaseCommand
from openslides.utils.autoupdate import inform_changed_data
from openslides.utils.postgres import restart_id_sequence
from ...models import User
@ -38,22 +39,29 @@ class Command(BaseCommand):
"default_password": options["password"],
"email": options["email"] or "",
}
if userid is None or not User.objects.filter(pk=userid).exists():
username = options["username"]
if (
userid is None or not User.objects.filter(pk=userid).exists()
) and not User.objects.filter(username=username).exists():
if userid is not None:
user_data["pk"] = userid
user = User.objects.create_user(
options["username"],
username,
options["password"],
**user_data,
)
if options["groups_id"].isdigit():
user.groups.add(int(options["groups_id"]))
inform_changed_data(user)
self.stdout.write(
self.style.SUCCESS(f"Created user {options['username']}.")
)
if userid is not None:
restart_id_sequence("users_user")
self.stdout.write(self.style.SUCCESS(f"Created user {username}."))
else:
self.stdout.write(
self.style.NOTICE(f"A user with id {userid} already exists.")
self.style.NOTICE(
f"A user with id {userid} or username {username} already exists."
)
)