From 617f1b35b3d2a5da6e24cbe3536ee868f1fe3dda Mon Sep 17 00:00:00 2001 From: Loei Petrus Marogi Date: Thu, 11 Jul 2019 13:32:45 +0200 Subject: [PATCH] Add possibility to let hooks always run refs #3772 --- library/Icinga/Application/Hook.php | 24 +++++++++++-------- library/Icinga/Application/Modules/Module.php | 6 ++--- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/library/Icinga/Application/Hook.php b/library/Icinga/Application/Hook.php index 85d18a56d..b3d12fe8a 100644 --- a/library/Icinga/Application/Hook.php +++ b/library/Icinga/Application/Hook.php @@ -76,7 +76,8 @@ class Hook } foreach (self::$hooks[$name] as $hook) { - if (self::hasPermission($hook)) { + list($class, $alwaysRun) = $hook; + if ($alwaysRun || self::hasPermission($class)) { return true; } } @@ -120,7 +121,7 @@ class Hook return self::$instances[$name][$key]; } - $class = self::$hooks[$name][$key]; + $class = self::$hooks[$name][$key][0]; if (! class_exists($class)) { throw new ProgrammingError( @@ -273,7 +274,8 @@ class Hook } foreach (self::$hooks[$name] as $key => $hook) { - if (self::hasPermission($hook)) { + list($class, $alwaysRun) = $hook; + if ($alwaysRun || self::hasPermission($class)) { if (self::createInstance($name, $key) === null) { return array(); } @@ -296,7 +298,8 @@ class Hook if (self::has($name)) { foreach (self::$hooks[$name] as $key => $hook) { - if (self::hasPermission($hook)) { + list($class, $alwaysRun) = $hook; + if ($alwaysRun || self::hasPermission($class)) { return self::createInstance($name, $key); } } @@ -306,12 +309,13 @@ class Hook /** * Register a class * - * @param string $name One of the predefined hook names - * @param string $key The identifier of a specific subtype - * @param string $class Your class name, must inherit one of the - * classes in the Icinga/Application/Hook folder + * @param string $name One of the predefined hook names + * @param string $key The identifier of a specific subtype + * @param string $class Your class name, must inherit one of the + * classes in the Icinga/Application/Hook folder + * @param bool $alwaysRun To run the hook always (e.g. without permission check) */ - public static function register($name, $key, $class) + public static function register($name, $key, $class, $alwaysRun = false) { $name = self::normalizeHookName($name); @@ -321,6 +325,6 @@ class Hook $class = ltrim($class, ClassLoader::NAMESPACE_SEPARATOR); - self::$hooks[$name][$key] = $class; + self::$hooks[$name][$key] = [$class, $alwaysRun]; } } diff --git a/library/Icinga/Application/Modules/Module.php b/library/Icinga/Application/Modules/Module.php index 8049217b2..450de7ded 100644 --- a/library/Icinga/Application/Modules/Module.php +++ b/library/Icinga/Application/Modules/Module.php @@ -1278,11 +1278,11 @@ class Module * @param string $implementation Fully qualified name of the class providing the hook implementation. * Defaults to the module's ProvidedHook namespace plus the hook's name for the * class name - * @param string $deprecated DEPRECATED - No-op arg for compatibility reasons + * @param bool $alwaysRun To run the hook always (e.g. without permission check) * * @return $this */ - protected function provideHook($name, $implementation = null, $deprecated = null) + protected function provideHook($name, $implementation = null, $alwaysRun = false) { if ($implementation === null) { $implementation = $name; @@ -1296,7 +1296,7 @@ class Module $class = $implementation; } - Hook::register($name, $class, $class); + Hook::register($name, $class, $class, $alwaysRun); return $this; }