Skip to content

Commit c051057

Browse files
David Monllaostronk7
authored andcommitted
Merge branch 'wip-MDL-61694_Check_Password_Callback' of https://github.com/Spudley/moodle
2 parents 5870677 + 99405aa commit c051057

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

lib/moodlelib.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,32 +4749,38 @@ function get_complete_user_data($field, $value, $mnethostid = null) {
47494749
function check_password_policy($password, &$errmsg) {
47504750
global $CFG;
47514751

4752-
if (empty($CFG->passwordpolicy)) {
4753-
return true;
4754-
}
4755-
4756-
$errmsg = '';
4757-
if (core_text::strlen($password) < $CFG->minpasswordlength) {
4758-
$errmsg .= '<div>'. get_string('errorminpasswordlength', 'auth', $CFG->minpasswordlength) .'</div>';
4759-
4760-
}
4761-
if (preg_match_all('/[[:digit:]]/u', $password, $matches) < $CFG->minpassworddigits) {
4762-
$errmsg .= '<div>'. get_string('errorminpassworddigits', 'auth', $CFG->minpassworddigits) .'</div>';
4763-
4764-
}
4765-
if (preg_match_all('/[[:lower:]]/u', $password, $matches) < $CFG->minpasswordlower) {
4766-
$errmsg .= '<div>'. get_string('errorminpasswordlower', 'auth', $CFG->minpasswordlower) .'</div>';
4767-
4752+
if (!empty($CFG->passwordpolicy)) {
4753+
$errmsg = '';
4754+
if (core_text::strlen($password) < $CFG->minpasswordlength) {
4755+
$errmsg .= '<div>'. get_string('errorminpasswordlength', 'auth', $CFG->minpasswordlength) .'</div>';
4756+
}
4757+
if (preg_match_all('/[[:digit:]]/u', $password, $matches) < $CFG->minpassworddigits) {
4758+
$errmsg .= '<div>'. get_string('errorminpassworddigits', 'auth', $CFG->minpassworddigits) .'</div>';
4759+
}
4760+
if (preg_match_all('/[[:lower:]]/u', $password, $matches) < $CFG->minpasswordlower) {
4761+
$errmsg .= '<div>'. get_string('errorminpasswordlower', 'auth', $CFG->minpasswordlower) .'</div>';
4762+
}
4763+
if (preg_match_all('/[[:upper:]]/u', $password, $matches) < $CFG->minpasswordupper) {
4764+
$errmsg .= '<div>'. get_string('errorminpasswordupper', 'auth', $CFG->minpasswordupper) .'</div>';
4765+
}
4766+
if (preg_match_all('/[^[:upper:][:lower:][:digit:]]/u', $password, $matches) < $CFG->minpasswordnonalphanum) {
4767+
$errmsg .= '<div>'. get_string('errorminpasswordnonalphanum', 'auth', $CFG->minpasswordnonalphanum) .'</div>';
4768+
}
4769+
if (!check_consecutive_identical_characters($password, $CFG->maxconsecutiveidentchars)) {
4770+
$errmsg .= '<div>'. get_string('errormaxconsecutiveidentchars', 'auth', $CFG->maxconsecutiveidentchars) .'</div>';
4771+
}
47684772
}
4769-
if (preg_match_all('/[[:upper:]]/u', $password, $matches) < $CFG->minpasswordupper) {
4770-
$errmsg .= '<div>'. get_string('errorminpasswordupper', 'auth', $CFG->minpasswordupper) .'</div>';
47714773

4772-
}
4773-
if (preg_match_all('/[^[:upper:][:lower:][:digit:]]/u', $password, $matches) < $CFG->minpasswordnonalphanum) {
4774-
$errmsg .= '<div>'. get_string('errorminpasswordnonalphanum', 'auth', $CFG->minpasswordnonalphanum) .'</div>';
4775-
}
4776-
if (!check_consecutive_identical_characters($password, $CFG->maxconsecutiveidentchars)) {
4777-
$errmsg .= '<div>'. get_string('errormaxconsecutiveidentchars', 'auth', $CFG->maxconsecutiveidentchars) .'</div>';
4774+
// Fire any additional password policy functions from plugins.
4775+
// Plugin functions should output an error message string or empty string for success.
4776+
$pluginsfunction = get_plugins_with_function('check_password_policy');
4777+
foreach ($pluginsfunction as $plugintype => $plugins) {
4778+
foreach ($plugins as $pluginfunction) {
4779+
$pluginerr = $pluginfunction($password);
4780+
if ($pluginerr) {
4781+
$errmsg .= '<div>'. $pluginerr .'</div>';
4782+
}
4783+
}
47784784
}
47794785

47804786
if ($errmsg == '') {

0 commit comments

Comments
 (0)