"""Promote an account to administrator. The first admin has to be made from outside the product - the dashboard that grants the role is itself behind the role. After that, admins promote each other from the users table. python scripts/make_admin.py you@example.com python scripts/make_admin.py --list """ from __future__ import annotations import argparse import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from sqlalchemy import create_engine, select # noqa: E402 from sqlalchemy.orm import Session # noqa: E402 from app.core.config import settings # noqa: E402 from app.db.models import User, UserRole # noqa: E402 def main() -> int: ap = argparse.ArgumentParser(description=__doc__) ap.add_argument("email", nargs="?", help="account to promote") ap.add_argument("--demote", action="store_true", help="make them a normal user instead") ap.add_argument("--list", action="store_true", dest="list_only") args = ap.parse_args() engine = create_engine(settings.sync_database_url, future=True) with Session(engine, future=True) as db: if args.list_only or not args.email: rows = db.scalars(select(User).order_by(User.created_at)).all() if not rows: print("No accounts yet. Sign up in the web app first, then run this again.") return 0 print(f"{len(rows)} account(s):\n") for p in rows: mark = "ADMIN" if p.role == UserRole.admin else " " print(f" {mark} {p.email:38s} {p.status.value}") return 0 email = args.email.strip().lower() user_account = db.scalar(select(User).where(User.email == email)) if user_account is None: print(f"No account for {email}.") print("Sign in through the web app once - the account row is created on first call.") return 1 user_account.role = UserRole.user if args.demote else UserRole.admin db.commit() print(f"{email} is now {user_account.role.value}.") print("Sign out and back in for the dashboard link to appear.") return 0 if __name__ == "__main__": raise SystemExit(main())