From 2939efff84a44d64ddbf0d7cbb4db8580e0d262a Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Tue, 6 Oct 2020 13:24:05 +0800 Subject: [PATCH 1/2] MDL-69751 course: Handle missing guest user context Makes sure the guest user context exists when fetching the recommended modules. If the context does not exist an exception is being thrown describing the problem. --- course/classes/local/service/content_item_service.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/course/classes/local/service/content_item_service.php b/course/classes/local/service/content_item_service.php index ca60efbc146..832a7eb63f8 100644 --- a/course/classes/local/service/content_item_service.php +++ b/course/classes/local/service/content_item_service.php @@ -110,7 +110,12 @@ class content_item_service { throw new \coding_exception('The guest user does not exist in the database.'); } - $favourites = $this->get_content_favourites(self::RECOMMENDATION_PREFIX, \context_user::instance($CFG->siteguest)); + // Make sure the guest user context exists. + if (!$guestusercontext = \context_user::instance($CFG->siteguest, false)) { + throw new \coding_exception('The guest user context does not exist.'); + } + + $favourites = $this->get_content_favourites(self::RECOMMENDATION_PREFIX, $guestusercontext); $recommendationcache->set($CFG->siteguest, $favourites); return $favourites; From e412f3029e69ea9590d6ae0905b8000df86ace2c Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Tue, 6 Oct 2020 13:30:45 +0800 Subject: [PATCH 2/2] MDL-69751 core: Restore deleted guest user and add missing context 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 | 36 ++++++++++++++++++++++++++++++++++++ version.php | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 412f94b1be8..c7f3e0c1d4c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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; } diff --git a/version.php b/version.php index d65f2ffd0e4..eab1c5f7209 100644 --- a/version.php +++ b/version.php @@ -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