permission.py 1.2 KB

12345678910111213141516171819202122232425
  1. import requests
  2. from fate_flow.db.service_registry import ServiceRegistry
  3. from fate_flow.entity.types import RegistryServiceName
  4. from fate_flow.hook import HookManager
  5. from fate_flow.hook.common.parameters import PermissionCheckParameters, PermissionReturn
  6. from fate_flow.settings import HOOK_SERVER_NAME
  7. @HookManager.register_permission_check_hook
  8. def permission(parm: PermissionCheckParameters) -> PermissionReturn:
  9. service_list = ServiceRegistry.load_service(server_name=HOOK_SERVER_NAME, service_name=RegistryServiceName.PERMISSION_CHECK.value)
  10. if not service_list:
  11. raise Exception(f"permission check error: no found server {HOOK_SERVER_NAME} service permission")
  12. service = service_list[0]
  13. response = getattr(requests, service.f_method.lower(), None)(
  14. url=service.f_url,
  15. json=parm.to_dict()
  16. )
  17. if response.status_code != 200:
  18. raise Exception(
  19. f"permission check error: request permission url failed, status code {response.status_code}")
  20. elif response.json().get("code") != 0:
  21. return PermissionReturn(code=response.json().get("code"), message=response.json().get("msg"))
  22. return PermissionReturn()