feat(auth): automatically assign admin role to the first registered user

This commit is contained in:
2026-06-12 23:46:20 +08:00
parent cdc60dc1d1
commit 96837fd868
2 changed files with 19 additions and 1 deletions
+12
View File
@@ -216,6 +216,18 @@ async def list_users(
return list(result.scalars().all())
async def count_users(
session: AsyncSession | None = None,
) -> int:
"""Return the total number of users (including inactive)."""
if session is None:
async with _session_factory() as session:
result = await session.execute(select(User))
return len(result.scalars().all())
result = await session.execute(select(User))
return len(result.scalars().all())
async def update_user(
user_id: uuid.UUID,
session: AsyncSession,
+7 -1
View File
@@ -91,11 +91,17 @@ async def register_endpoint(
detail=f"User '{body.username}' already exists",
)
# First user to register becomes an admin automatically.
from backend.core.database import count_users
total_users = await count_users(session=session)
role = "admin" if total_users == 0 else body.role
user = await create_user_with_password(
username=body.username,
display_name=body.display_name,
password=body.password,
role=body.role,
role=role,
team=body.team,
session=session,
)