Merge pull request #5548 from FinnStutzenstein/createinitialuser

Create initial user command
This commit is contained in:
Finn Stutzenstein 2020-10-29 07:50:00 +01:00 committed by GitHub
commit 61c5f77d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 9 deletions

View File

@ -2,4 +2,4 @@
**/__pycache__/ **/__pycache__/
**/.venv **/.venv
tests/ tests/
personal_data/ personal_data/

View File

@ -69,8 +69,7 @@ if [[ -f /run/secrets/os_user ]]; then
# first_name, last_name, username, password, groups_id # first_name, last_name, username, password, groups_id
# email is optional # email is optional
# userid forces to to only create a user with this id, if it not exists before. # userid forces to to only create a user with this id, if it not exists before.
python manage.py createopenslidesuser \ python manage.py createinitialuser \
--userid 2 \
--email "${OPENSLIDES_USER_EMAIL:-}" \ --email "${OPENSLIDES_USER_EMAIL:-}" \
"${OPENSLIDES_USER_FIRSTNAME}" \ "${OPENSLIDES_USER_FIRSTNAME}" \
"${OPENSLIDES_USER_LASTNAME}" \ "${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 django.core.management.base import BaseCommand
from openslides.utils.autoupdate import inform_changed_data from openslides.utils.autoupdate import inform_changed_data
from openslides.utils.postgres import restart_id_sequence
from ...models import User from ...models import User
@ -38,22 +39,29 @@ class Command(BaseCommand):
"default_password": options["password"], "default_password": options["password"],
"email": options["email"] or "", "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: if userid is not None:
user_data["pk"] = userid user_data["pk"] = userid
user = User.objects.create_user( user = User.objects.create_user(
options["username"], username,
options["password"], options["password"],
**user_data, **user_data,
) )
if options["groups_id"].isdigit(): if options["groups_id"].isdigit():
user.groups.add(int(options["groups_id"])) user.groups.add(int(options["groups_id"]))
inform_changed_data(user) 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: else:
self.stdout.write( 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."
)
) )