-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_local_password.py
More file actions
134 lines (105 loc) · 3.77 KB
/
reset_local_password.py
File metadata and controls
134 lines (105 loc) · 3.77 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
"""
Reset password for specific users in the local PostgreSQL database.
This is for LOCAL DEVELOPMENT ONLY - do not use in production!
"""
import psycopg2
import bcrypt
import sys
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def reset_password(username, new_password):
"""Reset password for a specific user."""
# Get database connection from environment
db_url = os.getenv('DATABASE_URL', 'postgresql://postgres:postgres@localhost:5432/pythonide')
try:
# Connect to database
conn = psycopg2.connect(db_url)
cur = conn.cursor()
# Hash the new password
password_hash = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
# Update the password
cur.execute(
"UPDATE users SET password_hash = %s WHERE username = %s",
(password_hash, username)
)
if cur.rowcount == 0:
print(f"❌ User '{username}' not found")
return False
conn.commit()
print(f"✅ Password reset successfully for user: {username}")
return True
except Exception as e:
print(f"❌ Error resetting password: {e}")
return False
finally:
if conn:
cur.close()
conn.close()
def list_users():
"""List all users in the database."""
db_url = os.getenv('DATABASE_URL', 'postgresql://postgres:postgres@localhost:5432/pythonide')
try:
conn = psycopg2.connect(db_url)
cur = conn.cursor()
cur.execute("SELECT username, role FROM users ORDER BY role, username")
users = cur.fetchall()
print("\n📋 Current users in database:")
print("-" * 40)
current_role = None
for username, role in users:
if role != current_role:
current_role = role
print(f"\n{role.upper()}S:")
print(f" - {username}")
print("-" * 40)
except Exception as e:
print(f"❌ Error listing users: {e}")
finally:
if conn:
cur.close()
conn.close()
def main():
print("🔐 Local Password Reset Tool")
print("=" * 40)
print("⚠️ This is for LOCAL DEVELOPMENT ONLY!")
print("=" * 40)
# List current users
list_users()
while True:
print("\nOptions:")
print("1. Reset password for a user")
print("2. Reset password for admin (sa9082)")
print("3. Reset password for test student")
print("4. Exit")
choice = input("\nEnter your choice (1-4): ").strip()
if choice == '1':
username = input("Enter username to reset: ").strip()
password = input("Enter new password: ").strip()
if not password:
print("❌ Password cannot be empty")
continue
reset_password(username, password)
elif choice == '2':
# Quick reset for your admin account
password = input("Enter new password for sa9082 (or press Enter for 'password'): ").strip()
if not password:
password = 'password'
reset_password('sa9082', password)
print(f"📝 You can now login with: sa9082 / {password}")
elif choice == '3':
# Reset a test student account
password = input("Enter new password for test_1 (or press Enter for 'password'): ").strip()
if not password:
password = 'password'
reset_password('test_1', password)
print(f"📝 You can now login with: test_1 / {password}")
elif choice == '4':
print("Goodbye!")
break
else:
print("❌ Invalid choice")
if __name__ == "__main__":
main()