class AnyUserAuthBackend(ModelBackend):
"""DEBUG Only smart auth backend auto-create users."""
def authenticate(
self,
request: "HttpRequest | None",
username: str | None = None,
password: str | None = None,
**kwargs: Any,
) -> "AbstractBaseUser | None":
offices = Office.objects.values_list("slug", flat=True)
if settings.DEBUG:
if username in offices:
user, __ = get_user_model().objects.update_or_create(
username=username,
defaults={"is_staff": True, "is_active": True, "is_superuser": False},
)
office = Office.objects.get(slug=username)
g = Group.objects.get(name=settings.ANALYST_GROUP_NAME)
UserRole.objects.get_or_create(user=user, country_office=office, group=g)
return user
elif username in ["admin", "superuser", "administrator", "sax"]:
user, __ = get_user_model().objects.update_or_create(
username=username,
defaults={"is_staff": True, "is_active": True, "is_superuser": True},
)
return user
elif username in [
"staff",
]:
user, __ = get_user_model().objects.update_or_create(
username=username,
defaults={"is_staff": True, "is_active": True, "is_superuser": False},
)
return user
return None