fix: Auto-provision dev-bypass-user with Admin roles on startup
Browse files- create_mock_user.py +49 -0
- main.py +31 -0
create_mock_user.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import firebase_admin
|
| 2 |
+
from firebase_admin import credentials, firestore
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Manual init for script
|
| 7 |
+
FIREBASE_CREDENTIALS_PATH = "serviceAccountKey.json"
|
| 8 |
+
STORAGE_BUCKET = "bussymath.firebasestorage.app"
|
| 9 |
+
|
| 10 |
+
if not firebase_admin._apps:
|
| 11 |
+
with open(FIREBASE_CREDENTIALS_PATH, "r", encoding="utf-8") as f:
|
| 12 |
+
cred_dict = json.load(f)
|
| 13 |
+
cred = credentials.Certificate(cred_dict)
|
| 14 |
+
firebase_admin.initialize_app(cred, {
|
| 15 |
+
'storageBucket': STORAGE_BUCKET
|
| 16 |
+
})
|
| 17 |
+
|
| 18 |
+
db = firestore.client()
|
| 19 |
+
|
| 20 |
+
def create_mock_user():
|
| 21 |
+
uid = "dev-bypass-user"
|
| 22 |
+
user_ref = db.collection('users').document(uid)
|
| 23 |
+
|
| 24 |
+
today = "2026-03-12" # Static for bypass
|
| 25 |
+
month = "2026-03"
|
| 26 |
+
|
| 27 |
+
user_data = {
|
| 28 |
+
'uid': uid,
|
| 29 |
+
'name': 'Dev Bypass User',
|
| 30 |
+
'status': 'approved',
|
| 31 |
+
'tier': 'student_premium',
|
| 32 |
+
'used_today': 0,
|
| 33 |
+
'used_tokens_this_month': 0,
|
| 34 |
+
'monthly_token_budget': 9999999,
|
| 35 |
+
'last_usage_month': month,
|
| 36 |
+
'quota': {
|
| 37 |
+
'limit': 9999,
|
| 38 |
+
'used_today': 0,
|
| 39 |
+
'last_reset_date': today,
|
| 40 |
+
},
|
| 41 |
+
'isAdmin': True,
|
| 42 |
+
'role': 'admin'
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
user_ref.set(user_data, merge=True)
|
| 46 |
+
print(f"✅ Created mock user: {uid}")
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
create_mock_user()
|
main.py
CHANGED
|
@@ -90,6 +90,34 @@ def verify_system_health():
|
|
| 90 |
else:
|
| 91 |
logger.info("✅ [HEALTH-CHECK] GOOGLE_API_KEY is detected.")
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# --- INFRA HARDENING: Lifespan Context Manager ---
|
| 94 |
@asynccontextmanager
|
| 95 |
async def lifespan(app: FastAPI):
|
|
@@ -101,6 +129,9 @@ async def lifespan(app: FastAPI):
|
|
| 101 |
loop.set_exception_handler(custom_async_exception_handler)
|
| 102 |
logger.info("🛡️ [STARTUP] Global Async Exception Handler registered.")
|
| 103 |
|
|
|
|
|
|
|
|
|
|
| 104 |
yield # Yield control back to FastAPI
|
| 105 |
|
| 106 |
# Shutdown Phase
|
|
|
|
| 90 |
else:
|
| 91 |
logger.info("✅ [HEALTH-CHECK] GOOGLE_API_KEY is detected.")
|
| 92 |
|
| 93 |
+
def _ensure_dev_admin_exists():
|
| 94 |
+
"""V5.9.6: Ensures dev-bypass-user exists with admin role in DEV mode."""
|
| 95 |
+
if IS_PRODUCTION:
|
| 96 |
+
return
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
db = firebase_manager.get_db()
|
| 100 |
+
if not db:
|
| 101 |
+
return
|
| 102 |
+
|
| 103 |
+
uid = "dev-bypass-user"
|
| 104 |
+
user_ref = db.collection('users').document(uid)
|
| 105 |
+
doc = user_ref.get()
|
| 106 |
+
|
| 107 |
+
# We always merge to ensure roles are correct even if doc exists
|
| 108 |
+
user_ref.set({
|
| 109 |
+
'uid': uid,
|
| 110 |
+
'name': 'Dev Bypass User (Admin)',
|
| 111 |
+
'isAdmin': True,
|
| 112 |
+
'role': 'admin',
|
| 113 |
+
'status': 'approved',
|
| 114 |
+
'tier': 'student_premium',
|
| 115 |
+
'monthly_token_budget': 9999999
|
| 116 |
+
}, merge=True)
|
| 117 |
+
logger.info(f"🛡️ [STARTUP] Dev admin user '{uid}' provisioned/verified.")
|
| 118 |
+
except Exception as e:
|
| 119 |
+
logger.error(f"❌ [STARTUP] Failed to provision dev admin: {e}")
|
| 120 |
+
|
| 121 |
# --- INFRA HARDENING: Lifespan Context Manager ---
|
| 122 |
@asynccontextmanager
|
| 123 |
async def lifespan(app: FastAPI):
|
|
|
|
| 129 |
loop.set_exception_handler(custom_async_exception_handler)
|
| 130 |
logger.info("🛡️ [STARTUP] Global Async Exception Handler registered.")
|
| 131 |
|
| 132 |
+
# V5.9.6: Provision dev admin if needed
|
| 133 |
+
_ensure_dev_admin_exists()
|
| 134 |
+
|
| 135 |
yield # Yield control back to FastAPI
|
| 136 |
|
| 137 |
# Shutdown Phase
|