MDL-69079 core: Restore and set missing guest user
authorMihail Geshoski <mihail@moodle.com>
Fri, 24 Jul 2020 03:08:25 +0000 (11:08 +0800)
committerMihail Geshoski <mihail@moodle.com>
Fri, 24 Jul 2020 03:11:42 +0000 (11:11 +0800)
lib/db/upgrade.php
version.php

index 1df218e..32298db 100644 (file)
@@ -2497,5 +2497,35 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2020061501.01);
     }
 
+    if ($oldversion < 2020061501.04) {
+        // Restore and set the guest user if it has been previously removed via GDPR, or set to an nonexistent
+        // user account.
+        $currentguestuser = $DB->get_record('user', array('id' => $CFG->siteguest));
+
+        if (!$currentguestuser) {
+            if (!$guest = $DB->get_record('user', array('username' => 'guest', 'mnethostid' => $CFG->mnet_localhost_id))) {
+                // Create a guest user account.
+                $guest = new stdClass();
+                $guest->auth        = 'manual';
+                $guest->username    = 'guest';
+                $guest->password    = hash_internal_user_password('guest');
+                $guest->firstname   = get_string('guestuser');
+                $guest->lastname    = ' ';
+                $guest->email       = 'root@localhost';
+                $guest->description = get_string('guestuserinfo');
+                $guest->mnethostid  = $CFG->mnet_localhost_id;
+                $guest->confirmed   = 1;
+                $guest->lang        = $CFG->lang;
+                $guest->timemodified= time();
+                $guest->id = $DB->insert_record('user', $guest);
+            }
+            // Set the guest user.
+            set_config('siteguest', $guest->id);
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2020061501.04);
+    }
+
     return true;
 }
index ccb3c81..8779403 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2020061501.03;              // 20200615      = branching date YYYYMMDD - do not modify!
+$version  = 2020061501.04;              // 20200615      = branching date YYYYMMDD - do not modify!
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '3.9.1+ (Build: 20200723)'; // Human-friendly version name