permission.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from functools import wraps
  4. from typing import Callable
  5. from flask_login import UserMixin
  6. from ruoyi_common.domain.entity import LoginUser
  7. from ruoyi_common.utils import security_util as SecurityUtil
  8. from ruoyi_common.utils.base import UtilException
  9. from ruoyi_common.constant import HttpStatus
  10. class PermissionService:
  11. """
  12. 菜单权限
  13. """
  14. # 所有权限标识
  15. ALL_PERMISSION = "*:*:*"
  16. # 管理员角色权限标识
  17. SUPER_ADMIN = "admin"
  18. ROLE_DELIMETER = ","
  19. PERMISSION_DELIMETER = ","
  20. @classmethod
  21. def has_perm(cls, permission: str) -> bool:
  22. """
  23. 验证用户是否具备某权限
  24. Args:
  25. permission (str): 权限标识
  26. Returns:
  27. bool: True:具备该权限,False:不具备该权限
  28. """
  29. if not permission:
  30. return False
  31. login_user: LoginUser = SecurityUtil.get_login_user()
  32. if not login_user:
  33. return False
  34. else:
  35. if not isinstance(login_user, UserMixin):
  36. return False
  37. user_authorities = login_user.permissions
  38. if not user_authorities: return False
  39. return cls.ALL_PERMISSION in user_authorities \
  40. or permission.strip() in user_authorities
  41. @classmethod
  42. def no_perm(cls, permission: str) -> bool:
  43. """
  44. 验证用户是否不具备某权限
  45. Args:
  46. permission (str): 权限标识
  47. Returns:
  48. bool: True:不具备该权限,False:具备该权限
  49. """
  50. return not cls.has_perm(permission)
  51. @classmethod
  52. def any_perm(cls, permissions: str) -> bool:
  53. """
  54. 验证用户是否具备某权限列表中的任意一个权限
  55. Args:
  56. permissions (str): 权限标识列表,多个权限标识以逗号分隔
  57. Returns:
  58. bool: True:具备任意一个权限,False:不具备任何一个权限
  59. """
  60. if not permissions: return False
  61. login_user: LoginUser = SecurityUtil.get_login_user()
  62. if not login_user:
  63. return False
  64. else:
  65. user_authorities = login_user.permissions
  66. if not user_authorities: return False
  67. for permission in permissions.split(cls.PERMISSION_DELIMETER):
  68. if permission.strip() in user_authorities:
  69. return True
  70. return False
  71. @classmethod
  72. def has_role(cls, role: str) -> bool:
  73. """
  74. 验证用户是否具备某角色
  75. Args:
  76. role (str): 角色标识
  77. Returns:
  78. bool: True:具备该权限,False:不具备该权限
  79. """
  80. if not role:
  81. return False
  82. login_user: LoginUser = SecurityUtil.get_login_user()
  83. if not login_user or not login_user.user.roles:
  84. return False
  85. for sys_role in login_user.user.roles:
  86. if sys_role.role_key == cls.SUPER_ADMIN \
  87. or sys_role.role_key == role.strip():
  88. return True
  89. return False
  90. @classmethod
  91. def no_role(cls, role: str) -> bool:
  92. """
  93. 验证用户是否不具备某角色
  94. Args:
  95. role (str): 角色标识
  96. Returns:
  97. bool: True:具备该权限,False:不具备该权限
  98. """
  99. return not cls.has_role(role)
  100. @classmethod
  101. def any_role(cls, roles: str) -> bool:
  102. """
  103. 验证用户是否具备某角色列表中的任意一个角色
  104. Args:
  105. roles (str): 角色标识列表,多个角色标识以逗号分隔
  106. Returns:
  107. bool: True:具备任意一个角色,False:不具备任何一个角色
  108. """
  109. if not roles: return False
  110. login_user: LoginUser = SecurityUtil.get_login_user()
  111. if not login_user or not login_user.user.roles:
  112. return False
  113. for role in roles.split(cls.ROLE_DELIMETER):
  114. for sys_role in login_user.user.roles:
  115. if sys_role.role_key == cls.SUPER_ADMIN \
  116. or sys_role.role_key == role.strip():
  117. return True
  118. return False
  119. class AuthorityCaller:
  120. def __init__(self, value: str) -> None:
  121. self._value = value
  122. def __call__(self) -> bool:
  123. NotImplementedError()
  124. def LoginRequired() -> bool:
  125. """
  126. 验证用户是否登录
  127. Returns:
  128. bool -- True:已登录,False:未登录
  129. """
  130. login_user: LoginUser = SecurityUtil.get_login_user()
  131. if not login_user:
  132. return False
  133. if not login_user.is_authenticated:
  134. return False
  135. return True
  136. class HasPerm(AuthorityCaller):
  137. """
  138. 验证用户是否具备某权限
  139. """
  140. def __call__(self) -> bool:
  141. return PermissionService.has_perm(self._value)
  142. class NoPerm(AuthorityCaller):
  143. """
  144. 验证用户是否不具备某权限
  145. """
  146. def __call__(self) -> bool:
  147. return PermissionService.no_perm(self._value)
  148. class AnyPerm(AuthorityCaller):
  149. """
  150. 验证用户是否具备某权限列表中的任意一个权限
  151. """
  152. def __call__(self) -> bool:
  153. return PermissionService.any_perm(self._value)
  154. class HasRole(AuthorityCaller):
  155. """
  156. 验证用户是否具备某角色
  157. """
  158. def __call__(self) -> bool:
  159. return PermissionService.has_role(self._value)
  160. class NoRole(AuthorityCaller):
  161. """
  162. 验证用户是否不具备某角色
  163. """
  164. def __call__(self) -> bool:
  165. return PermissionService.no_role(self._role)
  166. class AnyRole(AuthorityCaller):
  167. """
  168. 验证用户是否具备某角色列表中的任意一个角色
  169. """
  170. def __call__(self) -> bool:
  171. return PermissionService.any_role(self._role)
  172. class PreAuthorize:
  173. def __init__(self, auth: AuthorityCaller | Callable):
  174. self._auth = auth
  175. def __call__(self, func) -> Callable:
  176. @wraps(func)
  177. def wrapper(*args, **kwargs):
  178. if not callable(self._auth):
  179. raise UtilException("权限验证器必须是可调用对象", HttpStatus.ERROR)
  180. if not self._auth():
  181. raise UtilException("无访问权限", HttpStatus.FORBIDDEN)
  182. return func(*args, **kwargs)
  183. return wrapper