From 941aab6e63b876b8b6a627314b91b3c364aba2ef Mon Sep 17 00:00:00 2001 From: lkearn Date: Sun, 26 Feb 2023 14:53:47 +1100 Subject: [PATCH] feat: Introduce gated functionality for use in Totara (17.3+) without patches. --- classes/watcher/login_watcher.php | 59 +++++++++++++++++++++++++++++++ db/hooks.php | 45 +++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 classes/watcher/login_watcher.php create mode 100644 db/hooks.php diff --git a/classes/watcher/login_watcher.php b/classes/watcher/login_watcher.php new file mode 100644 index 00000000..bd1c693e --- /dev/null +++ b/classes/watcher/login_watcher.php @@ -0,0 +1,59 @@ +. + +namespace tool_mfa\watcher; + +defined('MOODLE_INTERNAL') || die(); + +use core\hook\after_require_login as after_require_login_hook; +use core\hook\after_config as after_config_hook; + +if (class_exists('\totara_core\hook\base')) { + require_once($CFG->dirroot . '/admin/tool/mfa/lib.php'); + + /** + * Watches Totara hooks to ensure a user is authenticated. + * + * Forwards on the events until: + * `TODO - PLATFORM-117 to create a watcher to enable those functions via this hook` + * + * @package tool_mfa + * @author Liam Kearney + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + class login_watcher { + /** + * Forwards calls to tool_mfa_after_require_login. + */ + public static function ensure_authenticated(after_require_login_hook $hook) { + tool_mfa_after_require_login( + $hook->courseorid, + $hook->autologinguest, + $hook->cm, + $hook->setwantsurltome, + $hook->preventredirect + ); + } + + /** + * Forwards calls to tool_mfa_after_config. + */ + public static function check_authenticated(after_config_hook $hook) { + tool_mfa_after_config(); + } + } +} diff --git a/db/hooks.php b/db/hooks.php new file mode 100644 index 00000000..f772192e --- /dev/null +++ b/db/hooks.php @@ -0,0 +1,45 @@ +. + +/** + * Definition of MFA sub-plugins (factors). + * + * @package tool_mfa + * @author Liam Kearney + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +// Hooks are Totara specific. +if (class_exists('\totara_core\hook\base')) { + $watchers = [ + [ + 'hookname' => '\core\hook\after_require_login', + 'callback' => '\tool_mfa\watcher\login_watcher::ensure_authenticated', + 'includefile' => null, + 'priority' => 100, + ], + [ + 'hookname' => '\core\hook\after_config', + 'callback' => '\tool_mfa\watcher\login_watcher::check_authenticated', + 'includefile' => null, + 'priority' => 100, + ], + ]; +} +