add tenant logic
This commit is contained in:
@@ -1 +1,2 @@
|
||||
from app.core.cache import domainCacheService
|
||||
from app.core.cache import domainCacheService
|
||||
from app.core.cache import tenantCacheService
|
||||
@@ -1,7 +1,7 @@
|
||||
from fastapi import HTTPException, status
|
||||
from fastapi import HTTPException, status,Depends
|
||||
import httpx
|
||||
from app.db.schemas import ErrorCreate
|
||||
from app.db.session import SessionLocal
|
||||
from app.db.session import get_tenant_db
|
||||
from app.db.crud import create_log
|
||||
|
||||
class APIException(Exception):
|
||||
@@ -31,9 +31,9 @@ class APIException(Exception):
|
||||
self.error = ErrorCreate(location=location, title=title, content=content)
|
||||
super().__init__(self.error)
|
||||
|
||||
def writedblog(exc: APIException):
|
||||
db = SessionLocal()
|
||||
try:
|
||||
create_log(db,exc.error)
|
||||
finally:
|
||||
db.close()
|
||||
def writedblog(exc: APIException,db = Depends(get_tenant_db())):
|
||||
#db = SessionLocal()
|
||||
#try:
|
||||
create_log(db,exc.error)
|
||||
#finally:
|
||||
#db.close()
|
||||
@@ -3,13 +3,14 @@ import jwt
|
||||
from fastapi import Depends, HTTPException, Request, Security, status
|
||||
from jwt import PyJWTError
|
||||
|
||||
from app.db import models, schemas, session
|
||||
from app.db import models, schemas
|
||||
from app.db.crud import get_user_by_email, create_user,get_user
|
||||
from app.core import security
|
||||
from app.db.cruddb import userService
|
||||
from app.core.dbmanager import get_db
|
||||
|
||||
async def get_current_user(security_scopes: SecurityScopes,
|
||||
db=Depends(session.get_db), token: str = Depends(security.oauth2_scheme)
|
||||
db=Depends(get_db), token: str = Depends(security.oauth2_scheme)
|
||||
):
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
@@ -17,7 +18,6 @@ async def get_current_user(security_scopes: SecurityScopes,
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
try:
|
||||
|
||||
payload = jwt.decode(
|
||||
token, security.SECRET_KEY, algorithms=[security.ALGORITHM]
|
||||
)
|
||||
@@ -25,6 +25,10 @@ async def get_current_user(security_scopes: SecurityScopes,
|
||||
if id is None:
|
||||
raise credentials_exception
|
||||
|
||||
tenant:str = payload.get("tenant")
|
||||
if tenant is None:
|
||||
raise credentials_exception
|
||||
|
||||
permissions: str = payload.get("permissions")
|
||||
if not permissions =="ALL":
|
||||
for scope in security_scopes.scopes:
|
||||
@@ -59,11 +63,11 @@ async def get_current_active_superuser(
|
||||
|
||||
|
||||
def authenticate_user(db, email: str, password: str):
|
||||
user = get_user_by_email(db, email)
|
||||
user = userService.get_user_by_email(db,email) #get_user_by_email(db, email)
|
||||
if not user:
|
||||
return False
|
||||
return None
|
||||
if not security.verify_password(password, user.hashed_password):
|
||||
return False
|
||||
return None
|
||||
return user
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import time
|
||||
from typing import Any
|
||||
from sqlalchemy.orm import Session
|
||||
from app.db.cruddb import domainService
|
||||
from app.db.cruddb import tenantService
|
||||
|
||||
class MemoryCache:
|
||||
def __init__(self, max_cache_size: int = 100, ttl: int = 60):
|
||||
@@ -52,4 +53,19 @@ class domainCache:
|
||||
def clear_default_domainurl(self):
|
||||
self.memoryCache.clear()
|
||||
|
||||
domainCacheService =domainCache()
|
||||
domainCacheService =domainCache()
|
||||
|
||||
|
||||
class tenantCache:
|
||||
|
||||
def __init__(self):
|
||||
self.memoryCache = MemoryCache(max_cache_size=50, ttl=120)
|
||||
|
||||
def get_tenant_db(self,db: Session, tenantid: str):
|
||||
if not self.memoryCache.get(f"TENANT_{tenantid}"):
|
||||
tenant = tenantService.get_tenant(db,tenantid)
|
||||
if tenant:
|
||||
self.memoryCache.set(f"TENANT_{tenantid}",tenant.db)
|
||||
return self.memoryCache.get(f"TENANT_{tenantid}")
|
||||
|
||||
tenantCacheService =tenantCache()
|
||||
12
backend/app/core/dbmanager.py
Normal file
12
backend/app/core/dbmanager.py
Normal file
@@ -0,0 +1,12 @@
|
||||
|
||||
from fastapi import Depends
|
||||
from app.db.session import get_tenant_db,get_user_db
|
||||
from app.core import tenantCacheService
|
||||
|
||||
def get_db(tenant:str = "1",tenantdb = Depends(get_tenant_db)):
|
||||
db_url = tenantCacheService.get_tenant_db(tenantdb,tenant)
|
||||
db = get_user_db(db_url)
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user