a working product with ugly ui
This commit is contained in:
93
scripts/create_admin.py
Normal file
93
scripts/create_admin.py
Normal file
@@ -0,0 +1,93 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script to create an admin user for the ISP Wiremap application.
|
||||
Usage: python scripts/create_admin.py
|
||||
"""
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add parent directory to path to import app modules
|
||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from app.database import SessionLocal
|
||||
from app.models.user import User
|
||||
from app.utils.password import hash_password
|
||||
import getpass
|
||||
|
||||
|
||||
def create_admin_user():
|
||||
"""Create an admin user interactively."""
|
||||
print("=" * 50)
|
||||
print("ISP Wiremap - Create Admin User")
|
||||
print("=" * 50)
|
||||
print()
|
||||
|
||||
# Get user input
|
||||
username = input("Enter admin username: ").strip()
|
||||
if not username:
|
||||
print("Error: Username cannot be empty")
|
||||
return
|
||||
|
||||
email = input("Enter admin email: ").strip()
|
||||
if not email:
|
||||
print("Error: Email cannot be empty")
|
||||
return
|
||||
|
||||
password = getpass.getpass("Enter admin password: ")
|
||||
if not password:
|
||||
print("Error: Password cannot be empty")
|
||||
return
|
||||
|
||||
password_confirm = getpass.getpass("Confirm password: ")
|
||||
if password != password_confirm:
|
||||
print("Error: Passwords do not match")
|
||||
return
|
||||
|
||||
# Create database session
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# Check if username already exists
|
||||
existing_user = db.query(User).filter(User.username == username).first()
|
||||
if existing_user:
|
||||
print(f"Error: Username '{username}' already exists")
|
||||
return
|
||||
|
||||
# Check if email already exists
|
||||
existing_email = db.query(User).filter(User.email == email).first()
|
||||
if existing_email:
|
||||
print(f"Error: Email '{email}' already exists")
|
||||
return
|
||||
|
||||
# Create admin user
|
||||
hashed_password = hash_password(password)
|
||||
admin_user = User(
|
||||
username=username,
|
||||
email=email,
|
||||
password_hash=hashed_password,
|
||||
is_admin=True
|
||||
)
|
||||
|
||||
db.add(admin_user)
|
||||
db.commit()
|
||||
db.refresh(admin_user)
|
||||
|
||||
print()
|
||||
print("=" * 50)
|
||||
print("Admin user created successfully!")
|
||||
print(f"Username: {admin_user.username}")
|
||||
print(f"Email: {admin_user.email}")
|
||||
print(f"User ID: {admin_user.id}")
|
||||
print(f"Is Admin: {admin_user.is_admin}")
|
||||
print("=" * 50)
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"Error creating admin user: {e}")
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_admin_user()
|
||||
Reference in New Issue
Block a user