MDL-32572 detect username conflicts in auth_db sync

This commit is contained in:
Petr Škoda 2012-09-18 12:35:08 +02:00
parent a0a5ca2578
commit bee0220935
2 changed files with 11 additions and 2 deletions

View File

@ -356,7 +356,7 @@ class auth_plugin_db extends auth_plugin_base {
if ($verbose) {
mtrace(get_string('auth_dbuserstoadd','auth_db',count($add_users)));
}
$transaction = $DB->start_delegated_transaction();
// Do not use transactions around this foreach, we want to skip problematic users, not revert everything.
foreach($add_users as $user) {
$username = $user;
if ($this->config->removeuser == AUTH_REMOVEUSER_SUSPEND) {
@ -382,6 +382,12 @@ class auth_plugin_db extends auth_plugin_base {
}
$user->timecreated = time();
$user->timemodified = $user->timecreated;
if ($collision = $DB->get_record_select('user', "username = :username AND mnethostid = :mnethostid AND auth <> :auth", array('username'=>$user->username, 'mnethostid'=>$CFG->mnet_localhost_id, 'auth'=>$this->authtype), 'id,username,auth')) {
if ($verbose) {
mtrace("\t".get_string('auth_dbinsertuserduplicate', 'auth_db', array('username'=>$user->username, 'auth'=>$collision->auth)));
}
continue;
}
try {
$id = $DB->insert_record ('user', $user); // it is truly a new user
if ($verbose) {
@ -391,14 +397,16 @@ class auth_plugin_db extends auth_plugin_base {
if ($verbose) {
mtrace("\t".get_string('auth_dbinsertusererror', 'auth_db', $user->username));
}
continue;
}
// if relevant, tag for password generation
if ($this->is_internal()) {
set_user_preference('auth_forcepasswordchange', 1, $id);
set_user_preference('create_password', 1, $id);
}
// Make sure user context is present.
context_user::instance($id);
}
$transaction->allow_commit();
unset($add_users); // free mem
}
return 0;

View File

@ -40,6 +40,7 @@ $string['auth_dbhost'] = 'The computer hosting the database server.';
$string['auth_dbhost_key'] = 'Host';
$string['auth_dbchangepasswordurl_key'] = 'Password-change URL';
$string['auth_dbinsertuser'] = 'Inserted user {$a->name} id {$a->id}';
$string['auth_dbinsertuserduplicate'] = 'Error inserting user {$a->username} - user with this username was already created through \'{$a->auth}\' plugin.';
$string['auth_dbinsertusererror'] = 'Error inserting user {$a}';
$string['auth_dbname'] = 'Name of the database itself';
$string['auth_dbname_key'] = 'DB name';