Files
Business-Management/backend/scripts/seed.py
T
curo1305 456681fdfa Add admin user management with role-gated access
Backend:
- schemas/user.py: is_admin (validation_alias=is_superuser) on UserOut and
  UserAdminOut; UserAdminCreate extends UserCreate with is_admin flag
- deps.py: get_current_admin dependency — 403 for non-superusers
- routers/admin.py: GET/POST /api/admin/users, DELETE and PATCH /active per
  user; self-delete and self-deactivate blocked
- main.py: register /api/admin router
- scripts/seed.py: seed test user with is_superuser=True; promotes existing
  user if already created without the flag

Frontend:
- api/client.ts: UserData type with is_admin, admin API functions
- components/Nav.tsx: Admin link visible only when user.is_admin is true
- pages/AdminPage.tsx: user table with add-user form, delete, toggle active
- App.tsx: AdminRoute guard (403-redirects non-admins to /); /admin route

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 18:40:05 +02:00

44 lines
1.3 KiB
Python

"""Create a test user for the dev environment if it doesn't exist yet."""
import asyncio
from sqlalchemy import select
from app.core.security import hash_password
from app.database import AsyncSessionLocal
from app.models.user import User
TEST_EMAIL = "test@example.com"
TEST_PASSWORD = "Test123!"
TEST_NAME = "Test User"
async def seed() -> None:
async with AsyncSessionLocal() as db:
result = await db.execute(select(User).where(User.email == TEST_EMAIL))
existing = result.scalar_one_or_none()
if existing:
# Ensure the dev test user is always an admin
if not existing.is_superuser:
existing.is_superuser = True
await db.commit()
print(f"[seed] promoted test user to admin: {TEST_EMAIL}")
else:
print(f"[seed] test user already exists: {TEST_EMAIL}")
return
user = User(
email=TEST_EMAIL,
hashed_password=hash_password(TEST_PASSWORD),
full_name=TEST_NAME,
is_superuser=True,
)
db.add(user)
await db.commit()
print(f"[seed] created test admin — email: {TEST_EMAIL} pwd: {TEST_PASSWORD}")
if __name__ == "__main__":
asyncio.run(seed())