File size: 2,248 Bytes
b688fe8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """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())
|