Spaces:
Running

File size: 2,141 Bytes
a78d47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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())