79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Create admin user script for ScriptsManager
|
|
Creates the default admin user account
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add the project root to the Python path
|
|
project_root = Path(__file__).parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
from app import create_app
|
|
from app.config.database import db
|
|
from app.models.user import User, UserRole
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
|
|
def create_admin():
|
|
"""Create the default admin user"""
|
|
print("Creating admin user for ScriptsManager...")
|
|
|
|
# Create application instance
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
try:
|
|
# Ensure database tables exist
|
|
db.create_all()
|
|
|
|
# Check if admin user already exists
|
|
admin_user = User.query.filter_by(username="admin").first()
|
|
if admin_user:
|
|
print("Admin user already exists!")
|
|
print(f"Username: {admin_user.username}")
|
|
print(f"Email: {admin_user.email}")
|
|
|
|
# Ask if user wants to reset password
|
|
response = input("Reset admin password? (y/N): ")
|
|
if response.lower() in ["y", "yes"]:
|
|
admin_user.set_password("admin123")
|
|
db.session.commit()
|
|
print("✓ Admin password reset successfully!")
|
|
print("New password: admin123")
|
|
return
|
|
|
|
# Create admin user
|
|
admin_user = User(
|
|
username="admin",
|
|
email="admin@scriptsmanager.local",
|
|
role=UserRole.ADMIN,
|
|
language="en",
|
|
theme="light",
|
|
)
|
|
admin_user.set_password("admin123")
|
|
|
|
db.session.add(admin_user)
|
|
db.session.commit()
|
|
|
|
print("✓ Admin user created successfully!")
|
|
print("\nAdmin credentials:")
|
|
print("Username: admin")
|
|
print("Password: admin123")
|
|
print("\nIMPORTANT: Change the password after first login!")
|
|
|
|
except IntegrityError as e:
|
|
db.session.rollback()
|
|
print(f"Error creating admin user: {e}")
|
|
raise
|
|
except Exception as e:
|
|
print(f"Unexpected error: {e}")
|
|
raise
|
|
|
|
|
|
if __name__ == "__main__":
|
|
create_admin()
|