Changeset 527c097 for uc_users.py
- Timestamp:
- 04/05/26 20:02:04 (3 months ago)
- Branches:
- main
- Parents:
- 95cf703
- File:
-
- 1 edited
-
uc_users.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uc_users.py
r95cf703 r527c097 3 3 import bcrypt 4 4 5 from db import get_connection, pick_from_list5 from db import get_connection, get_transaction, pick_from_list 6 6 7 7 … … 40 40 return 41 41 42 # Step 3: Check email uniqueness 43 with get_connection() as conn: 44 with conn.cursor() as cur: 45 cur.execute("SELECT COUNT(*) FROM users WHERE email = %s", (email,)) 46 exists = cur.fetchone()[0] 47 if exists > 0: 48 print(f"\n ERROR: A user with email '{email}' already exists.") 49 return 50 51 # Step 4: Enter password 42 # Step 3: Enter password 52 43 password = getpass.getpass(" Initial password: ").strip() 53 44 if not password: … … 59 50 return 60 51 61 # Step 5: Hash and insert52 # Step 4: Hash password 62 53 hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() 63 54 64 conn = get_connection() 55 # Step 5: Transaction — check email uniqueness + insert atomically 56 # This prevents a race condition where two users register with the 57 # same email simultaneously (one check passes, both insert). 65 58 try: 66 with conn.cursor() as cur: 67 cur.execute( 68 """ 69 INSERT INTO users (first_name, last_name, email, password, type_id) 70 VALUES (%s, %s, %s, %s, %s) 71 RETURNING user_id, first_name, last_name, email 72 """, 73 (first_name, last_name, email, hashed, type_id), 74 ) 75 result = cur.fetchone() 76 conn.commit() 59 with get_transaction() as conn: 60 with conn.cursor() as cur: 61 cur.execute("SELECT COUNT(*) FROM users WHERE email = %s", (email,)) 62 if cur.fetchone()[0] > 0: 63 raise ValueError(f"A user with email '{email}' already exists.") 77 64 78 # Step 6: Confirmation 65 cur.execute( 66 """ 67 INSERT INTO users (first_name, last_name, email, password, type_id) 68 VALUES (%s, %s, %s, %s, %s) 69 RETURNING user_id, first_name, last_name, email 70 """, 71 (first_name, last_name, email, hashed, type_id), 72 ) 73 result = cur.fetchone() 74 75 # Transaction committed automatically — Step 6: Confirmation 79 76 print(f"\n User created successfully!") 80 77 print(f" User ID: {result[0]}") … … 82 79 print(f" Email: {result[3]}") 83 80 print(f" Role: {role_name}") 81 except ValueError as e: 82 print(f"\n ERROR: {e}") 84 83 except Exception as e: 85 conn.rollback()86 84 print(f"\n Error creating user: {e}") 87 finally:88 conn.close()
Note:
See TracChangeset
for help on using the changeset viewer.
