Seed and bootstrap users via user_login.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4f0abceb20
commit
4eed6b2e87
9 files changed
+65
-43
No files matched your search
@@ -7,7 +7,7 @@ from apps.accounts.models import ApiKey, User
|
||||
|
||||
def ensure_bootstrap_user(
|
||||
*,
|
||||
user_name: str,
|
||||
user_login: str,
|
||||
password: str,
|
||||
status: str,
|
||||
is_superuser: bool,
|
||||
@@ -15,8 +15,9 @@ def ensure_bootstrap_user(
|
||||
stdout,
|
||||
) -> User:
|
||||
user, created = User.objects.get_or_create(
|
||||
user_name=user_name,
|
||||
user_login=user_login,
|
||||
defaults={
|
||||
"user_name": user_login,
|
||||
"status": status,
|
||||
"is_superuser": is_superuser,
|
||||
},
|
||||
@@ -24,7 +25,7 @@ def ensure_bootstrap_user(
|
||||
|
||||
if not created and user.status != status:
|
||||
stdout.write(
|
||||
f"Updating {user_name!r} status {user.status!r} → {status!r}"
|
||||
f"Updating {user_login!r} status {user.status!r} → {status!r}"
|
||||
)
|
||||
|
||||
should_set_password = created or not user.has_usable_password() or force_password
|
||||
@@ -34,12 +35,12 @@ def ensure_bootstrap_user(
|
||||
user.is_superuser = is_superuser
|
||||
user.save()
|
||||
action = "Created" if created else "Updated password for"
|
||||
stdout.write(f"{action} {status} user {user_name}")
|
||||
stdout.write(f"{action} {status} user {user_login}")
|
||||
else:
|
||||
user.status = status
|
||||
user.is_superuser = is_superuser
|
||||
user.save(update_fields=["status", "is_superuser", "updated_at"])
|
||||
stdout.write(f"User {user_name} already exists (password unchanged)")
|
||||
stdout.write(f"User {user_login} already exists (password unchanged)")
|
||||
|
||||
return user
|
||||
|
||||
@@ -70,7 +71,7 @@ class Command(BaseCommand):
|
||||
)
|
||||
|
||||
admin = ensure_bootstrap_user(
|
||||
user_name=admin_user_name,
|
||||
user_login=admin_user_name,
|
||||
password=admin_password,
|
||||
status=User.STATUS_SUPERUSER,
|
||||
is_superuser=True,
|
||||
@@ -90,7 +91,7 @@ class Command(BaseCommand):
|
||||
"BOOTSTRAP_STAFF_PASSWORD is required when BOOTSTRAP_STAFF_USER is set."
|
||||
)
|
||||
ensure_bootstrap_user(
|
||||
user_name=staff_user_name,
|
||||
user_login=staff_user_name,
|
||||
password=staff_password,
|
||||
status=User.STATUS_GM,
|
||||
is_superuser=False,
|
||||
|
||||
Reference in new issue
Block a user