Files
item-code/reset_admin_password.py
2026-10-13 16:00:15 +05:30

92 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""
Reset a user's password directly in users.db.
Run this from the same folder as users.db (and app_new_updated.py).
python3 reset_admin_password.py
"""
import sqlite3
import sys
import getpass
from werkzeug.security import generate_password_hash
DATABASE = 'users.db'
def get_conn():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
def list_users():
conn = get_conn()
cursor = conn.cursor()
cursor.execute("SELECT username, is_admin FROM users ORDER BY username")
users = cursor.fetchall()
conn.close()
if not users:
print("❌ No users found in database.")
return None
print("\n📋 Current Users:")
print("-" * 40)
for u in users:
tag = "👑 ADMIN" if u['is_admin'] else "👤 User"
print(f" {u['username']:20} | {tag}")
print("-" * 40)
return users
def reset_password(username, new_password):
conn = get_conn()
cursor = conn.cursor()
cursor.execute("SELECT id FROM users WHERE username = ?", (username,))
if not cursor.fetchone():
print(f"❌ User '{username}' not found.")
conn.close()
return False
hashed = generate_password_hash(new_password)
cursor.execute("UPDATE users SET password = ? WHERE username = ?", (hashed, username))
conn.commit()
conn.close()
print(f"✅ Password for '{username}' has been reset.")
return True
def main():
print("=" * 60)
print(" 🔑 PASSWORD RESET")
print("=" * 60)
users = list_users()
if not users:
sys.exit(1)
username = input("\nEnter the username to reset (or 'q' to quit): ").strip()
if username.lower() == 'q' or not username:
print("Cancelled.")
return
while True:
new_password = getpass.getpass("New password (min 6 characters, hidden as you type): ")
if len(new_password) < 6:
print("❌ Password must be at least 6 characters. Try again.")
continue
confirm = getpass.getpass("Confirm new password: ")
if new_password != confirm:
print("❌ Passwords don't match. Try again.")
continue
break
if reset_password(username, new_password):
print(f"\n✅ Done. Log in as '{username}' with the new password.")
if __name__ == '__main__':
main()