| |
| """ |
| Create a new user for Amalfa Ad Creative Studio. |
| Run from repo root: python backend/scripts/create_user.py --username admin --password yourpassword |
| Or from backend: python scripts/create_user.py --username admin --password yourpassword |
| """ |
|
|
| import argparse |
| import sys |
| from pathlib import Path |
|
|
| |
| backend_dir = Path(__file__).resolve().parent.parent |
| sys.path.insert(0, str(backend_dir)) |
|
|
| from app.auth import get_user_by_username, hash_password |
| from app.mongo import get_mongo_db |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Create a new Amalfa user") |
| parser.add_argument("--username", "-u", required=True, help="Username") |
| parser.add_argument("--password", "-p", required=True, help="Password") |
| args = parser.parse_args() |
|
|
| username = (args.username or "").strip() |
| password = (args.password or "").strip() |
|
|
| if not username: |
| print("Error: username is required and cannot be empty.", file=sys.stderr) |
| sys.exit(1) |
| if not password: |
| print("Error: password is required and cannot be empty.", file=sys.stderr) |
| sys.exit(1) |
|
|
| if get_user_by_username(username): |
| print(f"Error: User '{username}' already exists.", file=sys.stderr) |
| sys.exit(1) |
|
|
| db = get_mongo_db() |
| if db is None: |
| print("Error: MongoDB is not configured. Set MONGODB_URL and MONGODB_DB_NAME.", file=sys.stderr) |
| sys.exit(1) |
| users = list(db["users"].find({}, {"_id": 0})) |
|
|
| next_id = max((u.get("id") or 0) for u in users) + 1 if users else 1 |
| new_user = { |
| "id": next_id, |
| "username": username, |
| "username_lower": username.lower(), |
| "password_hash": hash_password(password), |
| } |
| db["users"].insert_one(new_user) |
| print(f"User '{username}' created successfully (id={next_id}).") |
| print("Storage: MongoDB") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|