add tenant logic

This commit is contained in:
2024-12-15 12:28:22 +09:00
parent 2823364148
commit 39775a5179
16 changed files with 138 additions and 55 deletions

View File

@@ -1,26 +1,35 @@
from fastapi.security import OAuth2PasswordRequestForm
from fastapi import Form
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi import APIRouter, Depends, HTTPException, status
from datetime import timedelta
from app.db.session import get_db
from app.core import security
from app.core.auth import authenticate_user, sign_up_new_user
from app.core import security,tenantCacheService
from app.core.dbmanager import get_db
from sqlalchemy.orm import Session
auth_router = r = APIRouter()
@r.post("/token")
async def login(
db=Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
):
user = authenticate_user(db, form_data.username, form_data.password)
if not user:
async def login(db:Session= Depends(get_db) ,form_data: OAuth2PasswordRequestForm = Depends()):
if not db :
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
user = authenticate_user(db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="abcIncorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(
minutes=security.ACCESS_TOKEN_EXPIRE_MINUTES
)
@@ -33,7 +42,7 @@ async def login(
permissions =";".join(list(set(perlst)))
access_token = security.create_access_token(
data={"sub": user.id, "roles":roles,"permissions": permissions ,},
data={"sub": user.id,"roles":roles,"permissions": permissions,"tenant":user.tenantid,},
expires_delta=access_token_expires,
)

View File

@@ -8,7 +8,7 @@ import deepdiff
import app.core.config as config
import os
from pathlib import Path
from app.db.session import SessionLocal
from app.core.dbmanager import get_db
from app.db.crud import get_flows_by_app,get_kintoneformat
from app.core.auth import get_current_active_user,get_current_user
from app.core.apiexception import APIException
@@ -17,15 +17,15 @@ from app.db.cruddb import domainService
kinton_router = r = APIRouter()
def getkintoneenv(user = Depends(get_current_user)):
db = SessionLocal()
db = get_db(user.tenantid) #SessionLocal()
domain = domainService.get_default_domain(db,user.id) #get_activedomain(db, user.id)
db.close()
kintoneevn = config.KINTONE_ENV(domain)
return kintoneevn
def getkintoneformat():
db = SessionLocal()
def getkintoneformat(user = Depends(get_current_user)):
db = get_db(user.tenantid)#SessionLocal()
formats = get_kintoneformat(db)
db.close()
return formats

View File

@@ -2,8 +2,8 @@ from http import HTTPStatus
from fastapi import Query, Request,Depends, APIRouter, UploadFile,HTTPException,File
from fastapi.responses import JSONResponse
# from app.core.operation import log_operation
from app.db import Base,engine
from app.db.session import get_db
# from app.db import Base,engine
from app.core.dbmanager import get_db
from app.db.crud import *
from app.db.schemas import *
from typing import List, Optional
@@ -15,7 +15,7 @@ from app.db.cruddb import domainService,appService
import httpx
import app.core.config as config
from app.core import domainCacheService
from app.core import domainCacheService,tenantCacheService
platform_router = r = APIRouter()

View File

@@ -2,7 +2,7 @@ from fastapi import APIRouter, Request, Depends, Response, Security, encoders
import typing as t
from app.core.common import ApiReturnModel,ApiReturnPage
from app.core.apiexception import APIException
from app.db.session import get_db
from app.core.dbmanager import get_db
from app.db.crud import (
get_allusers,
get_users,
@@ -16,6 +16,7 @@ from app.db.crud import (
from app.db.schemas import UserCreate, UserEdit, User, UserOut,RoleBase,Permission
from app.core.auth import get_current_user,get_current_active_user, get_current_active_superuser
from app.db.cruddb import userService
from app.core import tenantCacheService
users_router = r = APIRouter()