You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					69 lines
				
				2.8 KiB
			
		
		
			
		
	
	
					69 lines
				
				2.8 KiB
			| 
											2 years ago
										 | from fastapi import Depends | ||
| 
											1 year ago
										 | from typing import List, Union | ||
|  | from exceptions.exception import PermissionException | ||
| 
											2 years ago
										 | from module_admin.entity.vo.user_vo import CurrentUserModel | ||
|  | from module_admin.service.login_service import LoginService | ||
|  | 
 | ||
|  | 
 | ||
|  | class CheckUserInterfaceAuth: | ||
|  |     """
 | ||
|  |     校验当前用户是否具有相应的接口权限 | ||
|  |     """
 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |     def __init__(self, perm: Union[str, List], is_strict: bool = False): | ||
| 
											1 year ago
										 |         """
 | ||
|  |         校验当前用户是否具有相应的接口权限 | ||
|  | 
 | ||
|  |         :param perm: 权限标识 | ||
|  |         :param is_strict: 当传入的权限标识是list类型时,是否开启严格模式,开启表示会校验列表中的每一个权限标识,所有的校验结果都需要为True才会通过 | ||
|  |         """
 | ||
| 
											2 years ago
										 |         self.perm = perm | ||
|  |         self.is_strict = is_strict | ||
| 
											2 years ago
										 | 
 | ||
|  |     def __call__(self, current_user: CurrentUserModel = Depends(LoginService.get_current_user)): | ||
|  |         user_auth_list = current_user.permissions | ||
| 
											2 years ago
										 |         if '*:*:*' in user_auth_list: | ||
| 
											2 years ago
										 |             return True | ||
| 
											2 years ago
										 |         if isinstance(self.perm, str): | ||
|  |             if self.perm in user_auth_list: | ||
|  |                 return True | ||
|  |         if isinstance(self.perm, list): | ||
|  |             if self.is_strict: | ||
|  |                 if all([perm_str in user_auth_list for perm_str in self.perm]): | ||
|  |                     return True | ||
|  |             else: | ||
|  |                 if any([perm_str in user_auth_list for perm_str in self.perm]): | ||
|  |                     return True | ||
| 
											1 year ago
										 |         raise PermissionException(data='', message='该用户无此接口权限') | ||
| 
											2 years ago
										 | 
 | ||
|  | 
 | ||
|  | class CheckRoleInterfaceAuth: | ||
|  |     """
 | ||
|  |     根据角色校验当前用户是否具有相应的接口权限 | ||
|  |     """
 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |     def __init__(self, role_key: Union[str, List], is_strict: bool = False): | ||
| 
											1 year ago
										 |         """
 | ||
|  |         根据角色校验当前用户是否具有相应的接口权限 | ||
|  | 
 | ||
|  |         :param role_key: 角色标识 | ||
|  |         :param is_strict: 当传入的角色标识是list类型时,是否开启严格模式,开启表示会校验列表中的每一个角色标识,所有的校验结果都需要为True才会通过 | ||
|  |         """
 | ||
| 
											2 years ago
										 |         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 | ||
| 
											1 year ago
										 |         raise PermissionException(data='', message='该用户无此接口权限') |