File size: 1,942 Bytes
390c25a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a64b4
 
390c25a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a64b4
 
 
 
 
390c25a
 
 
 
 
c4a64b4
390c25a
 
c4a64b4
390c25a
c4a64b4
390c25a
 
 
 
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
#!/usr/bin/env python3
"""
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

# Add backend to path so app.auth is importable
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()