diff --git a/backend/app/admin/schema/opera_log.py b/backend/app/admin/schema/opera_log.py index e3989786..f75e7f6a 100644 --- a/backend/app/admin/schema/opera_log.py +++ b/backend/app/admin/schema/opera_log.py @@ -37,8 +37,6 @@ class CreateOperaLogParam(OperaLogSchemaBase): if settings.TENANT_ENABLED: tenant_id: int = Field(description='租户 ID') - else: - tenant_id: int = Field(settings.TENANT_DEFAULT_ID, description='租户 ID') class UpdateOperaLogParam(OperaLogSchemaBase): diff --git a/backend/app/admin/schema/user.py b/backend/app/admin/schema/user.py index efe51aa5..2a783305 100644 --- a/backend/app/admin/schema/user.py +++ b/backend/app/admin/schema/user.py @@ -23,7 +23,8 @@ class AuthLoginParam(AuthSchemaBase): uuid: str | None = Field(None, description='验证码 UUID') captcha: str | None = Field(None, description='验证码') - tenant_id: int = Field(settings.TENANT_DEFAULT_ID, description='租户 ID') + if settings.TENANT_ENABLED: + tenant_id: int = Field(settings.TENANT_DEFAULT_ID, description='租户 ID') class AddUserParam(AuthSchemaBase): @@ -93,8 +94,6 @@ class GetUserInfoDetail(UserInfoSchemaBase): dept_id: int | None = Field(None, description='部门 ID') if settings.TENANT_ENABLED: tenant_id: int = Field(description='租户 ID') - else: - tenant_id: int = Field(settings.TENANT_DEFAULT_ID, description='租户 ID') class GetUserInfoWithRelationDetail(GetUserInfoDetail): diff --git a/backend/app/admin/service/auth_service.py b/backend/app/admin/service/auth_service.py index 5e32d976..a6c2fd33 100644 --- a/backend/app/admin/service/auth_service.py +++ b/backend/app/admin/service/auth_service.py @@ -81,7 +81,7 @@ class AuthService: ) return access_token_data.access_token, user - async def login( + async def login( # noqa: C901 self, *, db: AsyncSession, @@ -99,6 +99,7 @@ class AuthService: :return: """ user = None + tenant_id = settings.TENANT_DEFAULT_ID try: await load_login_config(db) if settings.LOGIN_CAPTCHA_ENABLED: @@ -111,8 +112,10 @@ class AuthService: raise errors.CustomError(error=CustomErrorCode.CAPTCHA_ERROR) await redis_client.delete(f'{settings.LOGIN_CAPTCHA_REDIS_PREFIX}:{obj.uuid}') - ctx.tenant_id = obj.tenant_id # 用于操作日志 - await check_tenant_status(db, obj.tenant_id) + if settings.TENANT_ENABLED: + tenant_id = obj.tenant_id + await check_tenant_status(db, tenant_id) + ctx.tenant_id = tenant_id # 用于操作日志 user, days_remaining = await self.user_verify(db, obj.username, obj.password) await user_dao.update_login_time(db, obj.username) await db.refresh(user) @@ -153,7 +156,7 @@ class AuthService: login_time=timezone.now(), status=LoginLogStatusType.fail.value, msg=e.msg, - tenant_id=obj.tenant_id, + tenant_id=tenant_id, ) raise errors.RequestError(code=e.code, msg=e.msg, background=task) except Exception as e: @@ -167,7 +170,7 @@ class AuthService: login_time=timezone.now(), status=LoginLogStatusType.success.value, msg=t('success.login.success'), - tenant_id=obj.tenant_id, + tenant_id=tenant_id, ) data = GetLoginToken( access_token=access_token_data.access_token, diff --git a/backend/app/admin/service/login_log_service.py b/backend/app/admin/service/login_log_service.py index c444b61f..99f9839f 100644 --- a/backend/app/admin/service/login_log_service.py +++ b/backend/app/admin/service/login_log_service.py @@ -51,23 +51,24 @@ class LoginLogService: :return: """ try: - obj = CreateLoginLogParam( - tenant_id=tenant_id, - user_uuid=user_uuid, - username=username, - status=status, - ip=ctx.ip, - country=ctx.country, - region=ctx.region, - city=ctx.city, - user_agent=ctx.user_agent, - browser=ctx.browser, - os=ctx.os, - device=ctx.device, - msg=msg, - login_time=login_time, - ) - # 为后台任务创建独立数据库会话 + data = { + 'user_uuid': user_uuid, + 'username': username, + 'status': status, + 'ip': ctx.ip, + 'country': ctx.country, + 'region': ctx.region, + 'city': ctx.city, + 'user_agent': ctx.user_agent, + 'browser': ctx.browser, + 'os': ctx.os, + 'device': ctx.device, + 'msg': msg, + 'login_time': login_time, + } + if settings.TENANT_ENABLED: + data['tenant_id'] = tenant_id + obj = CreateLoginLogParam(**data) async with async_db_session.begin() as db: await login_log_dao.create(db, obj) except Exception as e: diff --git a/backend/core/conf.py b/backend/core/conf.py index 336d3861..344fe760 100644 --- a/backend/core/conf.py +++ b/backend/core/conf.py @@ -247,7 +247,7 @@ class Settings(BaseSettings): OPERA_LOG_QUEUE_TIMEOUT: int = 60 # 1 分钟 # 租户 - TENANT_ENABLED: bool = False + TENANT_ENABLED: bool = True TENANT_DEFAULT_ID: int = 0 # Plugin 配置 diff --git a/backend/middleware/jwt_auth_middleware.py b/backend/middleware/jwt_auth_middleware.py index a7e422b2..3b35a639 100644 --- a/backend/middleware/jwt_auth_middleware.py +++ b/backend/middleware/jwt_auth_middleware.py @@ -98,7 +98,7 @@ class JwtAuthMiddleware(AuthenticationBackend): # 设置用户 ID 和租户 ID 到上下文 ctx.user_id = user.id - ctx.tenant_id = user.tenant_id + ctx.tenant_id = getattr(user, 'tenant_id', settings.TENANT_DEFAULT_ID) # 请注意,此返回使用非标准模式,所以在认证通过时,将丢失某些标准特性 # 标准返回模式请查看:https://www.starlette.io/authentication/ diff --git a/backend/middleware/opera_log_middleware.py b/backend/middleware/opera_log_middleware.py index 41dddbcb..882a39a3 100644 --- a/backend/middleware/opera_log_middleware.py +++ b/backend/middleware/opera_log_middleware.py @@ -119,28 +119,31 @@ class OperaLogMiddleware(BaseHTTPMiddleware): if settings.TENANT_ENABLED: tenant_id = ctx.tenant_id - opera_log_in = CreateOperaLogParam( - trace_id=get_request_trace_id(), - username=username, - method=method, - title=summary, - path=path, - ip=ctx.ip, - country=ctx.country, - region=ctx.region, - city=ctx.city, - user_agent=ctx.user_agent, - os=ctx.os, - browser=ctx.browser, - device=ctx.device, - args=args, - status=status, - code=str(code), - msg=msg, - cost_time=elapsed, - opera_time=ctx.start_time, - tenant_id=tenant_id, - ) + opera_log_data = { + 'trace_id': get_request_trace_id(), + 'username': username, + 'method': method, + 'title': summary, + 'path': path, + 'ip': ctx.ip, + 'country': ctx.country, + 'region': ctx.region, + 'city': ctx.city, + 'user_agent': ctx.user_agent, + 'os': ctx.os, + 'browser': ctx.browser, + 'device': ctx.device, + 'args': args, + 'status': status, + 'code': str(code), + 'msg': msg, + 'cost_time': elapsed, + 'opera_time': ctx.start_time, + } + if settings.TENANT_ENABLED: + opera_log_data['tenant_id'] = tenant_id + + opera_log_in = CreateOperaLogParam(**opera_log_data) await self.opera_log_queue.put(opera_log_in) if path.startswith(settings.FASTAPI_API_V1_PATH):