MDL-79011 core: Convert after_config callback to hook

This commit is contained in:
Andrew Nicols 2024-03-08 14:27:25 +08:00
parent e0a9cf2288
commit fc7945cbc3
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
6 changed files with 137 additions and 11 deletions

View File

@ -0,0 +1,7 @@
issueNumber: MDL-79011
notes:
core:
- message: >-
The `after_config()` callback has been converted to a hook,
`\core\hook\after_config`.
type: improved

View File

@ -0,0 +1,56 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace tool_mfa;
use core\hook\after_config;
/**
* Callbacks for hooks.
*
* @package tool_mfa
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class hook_callbacks {
/**
* Listener for the after_config hook.
*
* @param after_config $hook
*/
public static function after_config(\core\hook\after_config $hook): void {
global $CFG, $SESSION;
if (during_initial_install() || isset($CFG->upgraderunning)) {
// Do nothing during installation or upgrade.
return;
}
// Tests for hooks being fired to test patches.
// Store in $CFG, $SESSION not present at this point.
if (PHPUNIT_TEST) {
$CFG->mfa_config_hook_test = true;
}
// Check for not logged in.
if (isloggedin() && !isguestuser()) {
// If not authenticated, force login required.
if (empty($SESSION->tool_mfa_authenticated)) {
\tool_mfa\manager::require_auth();
}
}
}
}

View File

@ -27,7 +27,11 @@ defined('MOODLE_INTERNAL') || die();
$callbacks = [
[
'hook' => core_user\hook\extend_bulk_user_actions::class,
'callback' => 'tool_mfa\local\hooks\extend_bulk_user_actions::callback',
'callback' => [\tool_mfa\local\hooks\extend_bulk_user_actions::class, 'callback'],
'priority' => 0,
],
[
'hook' => \core\hook\after_config::class,
'callback' => [\tool_mfa\hook_callbacks::class, 'after_config'],
],
];

View File

@ -106,6 +106,23 @@ function tool_mfa_after_config(): void {
}
}
/**
* Any plugin typically an admin tool can add new bulk user actions
*
* @return array
*/
function tool_mfa_bulk_user_actions(): array {
if (!has_capability('moodle/site:config', context_system::instance())) {
return [];
}
return [
'tool_mfa_reset_factors' => new action_link(
new moodle_url('/admin/tool/mfa/reset_factor.php'),
get_string('resetfactor', 'tool_mfa'),
),
];
}
/**
* Serves any files for the guidance page.
*

View File

@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\hook;
/**
* Class after_config
*
* @package core
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\tags('configuration', 'core')]
#[\core\attribute\label('Allows plugins to perform actions immediately after configuration')]
#[\core\attribute\hook\replaces_callbacks('after_config')]
class after_config {
/**
* Process legacy callbacks.
*/
public function process_legacy_callbacks(): void {
$pluginswithfunction = get_plugins_with_function(
function: 'after_config',
migratedtohook: true,
);
foreach ($pluginswithfunction as $plugins) {
foreach ($plugins as $function) {
try {
$function();
} catch (\Throwable $e) {
debugging("Exception calling '$function'", DEBUG_DEVELOPER, $e->getTrace());
}
}
}
}
}

View File

@ -1195,13 +1195,6 @@ if (false) {
initialise_local_config_cache();
// Allow plugins to callback as soon possible after setup.php is loaded.
$pluginswithfunction = get_plugins_with_function('after_config', 'lib.php');
foreach ($pluginswithfunction as $plugins) {
foreach ($plugins as $function) {
try {
$function();
} catch (Throwable $e) {
debugging("Exception calling '$function'", DEBUG_DEVELOPER, $e->getTrace());
}
}
}
$afterconfighook = new \core\hook\after_config();
$afterconfighook->process_legacy_callbacks();
\core\di::get(\core\hook\manager::class)->dispatch($afterconfighook);