Skip to content

Commit

Permalink
fixed call
Browse files Browse the repository at this point in the history
  • Loading branch information
wuranxu committed Jun 26, 2022
1 parent efc8f5e commit 5e4e2b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from fastapi import Header
from starlette import status

Expand All @@ -6,6 +8,7 @@
from app.handler.fatcory import PityResponse
from app.middleware.Jwt import UserToken
from app.models import async_session
from app.utils.internal import synchronize_async_helper
from config import Config

FORBIDDEN = "对不起, 你没有足够的权限"
Expand All @@ -15,14 +18,14 @@ class Permission:
def __init__(self, role: int = Config.MEMBER):
self.role = role

async def __call__(self, token: str = Header(...)):
def __call__(self, token: str = Header(...)):
if not token:
raise AuthException(status.HTTP_200_OK, "用户信息身份认证失败, 请检查")
try:
user_info = UserToken.parse_token(token)
if user_info.get("role", 0) < self.role:
raise PermissionException(status.HTTP_200_OK, FORBIDDEN)
user = await UserDao.query_user(user_info['id'])
user = synchronize_async_helper(UserDao.query_user(user_info['id']))
if user is None:
raise Exception("用户不存在")
user_info = PityResponse.model_to_dict(user, "password")
Expand Down
15 changes: 15 additions & 0 deletions app/utils/internal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import asyncio


def synchronize_async_helper(to_await):
async_response = []

async def run_and_capture_result():
r = await to_await
async_response.append(r)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
coroutine = run_and_capture_result()
loop.run_until_complete(coroutine)
return async_response[0]

0 comments on commit 5e4e2b9

Please sign in to comment.