diff --git a/backend/apps/users/management/__init__.py b/backend/apps/users/management/__init__.py new file mode 100644 index 0000000..a94f2a3 --- /dev/null +++ b/backend/apps/users/management/__init__.py @@ -0,0 +1 @@ +# Management package diff --git a/backend/apps/users/management/__pycache__/__init__.cpython-314.pyc b/backend/apps/users/management/__pycache__/__init__.cpython-314.pyc new file mode 100644 index 0000000..65bcdd7 Binary files /dev/null and b/backend/apps/users/management/__pycache__/__init__.cpython-314.pyc differ diff --git a/backend/apps/users/management/commands/__init__.py b/backend/apps/users/management/commands/__init__.py new file mode 100644 index 0000000..b5a3a84 --- /dev/null +++ b/backend/apps/users/management/commands/__init__.py @@ -0,0 +1 @@ +# Commands package diff --git a/backend/apps/users/management/commands/__pycache__/__init__.cpython-314.pyc b/backend/apps/users/management/commands/__pycache__/__init__.cpython-314.pyc new file mode 100644 index 0000000..a729d6d Binary files /dev/null and b/backend/apps/users/management/commands/__pycache__/__init__.cpython-314.pyc differ diff --git a/backend/apps/users/management/commands/__pycache__/create_admin.cpython-314.pyc b/backend/apps/users/management/commands/__pycache__/create_admin.cpython-314.pyc new file mode 100644 index 0000000..9d7d1b7 Binary files /dev/null and b/backend/apps/users/management/commands/__pycache__/create_admin.cpython-314.pyc differ diff --git a/backend/apps/users/management/commands/create_admin.py b/backend/apps/users/management/commands/create_admin.py new file mode 100644 index 0000000..52d14d9 --- /dev/null +++ b/backend/apps/users/management/commands/create_admin.py @@ -0,0 +1,42 @@ +import os +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model + +User = get_user_model() + +class Command(BaseCommand): + help = "Erstellt einen initialen Admin-Benutzer (Superuser) für das Portal." + + def add_arguments(self, parser): + parser.add_argument('--username', type=str, default=os.getenv('ADMIN_USERNAME', 'admin'), help='Benutzername für den Admin') + parser.add_argument('--email', type=str, default=os.getenv('ADMIN_EMAIL', 'admin@beispiel.de'), help='E-Mail-Adresse für den Admin') + parser.add_argument('--password', type=str, default=os.getenv('ADMIN_PASSWORD', ''), help='Passwort für den Admin') + + def handle(self, *args, **options): + username = options['username'] + email = options['email'] + password = options['password'] + + if not password: + self.stderr.write(self.style.ERROR("Fehler: Bitte gib ein Passwort mit --password an oder setze ADMIN_PASSWORD als Umgebungsvariable.")) + return + + user, created = User.objects.get_or_create( + username=username, + defaults={'email': email} + ) + + user.email = email + user.set_password(password) + user.is_superuser = True + user.is_staff = True + user.is_admin_user = True + user.is_approved = True + user.is_email_verified = True + user.is_active = True + user.save() + + if created: + self.stdout.write(self.style.SUCCESS(f"✅ Initialer Admin '{username}' ({email}) wurde erfolgreich erstellt!")) + else: + self.stdout.write(self.style.SUCCESS(f"✅ Passwort & Admin-Rechte für bestehenden Benutzer '{username}' ({email}) wurden aktualisiert!"))