117 lines
3.3 KiB
Python
117 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Complete migration and admin setup script
|
|
Run this once to:
|
|
1. Add is_admin column to existing users table
|
|
2. Prompt you to set an admin user
|
|
"""
|
|
|
|
import sqlite3
|
|
import sys
|
|
|
|
DATABASE = 'users.db'
|
|
|
|
def migrate_db():
|
|
"""Add is_admin column to existing database"""
|
|
conn = sqlite3.connect(DATABASE)
|
|
cursor = conn.cursor()
|
|
try:
|
|
cursor.execute("ALTER TABLE users ADD COLUMN is_admin INTEGER DEFAULT 0")
|
|
conn.commit()
|
|
print("✅ Successfully added is_admin column to users table")
|
|
return True
|
|
except sqlite3.OperationalError as e:
|
|
if "duplicate column name" in str(e) or "already exists" in str(e):
|
|
print("✅ is_admin column already exists")
|
|
return True
|
|
else:
|
|
print(f"❌ Error: {e}")
|
|
return False
|
|
finally:
|
|
conn.close()
|
|
|
|
def list_users():
|
|
"""List all users"""
|
|
conn = sqlite3.connect(DATABASE)
|
|
conn.row_factory = sqlite3.Row
|
|
cursor = conn.cursor()
|
|
cursor.execute("SELECT id, 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("-" * 50)
|
|
for user in users:
|
|
status = "👑 ADMIN" if user['is_admin'] else "👤 User"
|
|
print(f" {user['username']:20} | {status}")
|
|
print("-" * 50)
|
|
return users
|
|
|
|
def make_admin(username):
|
|
"""Make a user an admin"""
|
|
conn = sqlite3.connect(DATABASE)
|
|
cursor = conn.cursor()
|
|
|
|
# Check if user exists
|
|
cursor.execute("SELECT id FROM users WHERE username = ?", (username,))
|
|
user = cursor.fetchone()
|
|
|
|
if not user:
|
|
print(f"❌ User '{username}' not found")
|
|
conn.close()
|
|
return False
|
|
|
|
# Set as admin
|
|
cursor.execute("UPDATE users SET is_admin = 1 WHERE username = ?", (username,))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
print(f"✅ User '{username}' is now an ADMIN 👑")
|
|
return True
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print(" 🔐 ADMIN SETUP WIZARD")
|
|
print("=" * 60)
|
|
|
|
# Step 1: Migrate database
|
|
print("\n📦 Step 1: Migrating database...")
|
|
if not migrate_db():
|
|
print("❌ Migration failed. Exiting.")
|
|
sys.exit(1)
|
|
|
|
# Step 2: List users
|
|
print("\n📋 Step 2: Showing all users...")
|
|
users = list_users()
|
|
if not users:
|
|
print("❌ No users in database. Please register a user first.")
|
|
sys.exit(1)
|
|
|
|
# Step 3: Ask which user to make admin
|
|
print("\n👑 Step 3: Make a user an admin")
|
|
while True:
|
|
username = input("\nEnter username to promote to admin (or 'q' to quit): ").strip()
|
|
|
|
if username.lower() == 'q':
|
|
print("\n✅ Setup complete! Run your app now.")
|
|
break
|
|
|
|
if not username:
|
|
print("❌ Username cannot be empty")
|
|
continue
|
|
|
|
if make_admin(username):
|
|
print("\n✅ Admin setup successful!")
|
|
print("📝 You can now:")
|
|
print(f" 1. Login as '{username}'")
|
|
print(" 2. Navigate to /admin to see the admin dashboard")
|
|
break
|
|
else:
|
|
print("❌ Failed to promote user. Try again.")
|
|
|
|
if __name__ == '__main__':
|
|
main() |