maybe good
This commit is contained in:
75
create_admin.py
Normal file
75
create_admin.py
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Create initial admin user for Delphi Consulting Group Database System
|
||||
"""
|
||||
import sys
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from app.database.base import engine
|
||||
from app.models import User
|
||||
from app.auth.security import get_password_hash
|
||||
|
||||
|
||||
def create_admin_user():
|
||||
"""Create the initial admin user"""
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# Check if admin user already exists
|
||||
existing_admin = db.query(User).filter(User.username == "admin").first()
|
||||
if existing_admin:
|
||||
print("Admin user already exists!")
|
||||
return
|
||||
|
||||
# Get admin credentials
|
||||
print("Creating initial admin user...")
|
||||
username = input("Admin username (default: admin): ").strip() or "admin"
|
||||
email = input("Admin email: ").strip()
|
||||
|
||||
while not email:
|
||||
print("Email is required!")
|
||||
email = input("Admin email: ").strip()
|
||||
|
||||
full_name = input("Full name (default: System Administrator): ").strip() or "System Administrator"
|
||||
|
||||
import getpass
|
||||
password = getpass.getpass("Admin password: ")
|
||||
|
||||
while len(password) < 6:
|
||||
print("Password must be at least 6 characters long!")
|
||||
password = getpass.getpass("Admin password: ")
|
||||
|
||||
confirm_password = getpass.getpass("Confirm password: ")
|
||||
|
||||
if password != confirm_password:
|
||||
print("Passwords don't match!")
|
||||
return
|
||||
|
||||
# Create admin user
|
||||
admin_user = User(
|
||||
username=username,
|
||||
email=email,
|
||||
full_name=full_name,
|
||||
hashed_password=get_password_hash(password),
|
||||
is_active=True,
|
||||
is_admin=True
|
||||
)
|
||||
|
||||
db.add(admin_user)
|
||||
db.commit()
|
||||
|
||||
print(f"\nAdmin user '{username}' created successfully!")
|
||||
print(f"Email: {email}")
|
||||
print(f"Full name: {full_name}")
|
||||
print("\nYou can now start the application with:")
|
||||
print("python -m uvicorn app.main:app --reload")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error creating admin user: {e}")
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_admin_user()
|
||||
Reference in New Issue
Block a user