Spaces:
Running

axeronis commited on
Commit
8e64778
·
1 Parent(s): ac6b5b5

feat: Add database cleanup and superuser creation scripts

Browse files
backend/app/api/admin.py CHANGED
@@ -48,7 +48,7 @@ async def get_admin_user(current_user: User = Depends(get_current_user)):
48
  """
49
  Restrict access to specific admin email.
50
  """
51
- if current_user.email != ADMIN_EMAIL:
52
  raise HTTPException(
53
  status_code=status.HTTP_403_FORBIDDEN,
54
  detail="Access denied. Admin rights required."
 
48
  """
49
  Restrict access to specific admin email.
50
  """
51
+ if current_user.email.lower() != ADMIN_EMAIL.lower():
52
  raise HTTPException(
53
  status_code=status.HTTP_403_FORBIDDEN,
54
  detail="Access denied. Admin rights required."
backend/scripts/cleanup_users.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import asyncio
3
+ import sys
4
+ import os
5
+
6
+ # Add the parent directory to the path so we can import the app modules
7
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
+
9
+ from sqlmodel import select, delete
10
+ from sqlalchemy.ext.asyncio import AsyncSession
11
+ from app.core.database import async_session_maker
12
+ from app.models.user import User
13
+ from app.models.tenant import Tenant
14
+ from app.core.config import settings
15
+
16
+ async def cleanup_database():
17
+ """
18
+ Deletes all users and tenants from the database.
19
+ WARNING: This is a destructive operation!
20
+ """
21
+ print("WARNING: This will delete ALL users and tenants from the database.")
22
+ confirmation = input("Are you sure you want to proceed? (type 'yes' to confirm): ")
23
+
24
+ if confirmation.lower() != 'yes':
25
+ print("Operation cancelled.")
26
+ return
27
+
28
+ async with async_session_maker() as session:
29
+ try:
30
+ # Delete Users first (due to foreign key constraints, though often it's the other way if cascade is set,
31
+ # but usually User depends on Tenant. User.tenant_id -> Tenant.id)
32
+ # Actually, User has a foreign key to Tenant. So we delete Users first.
33
+
34
+ print("Deleting Users...")
35
+ statement = delete(User)
36
+ result = await session.exec(statement)
37
+ print(f"Deleted {result.rowcount} users.")
38
+
39
+ print("Deleting Tenants...")
40
+ statement = delete(Tenant)
41
+ result = await session.exec(statement)
42
+ print(f"Deleted {result.rowcount} tenants.")
43
+
44
+ await session.commit()
45
+ print("Database cleanup complete.")
46
+
47
+ except Exception as e:
48
+ await session.rollback()
49
+ print(f"Error occurred: {e}")
50
+ print("Rollback performed.")
51
+
52
+ if __name__ == "__main__":
53
+ if sys.platform == 'win32':
54
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
55
+ asyncio.run(cleanup_database())
backend/scripts/create_superuser.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import asyncio
3
+ import sys
4
+ import os
5
+
6
+ # Add parent dir to path
7
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
+
9
+ from sqlmodel import select
10
+ from app.core.database import async_session_maker
11
+ from app.models.user import User, UserRole
12
+ from app.models.tenant import Tenant
13
+ from app.core.security import get_password_hash
14
+
15
+ async def create_superuser():
16
+ email = "Axeroni.s2@gmail.com"
17
+ password = "admin" # Default password
18
+
19
+ print(f"Creating superuser: {email}")
20
+
21
+ async with async_session_maker() as session:
22
+ # Check if user exists
23
+ statement = select(User).where(User.email == email)
24
+ result = await session.exec(statement)
25
+ user = result.first()
26
+
27
+ if user:
28
+ print(f"User {email} already exists.")
29
+ return
30
+
31
+ # Create Tenant
32
+ print("Creating default tenant...")
33
+ tenant = Tenant(
34
+ name="Admin Tenant",
35
+ business_name="MicroFunnel Admin",
36
+ subscription_plan="enterprise"
37
+ )
38
+ session.add(tenant)
39
+ await session.commit()
40
+ await session.refresh(tenant)
41
+
42
+ # Create User
43
+ print("Creating user...")
44
+ user = User(
45
+ email=email,
46
+ hashed_password=get_password_hash(password),
47
+ full_name="Super Admin",
48
+ tenant_id=tenant.id,
49
+ role=UserRole.OWNER,
50
+ is_active=True,
51
+ is_superuser=True
52
+ )
53
+ session.add(user)
54
+ await session.commit()
55
+
56
+ print(f"Superuser created successfully!")
57
+ print(f"Email: {email}")
58
+ print(f"Password: {password}")
59
+
60
+ if __name__ == "__main__":
61
+ if sys.platform == 'win32':
62
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
63
+ asyncio.run(create_superuser())