From 45c3fa18e090de7db604762ab23682a3173f404a Mon Sep 17 00:00:00 2001 From: insistence <3055204202@qq.com> Date: Sun, 18 Feb 2024 11:10:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=8C=89=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E6=A0=A1=E9=AA=8C=E6=8E=A5=E5=8F=A3=E6=9D=83=E9=99=90?= =?UTF-8?q?=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module_admin/aspect/interface_auth.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ruoyi-fastapi-backend/module_admin/aspect/interface_auth.py b/ruoyi-fastapi-backend/module_admin/aspect/interface_auth.py index 247e0b6..e688ad8 100644 --- a/ruoyi-fastapi-backend/module_admin/aspect/interface_auth.py +++ b/ruoyi-fastapi-backend/module_admin/aspect/interface_auth.py @@ -30,3 +30,30 @@ class CheckUserInterfaceAuth: if any([perm_str in user_auth_list for perm_str in self.perm]): return True raise PermissionException(data="", message="该用户无此接口权限") + + +class CheckRoleInterfaceAuth: + """ + 根据角色校验当前用户是否具有相应的接口权限 + :param role_key: 角色标识 + :param is_strict: 当传入的角色标识是list类型时,是否开启严格模式,开启表示会校验列表中的每一个角色标识,所有的校验结果都需要为True才会通过 + """ + def __init__(self, role_key: Union[str, List], is_strict: bool = False): + self.role_key = role_key + self.is_strict = is_strict + + def __call__(self, current_user: CurrentUserModel = Depends(LoginService.get_current_user)): + user_role_list = current_user.user.role + user_role_key_list = [role.role_key for role in user_role_list] + if isinstance(self.role_key, str): + if self.role_key in user_role_key_list: + return True + if isinstance(self.role_key, list): + if self.is_strict: + if all([role_key_str in user_role_key_list for role_key_str in self.role_key]): + return True + else: + if any([role_key_str in user_role_key_list for role_key_str in self.role_key]): + return True + raise PermissionException(data="", message="该用户无此接口权限") +