From 3cb02583b75a16661c773480d29dd748ced4a9df Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 31 Jul 2026 10:33:31 +0200 Subject: [PATCH] feat: Add create_admin management command for easy initial admin user setup in Coolify/Docker --- backend/apps/users/management/__init__.py | 1 + .../__pycache__/__init__.cpython-314.pyc | Bin 0 -> 190 bytes .../users/management/commands/__init__.py | 1 + .../__pycache__/__init__.cpython-314.pyc | Bin 0 -> 199 bytes .../__pycache__/create_admin.cpython-314.pyc | Bin 0 -> 3153 bytes .../users/management/commands/create_admin.py | 42 ++++++++++++++++++ 6 files changed, 44 insertions(+) create mode 100644 backend/apps/users/management/__init__.py create mode 100644 backend/apps/users/management/__pycache__/__init__.cpython-314.pyc create mode 100644 backend/apps/users/management/commands/__init__.py create mode 100644 backend/apps/users/management/commands/__pycache__/__init__.cpython-314.pyc create mode 100644 backend/apps/users/management/commands/__pycache__/create_admin.cpython-314.pyc create mode 100644 backend/apps/users/management/commands/create_admin.py 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 0000000000000000000000000000000000000000..65bcdd73d6c7496837cd89ebe481aea5806fded5 GIT binary patch literal 190 zcmdPqS)xGtF^B^Lj8MjBHXx&cA&5blL6gyMB|{MtkpCGZc`I5!v^ce> zSU)8{xg@_x-z7h}G&eP`q*y;OuOu_QD6uTFq*6aGGp(djH$6W;B{3&QKPfReJ2fvw zKe3>oSicmkJ2x>eF+CNeQ9nLDGcU6wK3=b&@)n0pZhlH>PO4oIE6^^GYl=aPPt1&r Jj77{q769wuGF1Qo literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..a729d6d95ab1b1f959e4a227b2aa9d683e31ad34 GIT binary patch literal 199 zcmdPq zSU)8{xg@_x-z7h}G&eP`q*y;OuOu_QD6uTFq*6aGGp(djH$6W;B{3&QKPfReJ2fvw zKe3>oSicmkJ2x>eF+CNeQ9n697syK~){l?R%*!l^kJl@xyv1RYo1apelWJGQ3bYjD RtYQ%36EhMEqB3A0R|N==OD_fT7X$`UpqCse$cfk~0`vTM}es6xzmq{ULkMB9_Jb}>Pg%GuP$Q-{4%r1Hx=~4kPLFIyUTW0d4;A4f@ zZG|b5=$5;CE={9s;-5lX%1XTVhDq^FZ)3xBEfD)^ zm>E9B#MtmG>{LQ$0g*!C_#!YSgmVuuNk>e6JIP{$$b;aRqRT^w#q}8c6S~5ZCB#%c zE~ezka2kIe#xD@cu;VZd+b(uB+qIc(I>60Y8@4-p1G_%k!9=^T?AI|75oNXNA0H9T zGO1Sd2s52rMGhkpM1>C)sUDw$ICoJ=Dn{{k8R=-XJ!VyjI*O3?ryHbB(&a0mwI0)z ztD*hmclnM#hwp@*Z2MOK2j4x<;X9@Gwtc6&ed`(6hrXTE?5xPAYi?lPH#fgnSTa_Y z7xbmM#RVQWMf&|wNJq2a2>-j>^i z#ks=kJ7)r)*UQ+ZbsIZ53;W~Xe_?hJOlRjTf+>A^1`%Z4q;$(8Rup7$Zh8668~Qv) z{t%2Wwz}CJRy1EB*ksrU@{p@<^GtVqln;a#BX>eAP=US=zg^T+`;OGn19f!&^-t)d z8=n?zf~hMl zMy5+$rYGJ0{P5aajq*F-@=NvIV~7A%y6;jCC{Br+_B%)%+%#E7FP#%styYhLMmnKU zKqH^f#7pt>p?&QtLSv|eijjjZS`1;UOC>DZH@BlNCOdq@>9H$e+!37wK%7Ra0xN=6 z44^lXLS5#hETmFQmr6=r(&JY{MA!mKYIknA?RiUy;+bcRjCakQC?#Bxy`bqrYR?}? zy(jw8C!}^%fn{IR2eVE_e1}Fw0I(_<<=J{A%(K?9i@4{=^K>+(L-k}SmPWc-QsCcn zslDQ^l|$BH-?Yw{i2FZhPIb-P`&~0<(_C#MUc7}+YJ@I3<}JMDVDhqd!)6R?HM=Zo zlGcWo4V!5lFw#s{gUU!WilIJfEu4#xNi!X)t!&hA*>`Jn(t+W<>jnOz9(i^ z0PraUSZB89(jehMSVR;Xu80G`ldW(qDiC1i`!8vtkwIaQSc4Z3EVnNLFI6JeaHct=jC%-tY?ZrE~uTKp%)2Bap?ZInD>G6Z~_`db=`iJXB6S;$l z902ddWf_ok@=X~~II`Y_d0(?{yafjY8(;jud19Mkrc$LdIPcx+F3m}!nr3GrNWbW+ga=G2RV zPBcfxp}P;pe$exv=V9;Hz5UM&@#4Y6#m42^pAUbYYUnGC(p!y{)yC>}h*NQf|MOc>ZhL>WJO3{w z7JpCrcWN|#7nURb!4d-VY?AbYF(0+oO}FOdDjvbPPT|j$;Z03M>EdVG7On_0XZmc7 zi1I}QjEsnS_e_R*2>8%xb6}tQt%KbGtaF`t0RlZTC PrMvRp{cn+=ftUXRmt5<( literal 0 HcmV?d00001 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!"))