94 lines
No EOL
2.9 KiB
Python
94 lines
No EOL
2.9 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Script to make a user an admin in the AgentHub MongoDB database.
|
||
Usage: python make_admin_script.py <email>
|
||
"""
|
||
|
||
import sys
|
||
import asyncio
|
||
from motor.motor_asyncio import AsyncIOMotorClient
|
||
|
||
async def make_user_admin(email: str):
|
||
"""Make a user an admin by email address"""
|
||
|
||
# Database configuration
|
||
MONGODB_URI = "mongodb://localhost:27019"
|
||
DATABASE_NAME = "agenthub_db"
|
||
|
||
try:
|
||
# Connect to MongoDB
|
||
print(f"Connecting to MongoDB at {MONGODB_URI}...")
|
||
client = AsyncIOMotorClient(MONGODB_URI)
|
||
db = client[DATABASE_NAME]
|
||
users_collection = db.users
|
||
|
||
# Check if user exists
|
||
user = await users_collection.find_one({"email": email})
|
||
if not user:
|
||
print(f"❌ User with email '{email}' not found!")
|
||
return False
|
||
|
||
print(f"✅ Found user: {user.get('full_name', 'Unknown')} ({email})")
|
||
|
||
# Check if already admin
|
||
if user.get("is_admin", False):
|
||
print(f"ℹ️ User '{email}' is already an admin!")
|
||
return True
|
||
|
||
# Update user to admin
|
||
result = await users_collection.update_one(
|
||
{"email": email},
|
||
{"$set": {"is_admin": True}}
|
||
)
|
||
|
||
if result.modified_count > 0:
|
||
print(f"✅ Successfully made '{email}' an admin!")
|
||
|
||
# Verify the update
|
||
updated_user = await users_collection.find_one({"email": email})
|
||
if updated_user and updated_user.get("is_admin"):
|
||
print(f"✅ Verified: {email} now has admin privileges")
|
||
return True
|
||
else:
|
||
print(f"❌ Verification failed: Admin status not updated properly")
|
||
return False
|
||
else:
|
||
print(f"❌ Failed to update user '{email}' to admin")
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ Error: {str(e)}")
|
||
return False
|
||
finally:
|
||
# Close connection
|
||
if 'client' in locals():
|
||
client.close()
|
||
print("🔌 Database connection closed")
|
||
|
||
def main():
|
||
if len(sys.argv) != 2:
|
||
print("Usage: python make_admin_script.py <email>")
|
||
print("Example: python make_admin_script.py admin@agenthub.com")
|
||
sys.exit(1)
|
||
|
||
email = sys.argv[1].strip()
|
||
|
||
if not email or "@" not in email:
|
||
print("❌ Please provide a valid email address")
|
||
sys.exit(1)
|
||
|
||
print(f"🚀 Making user '{email}' an admin...")
|
||
print("-" * 50)
|
||
|
||
# Run the async function
|
||
success = asyncio.run(make_user_admin(email))
|
||
|
||
print("-" * 50)
|
||
if success:
|
||
print(f"🎉 Done! User '{email}' is now an admin.")
|
||
else:
|
||
print(f"💥 Failed to make user '{email}' an admin.")
|
||
sys.exit(1)
|
||
|
||
if __name__ == "__main__":
|
||
main() |