Skip to content

Commit f4cc3b7

Browse files
author
dustinhuynh
committed
Migrate check password policy callback to hook #83
1 parent de63a19 commit f4cc3b7

File tree

7 files changed

+88
-25
lines changed

7 files changed

+88
-25
lines changed

classes/form/test_password_form.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ public function validation($data, $files) {
7373
// check pw against that account. Else, against currenlty logged in account.
7474
$testervalidation = '';
7575
if ($testpassword != '') {
76-
$testervalidation = tool_passwordvalidator_check_password_policy($testpassword, $otheruser);
76+
if (get_config('tool_passwordvalidator', 'enable_plugin')) {
77+
// If plugin is enabled, execute validation.
78+
require_once(__DIR__.'/locallib.php');
79+
$testervalidation = tool_passwordvalidator_password_validate($testpassword, $otheruser);
80+
}
7781
}
7882

7983
if (!empty($testervalidation)) {

classes/hook_callbacks.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 tool_passwordvalidator;
18+
19+
use core\hook\check_password_policy;
20+
/**
21+
* Callbacks for hooks.
22+
*
23+
* @package tool_passwordvalidator
24+
* @copyright 2025 Dustin Huynh <[email protected]>
25+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
*/
27+
class hook_callbacks {
28+
/**
29+
* Listener for the check_password_policy hook.
30+
*
31+
* @param check_password_policy $hook
32+
*/
33+
public static function check_password_policy(check_password_policy $hook): void {
34+
global $CFG;
35+
if (get_config('tool_passwordvalidator', 'enable_plugin')) {
36+
require_once($CFG->dirroot . '/admin/tool/passwordvalidator/locallib.php');
37+
// If plugin is enabled, execute validation.
38+
$error = tool_passwordvalidator_password_validate($hook->password, $hook->user);
39+
$hook->add_errors($error);
40+
}
41+
}
42+
}

db/hooks.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/**
18+
* Hook callbacks for Password Validator
19+
*
20+
* @package tool_passwordvalidator
21+
* @copyright 2025 Dustin Huynh <[email protected]>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
defined('MOODLE_INTERNAL') || die();
26+
27+
$callbacks = [
28+
[
29+
'hook' => \core\hook\check_password_policy::class,
30+
'callback' => [\tool_passwordvalidator\hook_callbacks::class, 'check_password_policy'],
31+
],
32+
];

lang/en/tool_passwordvalidator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424

2525
$string['pluginname'] = 'Password validator';
26-
26+
$string['configpasswordcheckonlogin'] = 'It appears that the "Check password on login" (passwordpolicycheckonlogin) control is disabled. This plugin cannot function correctly without it.';
2727
$string['configpasswordpolicy'] = 'It appears that the "Password Policy" control is disabled. If this control is disabled, new users will not be able to view information about the password policy when setting their password.';
2828
$string['configpasswordrotationempty'] = 'It appears that the current password rotation limit is 0. This plugin relies on this configuration being set to atleast 1 for the password reset lockout period.
2929
It is recommended to set this value to atleast 1, but higher is better. ACSC recommends not reusing passwords within 8 changes.';
@@ -88,7 +88,6 @@
8888
$string['passwordtesterpass'] = 'Pass: Tester password passed validation settings. ';
8989
$string['passwordtesterfail'] = 'Fail: Tester password failed validation settings: <br>';
9090
$string['passwordtesterempty'] = 'No password entered to test.';
91-
9291
$string['responseminimumlength'] = 'Password must have at least {$a} characters.';
9392
$string['responsenoletters'] = 'Password cannot consist of only numbers and/or special characters, or contain no letters.';
9493
$string['responsedictionaryfailoneword'] = 'Password cannot be based of a single dictionary word: {$a}, consider adding more words.';

lib.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,6 @@
2323
*/
2424
defined('MOODLE_INTERNAL') || die;
2525

26-
/**
27-
* Wrapper function for the password validation. Simply calls password validate
28-
* with test mode disabled.
29-
*
30-
* @param string $password The password to be validated.
31-
* @param stdClass $user A user object to perform validation against if preset. Defaults to null
32-
* @return string Returns a string of any errors presented by the checks, or an empty string for success.
33-
*
34-
*/
35-
36-
function tool_passwordvalidator_check_password_policy($password, $user = null) {
37-
if (get_config('tool_passwordvalidator', 'enable_plugin')) {
38-
// If plugin is enabled, execute validation.
39-
require_once(__DIR__.'/locallib.php');
40-
return tool_passwordvalidator_password_validate($password, $user);
41-
} else {
42-
// Empty, passed validation.
43-
return '';
44-
}
45-
}
4626

4727
/**
4828
* Function for the printing of the password policy

locallib.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,12 @@ function tool_passwordvalidator_config_checker() {
472472
}
473473
}
474474

475+
// Check if password check on login is enabled.
476+
if ($CFG->passwordpolicycheckonlogin != 1) {
477+
$response .= get_string('configpasswordcheckonlogin', 'tool_passwordvalidator').'<br>';
478+
$type = 'notifyerror';
479+
}
480+
475481
// Minimum length enforcement is a fail.
476482
if (($CFG->passwordpolicy == 1) && $CFG->minpasswordlength >= 1) {
477483
$response .= get_string('configpasswordminlength', 'tool_passwordvalidator').'<br>';

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
defined('MOODLE_INTERNAL') || die();
2626

27-
$plugin->version = 2024111402;
28-
$plugin->release = $plugin->version;
27+
$plugin->version = 2025082100;
28+
$plugin->release = 2025082100;
2929
$plugin->requires = 2016052300;
3030
$plugin->component = 'tool_passwordvalidator';
3131
$plugin->maturity = MATURITY_STABLE;

0 commit comments

Comments
 (0)