MDL-69751 core: Restore deleted guest user and add missing context
authorMihail Geshoski <mihail@moodle.com>
Tue, 6 Oct 2020 05:30:45 +0000 (13:30 +0800)
committerMihail Geshoski <mihail@moodle.com>
Tue, 20 Oct 2020 00:37:44 +0000 (08:37 +0800)
This upgrade step fixes the cases where the current guest user is
labelled as 'deleted' and the related user context is missing.

lib/db/upgrade.php
version.php

index 412f94b..c7f3e0c 100644 (file)
@@ -2782,5 +2782,41 @@ function xmldb_main_upgrade($oldversion) {
         upgrade_main_savepoint(true, 2021052500.24);
     }
 
+    if ($oldversion < 2021052500.26) {
+        // Get the current guest user which is also set as 'deleted'.
+        $guestuser = $DB->get_record('user', ['id' => $CFG->siteguest, 'deleted' => 1]);
+        // If there is a deleted guest user, reset the user to not be deleted and make sure the related
+        // user context exists.
+        if ($guestuser) {
+            $guestuser->deleted = 0;
+            $DB->update_record('user', $guestuser);
+
+            // Get the guest user context.
+            $guestusercontext = $DB->get_record('context',
+                ['contextlevel' => CONTEXT_USER, 'instanceid' => $guestuser->id]);
+
+            // If the guest user context does not exist, create it.
+            if (!$guestusercontext) {
+                $record = new stdClass();
+                $record->contextlevel = CONTEXT_USER;
+                $record->instanceid = $guestuser->id;
+                $record->depth = 0;
+                // The path is not known before insert.
+                $record->path = null;
+                $record->locked = 0;
+
+                $record->id = $DB->insert_record('context', $record);
+
+                // Update the path.
+                $record->path = '/' . SYSCONTEXTID . '/' . $record->id;
+                $record->depth = substr_count($record->path, '/');
+                $DB->update_record('context', $record);
+            }
+        }
+
+        // Main savepoint reached.
+        upgrade_main_savepoint(true, 2021052500.26);
+    }
+
     return true;
 }
index d65f2ff..eab1c5f 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2021052500.25;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2021052500.26;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '4.0dev (Build: 20201016)'; // Human-friendly version name