fix(users): require mobile for superuser creation
Some checks failed
Backend CI/CD / test (push) Has been cancelled
Backend CI/CD / deploy (push) Has been cancelled

This commit is contained in:
2026-06-11 21:20:59 +03:30
parent 13ea129d3a
commit 41f9be4c7e
3 changed files with 49 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractUser, UserManager as DjangoUserManager
from django.utils import timezone
from django.db import models
@@ -14,6 +14,24 @@ from core.models import BaseModel
from apps.users.email_identity import normalize_email_identity, normalize_mobile_number
class UserManager(DjangoUserManager):
def _normalize_required_mobile(self, mobile):
normalized = normalize_mobile_number(mobile)
if not normalized:
raise ValueError("The mobile number must be set")
return normalized
def create_user(self, username, email=None, password=None, **extra_fields):
extra_fields["mobile"] = self._normalize_required_mobile(extra_fields.get("mobile"))
return super().create_user(username, email=email, password=password, **extra_fields)
def create_superuser(self, username, email=None, password=None, **extra_fields):
extra_fields["mobile"] = self._normalize_required_mobile(extra_fields.get("mobile"))
extra_fields.setdefault("is_active", True)
extra_fields.setdefault("is_mobile_verified", True)
return super().create_superuser(username, email=email, password=password, **extra_fields)
class University(BaseModel):
code = models.CharField(max_length=64, unique=True)
name = models.CharField(max_length=255)
@@ -69,7 +87,9 @@ class User(AbstractUser, BaseModel):
password_reset_token_expires_at = models.DateTimeField(null=True, blank=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
REQUIRED_FIELDS = ['mobile']
objects = UserManager()
class Meta:
db_table = 'users'