gooderp18绿色标准版
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.

66 lines
1.9KB

  1. ##########################################################################
  2. #
  3. # pgAdmin 4 - PostgreSQL Tools
  4. #
  5. # Copyright (C) 2013 - 2020, The pgAdmin Development Team
  6. # This software is released under the PostgreSQL Licence
  7. #
  8. ##########################################################################
  9. """External Authentication Registry."""
  10. from flask_babelex import gettext
  11. from abc import ABCMeta
  12. def _decorate_cls_name(module_name):
  13. length = len(__package__) + 1
  14. if len(module_name) > length and module_name.startswith(__package__):
  15. return module_name[length:]
  16. return module_name
  17. class AuthSourceRegistry(ABCMeta):
  18. registry = None
  19. auth_sources = dict()
  20. def __init__(cls, name, bases, d):
  21. # Register this type of auth_sources, based on the module name
  22. # Avoid registering the BaseAuthentication itself
  23. AuthSourceRegistry.registry[_decorate_cls_name(d['__module__'])] = cls
  24. ABCMeta.__init__(cls, name, bases, d)
  25. @classmethod
  26. def create(cls, name, **kwargs):
  27. if name in AuthSourceRegistry.auth_sources:
  28. return AuthSourceRegistry.auth_sources[name]
  29. if name in AuthSourceRegistry.registry:
  30. AuthSourceRegistry.auth_sources[name] = \
  31. (AuthSourceRegistry.registry[name])(**kwargs)
  32. return AuthSourceRegistry.auth_sources[name]
  33. raise NotImplementedError(
  34. gettext(
  35. "Authentication source '{0}' has not been implemented."
  36. ).format(name)
  37. )
  38. @classmethod
  39. def load_auth_sources(cls):
  40. # Initialize the registry only if it has not yet been initialized
  41. if AuthSourceRegistry.registry is None:
  42. AuthSourceRegistry.registry = dict()
  43. from importlib import import_module
  44. from werkzeug.utils import find_modules
  45. for module_name in find_modules(__package__, True):
  46. import_module(module_name)
上海开阖软件有限公司 沪ICP备12045867号-1