def get_resolver_object(resolvername): """ Return the cached resolver object for the given resolver name (stored in the request context). If no resolver object is cached, create it and add it to the cache. :param resolvername: the resolver string as from the token including the config as last part :return: instance of the resolver with the loaded config """ r_type = get_resolver_type(resolvername) r_obj_class = get_resolver_class(r_type) if r_obj_class is None: log.error("Can not find resolver with name {0!s} ".format(resolvername)) return None else: store = get_request_local_store() if 'resolver_objects' not in store: store['resolver_objects'] = {} resolver_objects = store['resolver_objects'] if resolvername not in resolver_objects: # create the resolver instance and load the config r_obj = resolver_objects[resolvername] = r_obj_class() if r_obj is not None: resolver_config = get_resolver_config(resolvername) r_obj.loadConfig(resolver_config) return resolver_objects[resolvername]
def get_resolver_object(resolvername): """ Return the cached resolver object for the given resolver name (stored in the request context). If no resolver object is cached, create it and add it to the cache. :param resolvername: the resolver string as from the token including the config as last part :return: instance of the resolver with the loaded config """ r_type = get_resolver_type(resolvername) r_obj_class = get_resolver_class(r_type) if r_obj_class is None: log.error( "Can not find resolver with name {0!s} ".format(resolvername)) return None else: store = get_request_local_store() if 'resolver_objects' not in store: store['resolver_objects'] = {} resolver_objects = store['resolver_objects'] if resolvername not in resolver_objects: # create the resolver instance and load the config r_obj = resolver_objects[resolvername] = r_obj_class() if r_obj is not None: resolver_config = get_resolver_config(resolvername) r_obj.loadConfig(resolver_config) return resolver_objects[resolvername]
def delete_resolver(resolvername): """ delete a resolver and all related ResolverConfig entries If there was no resolver, that could be deleted, it returns -1 :param resolvername: the name of the to be deleted resolver :type resolvername: string :return: The Id of the resolver :rtype: int """ ret = -1 reso = Resolver.query.filter_by(name=resolvername).first() if reso: if reso.realm_list: # The resolver is still contained in a realm! We must not delete it realmname = reso.realm_list[0].realm.name raise ConfigAdminError("The resolver %r is still contained in " "realm %r." % (resolvername, realmname)) reso.delete() ret = reso.id # Delete resolver object from cache store = get_request_local_store() if 'resolver_objects' in store: if resolvername in store['resolver_objects']: del store['resolver_objects'][resolvername] # Remove corresponding entries from the user cache delete_user_cache(resolver=resolvername) return ret
def update_config_object(): """ Ensure that the request-local store contains a config object. If it already contains one, check for updated configuration. :return: a ConfigClass object """ store = get_request_local_store() config_object = store['config_object'] = ConfigClass() return config_object
def test_03_request_local_store(self): store1 = get_request_local_store() store1["hello"] = "world" # We get a different store if we push another context for the same app with self.app.app_context(): store2 = get_request_local_store() self.assertNotIn("hello", store2) store2["hello"] = "hallo" store3 = get_request_local_store() self.assertEqual(store3["hello"], "hallo") store3["one"] = 1 self.assertEqual(store2["one"], 1) store1["foo"] = "bar" store4 = get_request_local_store() self.assertEqual(store4["hello"], "world") self.assertEqual(store1["hello"], "world") self.assertEqual(store1["foo"], "bar")
def get_config_object(): """ Return the request-local config object. If it does not exist yet, create it. Currently, the config object is a singleton, so it is shared among threads. :return: a ConfigClass object """ store = get_request_local_store() if 'config_object' not in store: store['config_object'] = update_config_object() return store['config_object']
def get_config_object(): """ Return the request-local config object. If it does not exist yet, create it. :return: a ``LocalConfigClass`` object """ store = get_request_local_store() if 'config_object' not in store: log.debug(u"Cloning request-local config from shared config object") shared_config = get_shared_config_object() store['config_object'] = shared_config.reload_and_clone() return store['config_object']
def register_finalizer(func): """ Register ``func`` to be called after the request has ended (this includes cases in which an error has been thrown) :param func: a function that takes no arguments """ # from http://flask.pocoo.org/snippets/53/ store = get_request_local_store() if 'call_on_teardown' not in store: store['call_on_teardown'] = [] store['call_on_teardown'].append(func)
def ensure_no_config_object(): """ Ensure that the request-local store contains no config object, and emit a warning if it does. This should only be the case if we are running tests. If the request-local store contains a config object, remove it and emit a warning. If the request-local store contains no config object, do nothing. """ store = get_request_local_store() if 'config_object' in store: log.warning(u"Request-local store already contains config object, even though it should not") del store['config_object']
def call_finalizers(): """ Call all finalizers that have been registered with the current request. Exceptions will be caught and written to the log. """ store = get_request_local_store() if 'call_on_teardown' in store: for func in store['call_on_teardown']: try: func() except Exception as exx: log.warning(u"Caught exception in finalizer: {!r}".format(exx)) log.debug(u"Exception in finalizer:", exc_info=True) store['call_on_teardown'] = []
def invalidate_config_object(): """ Invalidate the request-local config object. This is useful whenever a request modifies the configuration in the database: In this case, the request-local config object still contains the old configuration and needs to be invalidated. The same request can then create a new request-local config object, which will contain the new configuration. In other words, this function does the following: If the request-local store contains a config object, remove it. If the request-local store contains no config object, do nothing. """ store = get_request_local_store() if 'config_object' in store: log.debug(u"Invalidating request-local config object") del store['config_object']
def _get_monitoring(): """ This wrapper function creates a new monitoring object based on the config from the config file. The config file entry could look like this: PI_MONITORING_MODULE = privacyidea.lib.monitoringmodule.sqlstats Each monitoring module can have its own config values. :return: Monitoring Object """ store = get_request_local_store() # check if the monitoring object is not yet in this request_store if 'monitoring_object' not in store: config = get_app_config() monitoring_module = config.get( "PI_MONITORING_MODULE", "privacyidea.lib.monitoringmodules.sqlstats") monitoring = get_module_class(monitoring_module, "Monitoring")(config) store['monitoring_object'] = monitoring return store.get('monitoring_object')