Files
space-booking/backend/seed_db.py
Claude Agent 7ce430cc1d feat(security): harden for production deployment
- auth: first registered user becomes superadmin (active immediately)
- entrypoint: no longer seeds demo data in prod (opt-in via RUN_SEED=1)
- config: refuse to boot in prod with weak/placeholder SECRET_KEY (<32 chars)
- main: restrict CORS to FRONTEND_URL only in prod (localhost dev-only)
- seed_db: block prod seeding, read passwords from env, stop printing them
- login: remove demo account credentials from UI

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 19:44:20 +00:00

194 lines
6.3 KiB
Python

"""Seed database with initial data for multi-property system."""
import os
from app.core.config import settings
from app.core.security import get_password_hash
from app.db.session import Base, SessionLocal, engine
from app.models.organization import Organization
from app.models.organization_member import OrganizationMember
from app.models.property import Property
from app.models.property_access import PropertyAccess
from app.models.property_manager import PropertyManager
from app.models.settings import Settings
from app.models.space import Space
from app.models.user import User
def seed_database() -> None:
"""Create initial data for testing multi-property system."""
# Safety guard: refuse to plant demo accounts in production unless the
# operator explicitly opts in AND supplies non-default passwords via env.
if not settings.debug and os.getenv("ALLOW_PROD_SEED") != "1":
raise SystemExit(
"Refusing to seed in production (DEBUG=false). "
"Demo accounts use weak, public passwords. "
"Set ALLOW_PROD_SEED=1 and override ADMIN_PASSWORD/MANAGER_PASSWORD/USER_PASSWORD "
"if you really mean to."
)
admin_password = os.getenv("ADMIN_PASSWORD", "adminpassword")
manager_password = os.getenv("MANAGER_PASSWORD", "managerpassword")
user_password = os.getenv("USER_PASSWORD", "userpassword")
# Create tables
Base.metadata.create_all(bind=engine)
db = SessionLocal()
try:
# Check if users already exist
existing_admin = db.query(User).filter(User.email == "admin@example.com").first()
if existing_admin:
print("Database already seeded. Skipping...")
return
# Create superadmin user
superadmin = User(
email="admin@example.com",
full_name="Super Admin",
hashed_password=get_password_hash(admin_password),
role="superadmin",
organization="Management",
is_active=True,
)
db.add(superadmin)
# Create manager user
manager = User(
email="manager@example.com",
full_name="Property Manager",
hashed_password=get_password_hash(manager_password),
role="manager",
organization="Management",
is_active=True,
)
db.add(manager)
# Create regular user
user = User(
email="user@example.com",
full_name="Regular User",
hashed_password=get_password_hash(user_password),
role="user",
organization="Engineering",
is_active=True,
)
db.add(user)
db.flush() # Get IDs
# Create properties
prop1 = Property(
name="Clădirea Centrală",
description="Clădirea principală din centru",
address="Str. Principală nr. 1",
is_public=True,
is_active=True,
)
db.add(prop1)
prop2 = Property(
name="Biroul Privat",
description="Spațiu privat pentru echipă",
address="Str. Secundară nr. 5",
is_public=False,
is_active=True,
)
db.add(prop2)
db.flush() # Get property IDs
# Assign manager to both properties
db.add(PropertyManager(property_id=prop1.id, user_id=manager.id))
db.add(PropertyManager(property_id=prop2.id, user_id=manager.id))
# Create spaces (2 in first property, 1 in second)
space1 = Space(
name="Sala Mare",
type="sala",
capacity=20,
description="Sală de conferințe mare",
is_active=True,
property_id=prop1.id,
)
db.add(space1)
space2 = Space(
name="Birou A1",
type="birou",
capacity=4,
description="Birou deschis",
is_active=True,
property_id=prop1.id,
)
db.add(space2)
space3 = Space(
name="Sala Privată",
type="sala",
capacity=10,
description="Sală privată pentru echipă",
is_active=True,
property_id=prop2.id,
)
db.add(space3)
# Create organizations
org1 = Organization(
name="Engineering",
description="Echipa de dezvoltare",
is_active=True,
)
db.add(org1)
org2 = Organization(
name="Management",
description="Echipa de management",
is_active=True,
)
db.add(org2)
db.flush() # Get org IDs
# Create organization members
db.add(OrganizationMember(organization_id=org1.id, user_id=user.id, role="member"))
db.add(OrganizationMember(organization_id=org2.id, user_id=manager.id, role="admin"))
db.add(OrganizationMember(organization_id=org2.id, user_id=superadmin.id, role="admin"))
# Grant user access to private property
db.add(PropertyAccess(
property_id=prop2.id,
user_id=user.id,
granted_by=manager.id,
))
# Create default settings if not exist
existing_settings = db.query(Settings).filter(Settings.id == 1).first()
if not existing_settings:
default_settings = Settings(
id=1,
min_duration_minutes=30,
max_duration_minutes=480, # 8 hours
working_hours_start=8, # 8 AM
working_hours_end=20, # 8 PM
max_bookings_per_day_per_user=3,
min_hours_before_cancel=2,
)
db.add(default_settings)
db.commit()
print("Database seeded successfully!")
print("Superadmin: admin@example.com (password from ADMIN_PASSWORD env)")
print("Manager: manager@example.com (password from MANAGER_PASSWORD env)")
print("User: user@example.com (password from USER_PASSWORD env)")
print(f"Properties: '{prop1.name}' (public), '{prop2.name}' (private)")
print(f"Organizations: '{org1.name}', '{org2.name}'")
except Exception as e:
print(f"Error seeding database: {e}")
db.rollback()
finally:
db.close()
if __name__ == "__main__":
seed_database()