Adding multi tenant check and Starting firm initialization
This commit is contained in:
34
api/rpk-api/firm/core/depends.py
Normal file
34
api/rpk-api/firm/core/depends.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from fastapi import HTTPException, Depends
|
||||
from hub.auth import get_current_user
|
||||
|
||||
from firm.db import get_db_client
|
||||
from firm.current_firm import CurrentFirmModel
|
||||
|
||||
async def get_tenant_db_cursor(instance: str, firm: str, db_client=Depends(get_db_client)):
|
||||
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
||||
current_firm = await CurrentFirmModel.get(db_cursor)
|
||||
if current_firm is None:
|
||||
raise HTTPException(status_code=405, detail=f"Firm needs to be instantiated first")
|
||||
db_cursor.firm = current_firm
|
||||
return db_cursor
|
||||
|
||||
def get_logged_tenant_db_cursor(db_cursor=Depends(get_tenant_db_cursor), user=Depends(get_current_user)):
|
||||
for firm in user.firms:
|
||||
if firm == db_cursor.firm:
|
||||
db_cursor.user = user
|
||||
return db_cursor
|
||||
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
|
||||
async def get_uninitialized_tenant_db_cursor(instance: str, firm: str, db_client=Depends(get_db_client), user=Depends(get_current_user)):
|
||||
db_cursor = db_client[f"tenant_{instance}_{firm}"]
|
||||
current_firm = await CurrentFirmModel.get(db_cursor)
|
||||
if current_firm is not None:
|
||||
HTTPException(status_code=409, detail="Firm configuration already exists")
|
||||
|
||||
for firm in user.firms:
|
||||
if firm == db_cursor.firm:
|
||||
db_cursor.user = user
|
||||
return db_cursor
|
||||
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
Reference in New Issue
Block a user