Skip to content

Commit 3676335

Browse files
committed
feat: 用户接口权限校验增加列表接收参数,实现同一接口支持多个权限标识校验
1 parent b9a867a commit 3676335

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import Depends
2+
from typing import Union, List
23
from module_admin.entity.vo.user_vo import CurrentUserInfoServiceResponse
34
from module_admin.service.login_service import get_current_user
45
from utils.response_util import PermissionException
@@ -7,13 +8,24 @@
78
class CheckUserInterfaceAuth:
89
"""
910
校验当前用户是否具有相应的接口权限
11+
:param perm: 权限标识
12+
:param is_strict: 当传入的权限标识是list类型时,是否开启严格模式,开启表示会校验列表中的每一个权限标识,所有的校验结果都需要为True才会通过
1013
"""
11-
def __init__(self, perm_str: str = 'common'):
12-
self.perm_str = perm_str
14+
def __init__(self, perm: Union[str, List], is_strict: bool = False):
15+
self.perm = perm
16+
self.is_strict = is_strict
1317

1418
def __call__(self, current_user: CurrentUserInfoServiceResponse = Depends(get_current_user)):
1519
user_auth_list = [item.perms for item in current_user.menu]
1620
user_auth_list.append('common')
17-
if self.perm_str in user_auth_list:
18-
return True
21+
if isinstance(self.perm, str):
22+
if self.perm in user_auth_list:
23+
return True
24+
if isinstance(self.perm, list):
25+
if self.is_strict:
26+
if all([perm_str in user_auth_list for perm_str in self.perm]):
27+
return True
28+
else:
29+
if any([perm_str in user_auth_list for perm_str in self.perm]):
30+
return True
1931
raise PermissionException(data="", message="该用户无此接口权限")

0 commit comments

Comments
 (0)