Spaces:
Running

File size: 2,774 Bytes
77db408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Admin recovery script — run inside the container when an admin is locked out.

Usage (inside the Space/container, e.g. via `hf-space exec` or `fly ssh console`):

    # Reset the existing admin's password from the env var:
    python scripts/reset_admin.py

    # Or pass email + password explicitly (for one-off rescues):
    python scripts/reset_admin.py --email admin@example.com --password NEW-STRONG-PW

If the email doesn't exist, it's created as `admin` + `is_active=True`. If the
user exists with a different role, it's upgraded to `admin`. The password is
rehashed with the same bcrypt cost the app uses.
"""

from __future__ import annotations

import argparse
import os
import sys
from pathlib import Path

# Allow running this file directly from backend/scripts/ without a package install.
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

import models  # noqa: E402
from auth import MIN_BOOTSTRAP_PASSWORD_LEN, hash_password  # noqa: E402
from database import SessionLocal, engine  # noqa: E402


def main() -> int:
    parser = argparse.ArgumentParser(description="Reset or create the admin user.")
    parser.add_argument("--email", default=os.environ.get("BOOTSTRAP_ADMIN_EMAIL"))
    parser.add_argument("--password", default=os.environ.get("BOOTSTRAP_ADMIN_PASSWORD"))
    args = parser.parse_args()

    if not args.email or not args.password:
        print(
            "error: --email and --password are required (or set "
            "BOOTSTRAP_ADMIN_EMAIL and BOOTSTRAP_ADMIN_PASSWORD).",
            file=sys.stderr,
        )
        return 2

    if len(args.password) < MIN_BOOTSTRAP_PASSWORD_LEN:
        print(
            f"error: password must be at least {MIN_BOOTSTRAP_PASSWORD_LEN} characters.",
            file=sys.stderr,
        )
        return 2

    # Make sure the schema exists (covers a first boot where this is the only
    # entrypoint that ever touches the DB).
    models.Base.metadata.create_all(bind=engine)

    email = args.email.strip().lower()
    password_hash = hash_password(args.password)

    db = SessionLocal()
    try:
        user = db.query(models.User).filter(models.User.email == email).first()
        if user is None:
            user = models.User(
                email=email,
                password_hash=password_hash,
                role="admin",
                is_active=True,
            )
            db.add(user)
            action = "created"
        else:
            user.password_hash = password_hash
            user.role = "admin"
            user.is_active = True
            action = "reset"
        db.commit()
        print(f"Admin {action}: {email}")
        return 0
    finally:
        db.close()


if __name__ == "__main__":
    sys.exit(main())