Spaces:
Running

microfunnel-api / backend /grant_admin.py
axeronis
fix: Register Order models in init and add admin grant script
a78d47f
Raw History Blame Contribute Delete
2.14 kB
import asyncio
from sqlmodel import select
from app.core.database import async_session_maker
from app.models.user import User
from app.models.tenant import Tenant
TARGET_EMAIL = "axeroni.s2@gmail.com"
DEFAULT_PASSWORD = "ChangeMe123!"
async def grant_admin():
print(f"πŸš€ Granting Admin Access to {TARGET_EMAIL}...")
async with async_session_maker() as session:
try:
# 1. Check if user exists
result = await session.execute(select(User).where(User.email == TARGET_EMAIL))
user = result.scalar_one_or_none()
if user:
print(f"βœ… User found: {user.id}")
user.is_superuser = True
user.role = "OWNER"
session.add(user)
await session.commit()
print("πŸŽ‰ User promoted to SUPERUSER!")
else:
print("⚠️ User not found. Creating new...")
# 2. Find a tenant to link to
t_result = await session.execute(select(Tenant))
tenant = t_result.scalars().first()
if not tenant:
print("❌ No Tenants found! Cannot create user without a tenant.")
return
print(f"πŸ”— Linking to Tenant: {tenant.name} ({tenant.id})")
# 3. Create User
hashed = User.get_password_hash(DEFAULT_PASSWORD)
new_user = User(
email=TARGET_EMAIL,
hashed_password=hashed,
tenant_id=tenant.id,
role="OWNER",
is_superuser=True,
full_name="Admin User"
)
session.add(new_user)
await session.commit()
print(f"πŸŽ‰ Created new Admin User. Password: {DEFAULT_PASSWORD}")
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
asyncio.run(grant_admin())