Switching to the registry paradigm
This commit is contained in:
@@ -4,31 +4,48 @@ 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
|
||||
class Registry:
|
||||
user = None
|
||||
|
||||
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
|
||||
def __init__(self, db_client, instance, firm):
|
||||
self.db = db_client[f"tenant_{instance}_{firm}"]
|
||||
self.instance = instance
|
||||
self.firm = firm
|
||||
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
self.current_firm = CurrentFirmModel.get(self.db)
|
||||
|
||||
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:
|
||||
def set_user(self, user):
|
||||
for firm in user.firms:
|
||||
if firm.instance == self.instance and firm.firm == firm:
|
||||
self.user = user
|
||||
self.db.user = user
|
||||
return
|
||||
|
||||
raise PermissionError
|
||||
|
||||
async def get_tenant_registry(instance: str, firm: str, db_client=Depends(get_db_client)) -> Registry:
|
||||
registry = Registry(db_client, instance, firm)
|
||||
if await registry.current_firm is None:
|
||||
raise HTTPException(status_code=405, detail=f"Firm needs to be initialized first")
|
||||
|
||||
return registry
|
||||
|
||||
def get_authed_tenant_registry(registry=Depends(get_tenant_registry), user=Depends(get_current_user)) -> Registry:
|
||||
try:
|
||||
registry.set_user(user)
|
||||
except PermissionError:
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
|
||||
return registry
|
||||
|
||||
async def get_uninitialized_registry(instance: str, firm: str, db_client=Depends(get_db_client), user=Depends(get_current_user)) -> Registry:
|
||||
registry = Registry(db_client, instance, firm)
|
||||
if await registry.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
|
||||
try:
|
||||
registry.set_user(user)
|
||||
except PermissionError:
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
|
||||
raise HTTPException(status_code=404, detail="This firm doesn't exist or you are not allowed to access it.")
|
||||
return registry
|
||||
|
||||
Reference in New Issue
Block a user