#!/usr/bin/env python3 """Create or update a browser portal user (admin). Usage: python scripts/create_user.py Run inside the running container or with POSTGRES_* env vars set: docker compose exec server python scripts/create_user.py admin yourpassword """ import sys import os # Allow importing modules from the app root when run directly. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from modules import db from modules.user_auth import hash_password def main(): if len(sys.argv) != 3: print("Usage: create_user.py ", file=sys.stderr) sys.exit(1) username, password = sys.argv[1], sys.argv[2] if len(password) < 8: print("Password must be at least 8 characters.", file=sys.stderr) sys.exit(1) hashed = hash_password(password) # Try to create; if user already exists, update password. uid = db.create_user(username, hashed) if uid is None: # User exists – update password with db.get_cursor(commit=True) as cur: cur.execute( "UPDATE users SET password_hash = %s WHERE username = %s", (hashed, username), ) print(f"Updated password for user '{username}'.") else: print(f"Created user '{username}' (id={uid}).") if __name__ == "__main__": main()