Skip to content

Commit 3515d30

Browse files
committed
feat: 新增按角色校验接口权限依赖
1 parent 3676335 commit 3515d30

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

dash-fastapi-backend/module_admin/aspect/interface_auth.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,29 @@ def __call__(self, current_user: CurrentUserInfoServiceResponse = Depends(get_cu
2929
if any([perm_str in user_auth_list for perm_str in self.perm]):
3030
return True
3131
raise PermissionException(data="", message="该用户无此接口权限")
32+
33+
34+
class CheckRoleInterfaceAuth:
35+
"""
36+
根据角色校验当前用户是否具有相应的接口权限
37+
:param role_key: 角色标识
38+
:param is_strict: 当传入的角色标识是list类型时,是否开启严格模式,开启表示会校验列表中的每一个角色标识,所有的校验结果都需要为True才会通过
39+
"""
40+
def __init__(self, role_key: Union[str, List], is_strict: bool = False):
41+
self.role_key = role_key
42+
self.is_strict = is_strict
43+
44+
def __call__(self, current_user: CurrentUserInfoServiceResponse = Depends(get_current_user)):
45+
user_role_list = current_user.role
46+
user_role_key_list = [role.role_key for role in user_role_list]
47+
if isinstance(self.role_key, str):
48+
if self.role_key in user_role_key_list:
49+
return True
50+
if isinstance(self.role_key, list):
51+
if self.is_strict:
52+
if all([role_key_str in user_role_key_list for role_key_str in self.role_key]):
53+
return True
54+
else:
55+
if any([role_key_str in user_role_key_list for role_key_str in self.role_key]):
56+
return True
57+
raise PermissionException(data="", message="该用户无此接口权限")

0 commit comments

Comments
 (0)