update
This commit is contained in:
91
Backend/reset_user_passwords.py
Normal file
91
Backend/reset_user_passwords.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script to reset passwords for test users
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import bcrypt
|
||||
|
||||
# Add the src directory to the path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from src.config.database import SessionLocal
|
||||
from src.models.user import User
|
||||
from src.config.logging_config import setup_logging
|
||||
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
"""Hash password using bcrypt"""
|
||||
password_bytes = password.encode('utf-8')
|
||||
salt = bcrypt.gensalt()
|
||||
hashed = bcrypt.hashpw(password_bytes, salt)
|
||||
return hashed.decode('utf-8')
|
||||
|
||||
|
||||
def reset_password(db: Session, email: str, new_password: str) -> bool:
|
||||
"""Reset password for a user"""
|
||||
user = db.query(User).filter(User.email == email).first()
|
||||
|
||||
if not user:
|
||||
print(f"❌ User with email '{email}' not found")
|
||||
return False
|
||||
|
||||
# Hash new password
|
||||
hashed_password = hash_password(new_password)
|
||||
|
||||
# Update password
|
||||
user.password = hashed_password
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
|
||||
print(f"✅ Password reset for {email}")
|
||||
print(f" New password: {new_password}")
|
||||
print(f" Hash length: {len(user.password)} characters")
|
||||
print()
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
"""Reset passwords for all test users"""
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
print("="*80)
|
||||
print("RESETTING TEST USER PASSWORDS")
|
||||
print("="*80)
|
||||
print()
|
||||
|
||||
test_users = [
|
||||
{"email": "admin@hotel.com", "password": "admin123"},
|
||||
{"email": "staff@hotel.com", "password": "staff123"},
|
||||
{"email": "customer@hotel.com", "password": "customer123"},
|
||||
]
|
||||
|
||||
for user_data in test_users:
|
||||
reset_password(db, user_data["email"], user_data["password"])
|
||||
|
||||
print("="*80)
|
||||
print("SUMMARY")
|
||||
print("="*80)
|
||||
print("All test user passwords have been reset.")
|
||||
print("\nYou can now login with:")
|
||||
for user_data in test_users:
|
||||
print(f" {user_data['email']:<25} Password: {user_data['password']}")
|
||||
print()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error: {e}", exc_info=True)
|
||||
print(f"\n❌ Error: {e}")
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user