Spaces:
Running
Running
Andre
deploy: snapshot c96b9ab -- Inventory Journals (Lagerforandringsjournal) na Contabilidade (v1.3.61)
77db408 Download backend/scripts/reset_admin.py from inwave-gateway/hub: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/inwave-gateway/hub/resolve/main/backend/scripts/reset_admin.py
- Command line
-
hf download hf://spaces/inwave-gateway/hub/backend/scripts/reset_admin.py
-
curl -L -o reset_admin.py https://huggingface.co/spaces/inwave-gateway/hub/resolve/main/backend/scripts/reset_admin.py
2.77 kB
| """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()) | |