Add user manager factory hook

This commit is contained in:
Gina Häußge 2017-07-20 20:00:21 +02:00
parent 0a7f2a209b
commit a6d3299b91
4 changed files with 68 additions and 1 deletions

View file

@ -14,4 +14,5 @@ Internal Modules
server.rst
settings.rst
slicing.rst
users.rst
util.rst

9
docs/modules/users.rst Normal file
View file

@ -0,0 +1,9 @@
.. _sec-modules-users:
octoprint.users
---------------
.. automodule:: octoprint.users
:members:

View file

@ -946,3 +946,38 @@ octoprint.ui.web.templatetypes
:param dict template_sorting: read-only dictionary of currently configured template sorting specifications
:return: a list of 3-tuples (template type, rule, sorting spec)
:rtype: list
.. _sec-plugins-hook-users-factory:
octoprint.users.factory
~~~~~~~~~~~~~~~~~~~~~~~
.. py:function:: user_manager_factory_hook(components, settings, *args, **kwargs)
Return a :class:`~octoprint.users.UserManager` instance to use as global user manager object. This will
be called only once during initial server startup.
The provided ``components`` is a dictionary containing the already initialized system components:
* ``plugin_manager``: The :class:`~octoprint.plugin.core.PluginManager`
* ``printer_profile_manager``: The :class:`~octoprint.printer.profile.PrinterProfileManager`
* ``event_bus``: The :class:`~octoprint.events.EventManager`
* ``analysis_queue``: The :class:`~octoprint.filemanager.analysis.AnalysisQueue`
* ``slicing_manager``: The :class:`~octoprint.slicing.SlicingManager`
* ``file_manager``: The :class:`~octoprint.filemanager.FileManager`
* ``app_session_manager``: The :class:`~octoprint.server.util.flask.AppSessionManager`
* ``plugin_lifecycle_manager``: The :class:`~octoprint.server.LifecycleManager`
* ``preemptive_cache``: The :class:`~octoprint.server.util.flask.PreemptiveCache`
If the factory returns anything but ``None``, it will be assigned to the global ``userManager`` instance.
If no of the registered factories return a user manager instance, the class referenced by the ``config.yaml``
entry ``accessControl.userManager`` will be initialized if possible, otherwise a stock
:class:`~octoprint.users.FilebasedUserManager` will be instantiated, linked to the default user storage
file ``~/.octoprint/users.yaml``.
:param dict components: System components to use for user manager instance initialization
:param SettingsManager settings: The global settings manager instance to fetch configuration values from if necessary
:return: The ``userManager`` instance to use globally.
:rtype: UserManager subclass or None

View file

@ -284,11 +284,33 @@ class Server(object):
file_manager=fileManager,
app_session_manager=appSessionManager,
plugin_lifecycle_manager=pluginLifecycleManager,
user_manager=userManager,
preemptive_cache=preemptiveCache,
connectivity_checker=connectivityChecker
)
# create user manager instance
user_manager_factories = pluginManager.get_hooks("octoprint.users.factory")
for name, factory in user_manager_factories.items():
try:
userManager = factory(components, self._settings)
if userManager is not None:
self._logger.debug("Created user manager instance from factory {}".format(name))
break
except:
self._logger.exception("Error while creating user manager instance from factory {}".format(name))
else:
name = self._settings.get(["accessControl", "userManager"])
try:
clazz = octoprint.util.get_class(name)
userManager = clazz()
except:
self._logger.exception(
"Could not instantiate user manager {}, falling back to FilebasedUserManager!".format(name))
userManager = octoprint.users.FilebasedUserManager()
finally:
userManager.enabled = self._settings.getBoolean(["accessControl", "enabled"])
components.update(dict(user_manager=userManager))
# create printer instance
printer_factories = pluginManager.get_hooks("octoprint.printer.factory")
for name, factory in printer_factories.items():