1+ #!/usr/bin/env python3
2+ """
3+ Emergency script to restore users directly on AWS without CSV file
4+ """
5+ import os
6+ import sys
7+ import bcrypt
8+ import hashlib
9+ import string
10+ import secrets
11+
12+ # Add server directory to path
13+ sys .path .append (os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'server' ))
14+
15+ from common .database import db_manager
16+ from common .file_storage import file_storage
17+
18+ def generate_consistent_password (username , environment = "production" ):
19+ """Generate deterministic password based on username and environment"""
20+ secret_seed = "PythonIDE2025SecureClassroom"
21+ hash_input = f"{ username } _{ secret_seed } _{ environment } " .encode ()
22+ hash_digest = hashlib .sha256 (hash_input ).hexdigest ()
23+
24+ # Convert hash to readable password format
25+ chars = []
26+ chars .append (hash_digest [0 ].upper () if hash_digest [0 ].isalpha () else 'X' )
27+ chars .append (hash_digest [1 :3 ])
28+ chars .append (hash_digest [3 ].upper () if hash_digest [3 ].isalpha () else 'R' )
29+ chars .append (str (sum (ord (c ) for c in hash_digest [:4 ]) % 10 ))
30+ chars .append (hash_digest [4 :6 ])
31+ chars .append (hash_digest [6 ].upper () if hash_digest [6 ].isalpha () else 'Q' )
32+ chars .append (hash_digest [7 :9 ])
33+ chars .append (str (sum (ord (c ) for c in hash_digest [6 :10 ]) % 10 ))
34+
35+ symbols = ['!' , '@' , '#' , '$' , '%' , '&' , '*' ]
36+ symbol_index = sum (ord (c ) for c in hash_digest [:8 ]) % len (symbols )
37+ chars .append (symbols [symbol_index ])
38+
39+ return '' .join (str (c ) for c in chars )
40+
41+ def create_user (username , password , role = 'student' ):
42+ """Create a user with hashed password"""
43+ try :
44+ # Hash the password
45+ password_hash = bcrypt .hashpw (password .encode ('utf-8' ), bcrypt .gensalt ())
46+
47+ # Generate email
48+ email = f"{ username } @college.edu"
49+
50+ # Insert user into database
51+ query = """
52+ INSERT INTO users (username, email, password_hash, role)
53+ VALUES (%s, %s, %s, %s)
54+ ON CONFLICT (username) DO UPDATE
55+ SET password_hash = EXCLUDED.password_hash, role = EXCLUDED.role, email = EXCLUDED.email
56+ """
57+ db_manager .execute_query (query , (username , email , password_hash .decode ('utf-8' ), role ))
58+
59+ # Directory creation handled by AWS container
60+ # user_dir = file_storage.ensure_user_directory(username)
61+
62+ return True
63+ except Exception as e :
64+ print (f"Error creating user { username } : { e } " )
65+ return False
66+
67+ def main ():
68+ print ("=" * 60 )
69+ print ("EMERGENCY USER RESTORATION" )
70+ print ("=" * 60 )
71+
72+ # Hardcoded user list since CSV is missing
73+ users = [
74+ # Admin accounts
75+ ('admin_editor' , 'XuR0ibQqhw6#' , 'professor' ),
76+ ('sa9082' , 'pXzwjLIYE20*' , 'professor' ),
77+ ('sl7927' , '4qPg1cmJkUa!' , 'professor' ),
78+ ('et2434' , 'evaTQRwfyhC*' , 'professor' ),
79+
80+ # Test accounts
81+ ('admin_viewer' , 'AdminView2025!' , 'student' ),
82+ ('test_student' , 'TestStudent2025!' , 'student' ),
83+ ('test_admin' , 'TestAdmin2025!' , 'professor' ),
84+
85+ # Student accounts
86+ ('ag11389' , 'X4fD1caEa39*' , 'student' ),
87+ ('agr8457' , 'Aab907Q4b4*' , 'student' ),
88+ ('ap10062' , 'X97807Qc97#' , 'student' ),
89+ ('arg9667' , 'B0dC9d3Fc04&' , 'student' ),
90+ ('arm9283' , 'Xd9303Q7a9&' , 'student' ),
91+ ('as19217' , 'BebD848Ed89!' , 'student' ),
92+ ('bap9618' , 'Xc2F909Dfc5&' , 'student' ),
93+ ('bsj5539' , 'B01E5afBe07*' , 'student' ),
94+ ('ch5315' , 'X96B5d0Q965%' , 'student' ),
95+ ('dl8926' , 'Fc0Q054E2c4*' , 'student' ),
96+ ('dl9362' , 'A23B2b5E829@' , 'student' ),
97+ ('ecs8863' , 'Dba805B8a5!' , 'student' ),
98+ ('ee7513' , 'F52E8a2Cb23!' , 'student' ),
99+ ('hr6456' , 'D88F71bEa80@' , 'student' ),
100+ ('jd7852' , 'Xb7F500A4b9$' , 'student' ),
101+ ('lp7436' , 'Be4D8e4B5e2&' , 'student' ),
102+ ('mm17329' , 'Da3D5edFe37#' , 'student' ),
103+ ('nyk5356' , 'F78F0ebE5f2#' , 'student' ),
104+ ('pm7841' , 'C47Q638Cc49*' , 'student' ),
105+ ('pp8019' , 'Eb4A066A2b5@' , 'student' ),
106+ ('ps10497' , 'C02E0daF406*' , 'student' ),
107+ ('pvd5683' , 'Ab3806Be37!' , 'student' ),
108+ ('rm11092' , 'X26D0fbQe25&' , 'student' ),
109+ ('rmm9894' , 'Xb6F0e8Ce76@' , 'student' ),
110+ ('sg12493' , 'X51803Da52&' , 'student' ),
111+ ('sr10641' , 'Ab0R4c7Ed04$' , 'student' ),
112+ ('ss18846' , 'C56Q855D951!' , 'student' ),
113+ ('ss19618' , 'C16F5f5Qd14&' , 'student' ),
114+ ('sw10013' , 'Be6D7ceEf64$' , 'student' ),
115+ ('sz4766' , 'X88B923Cab1%' , 'student' ),
116+ ('vn5684' , 'A29D3e9Ae20!' , 'student' ),
117+ ('vt5675' , 'A43D1c1D141@' , 'student' ),
118+ ('xl6213' , 'X59841F653!' , 'student' ),
119+ ('xs7043' , 'E5cBe09Xed0*' , 'student' ),
120+ ('zm2525' , 'C48E8beAd47&' , 'student' ),
121+ ]
122+
123+ success_count = 0
124+ for username , password , role in users :
125+ if create_user (username , password , role ):
126+ print (f"✓ Created { role } : { username } " )
127+ success_count += 1
128+ else :
129+ print (f"✗ Failed to create: { username } " )
130+
131+ print ("\n " + "=" * 60 )
132+ print (f"✅ Restored { success_count } /{ len (users )} users" )
133+ print ("\n Test with:" )
134+ print (" admin_editor / XuR0ibQqhw6#" )
135+ print (" URL: http://pythonide-alb-456687384.us-east-2.elb.amazonaws.com" )
136+ print ("=" * 60 )
137+
138+ if __name__ == "__main__" :
139+ main ()
0 commit comments