Skip to content

Commit 2dc645c

Browse files
authored
Merge pull request #839 from dravek/issue838
Migrate after_config hook callback
2 parents 3aa5842 + 385a2ee commit 2dc645c

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

classes/local/hooks/after_config.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace auth_saml2\local\hooks;
18+
19+
/**
20+
* after_config hook callback for auth_saml2
21+
*
22+
* @package auth_saml2
23+
* @copyright 2024 David Carrillo <[email protected]>
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
class after_config {
27+
/**
28+
* Callback executed from setup.php, available from Moodle 3.8 in core
29+
*
30+
* @param \core\hook\after_config $hook
31+
*/
32+
public static function callback(\core\hook\after_config $hook): void {
33+
global $CFG;
34+
35+
if (during_initial_install() || isset($CFG->upgraderunning)) {
36+
// Do nothing during installation or upgrade.
37+
return;
38+
}
39+
40+
try {
41+
$saml = optional_param('saml', null, PARAM_BOOL);
42+
if ($saml == 1) {
43+
if (isguestuser()) {
44+
// We want to force users to log in with a real account, so log guest users out.
45+
require_logout();
46+
}
47+
// We have the saml=on param set. Disable guest access (in memory -
48+
// not saved in database) to force the login with saml for this request.
49+
unset($CFG->autologinguests);
50+
}
51+
} catch (\Exception $exception) {
52+
debugging('auth_saml2_after_config error', DEBUG_DEVELOPER, $exception->getTrace());
53+
}
54+
}
55+
}

db/hooks.php

+5
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@
3030
'callback' => 'auth_saml2\local\hooks\output\before_http_headers::callback',
3131
'priority' => 0,
3232
],
33+
[
34+
'hook' => \core\hook\after_config::class,
35+
'callback' => 'auth_saml2\local\hooks\after_config::callback',
36+
'priority' => 0,
37+
],
3338
];

lib.php

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
/**
2626
* Check if we have the saml=on param set. If so, disable guest access and force the user to log in with saml.
2727
*
28+
* This is an implementation of a legacy callback that will only be called in older Moodle versions.
29+
* It will not be called in Moodle 4.5+ that contain the hook core\hook\after_config,
30+
* instead, the callback auth_saml2\local\hooks\after_config::callback will be executed.
31+
*
2832
* @since Moodle 3.8
2933
* @return void
3034
*/

0 commit comments

Comments
 (0)