Merge branch 'w38_MDL-32572_m24_authdbduplicates' of git://github.com/skodak/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 18 Sep 2012 17:51:24 +0000 (19:51 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 18 Sep 2012 17:51:24 +0000 (19:51 +0200)
auth/db/auth.php
auth/db/lang/en/auth_db.php

index f72b318..60babc8 100644 (file)
@@ -65,7 +65,7 @@ class auth_plugin_db extends auth_plugin_base {
                 $authdb->Close();
                 // user exists externally
                 // check username/password internally
-                if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
+                if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id, 'auth'=>$this->authtype))) {
                     return validate_internal_user_password($user, $password);
                 }
             } else {
@@ -191,8 +191,16 @@ class auth_plugin_db extends auth_plugin_base {
      * @return bool                  True on success
      */
     function user_update_password($user, $newpassword) {
+        global $DB;
+
         if ($this->is_internal()) {
-            return update_internal_user_password($user, $newpassword);
+            $puser = $DB->get_record('user', array('id'=>$user->id), '*', MUST_EXIST);
+            if (update_internal_user_password($puser, $newpassword)) {
+                $user->password = $puser->password;
+                return true;
+            } else {
+                return false;
+            }
         } else {
             // we should have never been called!
             return false;
@@ -356,7 +364,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 +390,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 +405,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;
index 6f42064..76967ea 100644 (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';