Private
Public Access
The setup view's User.objects.exists() check and the admin account's save() weren't in the same transaction, so two concurrent requests during first deployment could both pass the check and both create an admin account. SetupLock is a singleton row (pk=1); claiming it and creating the admin now happen in one atomic block, so only the first of any racing requests can succeed — the rest hit IntegrityError and are sent to the login page instead. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
20 lines
833 B
Python
20 lines
833 B
Python
from django.contrib.auth.models import AbstractUser
|
|
from django.db import models
|
|
|
|
|
|
class User(AbstractUser):
|
|
timezone = models.CharField(max_length=64, default="UTC")
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
class SetupLock(models.Model):
|
|
"""Singleton row that gates first-run setup against a race between
|
|
concurrent requests. The setup view always tries to INSERT the row
|
|
with pk=1 in the same transaction as creating the first admin user:
|
|
the database's primary-key uniqueness makes exactly one of those
|
|
INSERTs win even under concurrent requests, so only one admin account
|
|
can ever be created this way, instead of the User.objects.exists()
|
|
check alone (which has a window between the check and the save)."""
|
|
|
|
id = models.PositiveSmallIntegerField(primary_key=True, default=1)
|