Skip to content

Commit

Permalink
Merge branch 'MDL-80119-master' of https://github.com/andrewnicols/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Jan 23, 2024
2 parents 8811491 + 53416b1 commit 5cf7f99
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
25 changes: 18 additions & 7 deletions lib/moodlelib.php
Expand Up @@ -3648,16 +3648,27 @@ function delete_user(stdClass $user) {
$delemail = !empty($user->email) ? $user->email : $user->username . '.' . $user->id . '@unknownemail.invalid';

$deltime = time();
$deltimelength = core_text::strlen((string) $deltime);

// Max username length is 100 chars. Select up to limit - (length of current time + 1 [period character]) from users email.
$delname = clean_param($delemail, PARAM_USERNAME);
$delname = core_text::substr($delname, 0, 100 - ($deltimelength + 1)) . ".{$deltime}";
$delnameprefix = clean_param($delemail, PARAM_USERNAME);
$delnamesuffix = $deltime;
$delnamesuffixlength = 10;
do {
// Workaround for bulk deletes of users with the same email address.
$delname = sprintf(
"%s.%10d",
core_text::substr(
$delnameprefix,
0,
// 100 Character maximum, with a '.' character, and a 10-digit timestamp.
100 - 1 - $delnamesuffixlength,
),
$delnamesuffix,
);
$delnamesuffix++;

// Workaround for bulk deletes of users with the same email address.
while ($DB->record_exists('user', array('username' => $delname))) { // No need to use mnethostid here.
$delname++;
}
// No need to use mnethostid here.
} while ($DB->record_exists('user', ['username' => $delname]));

// Mark internal user record as "deleted".
$updateuser = new stdClass();
Expand Down
30 changes: 22 additions & 8 deletions webservice/lib.php
Expand Up @@ -1566,10 +1566,7 @@ protected function init_service_class() {
$rs->close();

// Generate the virtual class name.
$classname = 'webservices_virtual_class_000000';
while (class_exists($classname)) {
$classname++;
}
$classname = $this->get_unique_classname('wevservice_virtual_class');
$this->serviceclass = $classname;

// Get the list of all available external functions.
Expand Down Expand Up @@ -1618,10 +1615,7 @@ protected function generate_simple_struct_class(external_single_structure $struc
$fieldsstr = implode("\n", $fields);

// We do this after the call to get_phpdoc_type() to avoid duplicate class creation.
$classname = 'webservices_struct_class_000000';
while (class_exists($classname)) {
$classname++;
}
$classname = $this->get_unique_classname('wevservices_struct_class');
$code = <<<EOD
/**
* Virtual struct class for web services for user id $USER->id in context {$this->restricted_context->id}.
Expand Down Expand Up @@ -1779,6 +1773,26 @@ protected function get_phpdoc_type($keydesc) {
return $type;
}

/**
* Get a unique integer-suffixed classname for dynamic code creation.
*
* @param string $prefix The class name prefix to use.
* @return string The unused class name
*/
protected function get_unique_classname(string $prefix): string {
$suffix = 0;
do {
$classname = sprintf(
"%s_%06d",
$prefix,
$suffix,
);
$suffix++;
} while (class_exists($classname));

return $classname;
}

/**
* Generates the method body of the virtual external function.
*
Expand Down

0 comments on commit 5cf7f99

Please sign in to comment.