From 01fbadf2f4b6e8a15918624f5930ac5ed95ff2e3 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Fri, 30 Aug 2024 15:23:36 +0800 Subject: [PATCH] MDL-82958 user: Return general error if course/context not valid --- user/index.php | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/user/index.php b/user/index.php index 1c93977b1651d..d3cae1fca5732 100644 --- a/user/index.php +++ b/user/index.php @@ -52,16 +52,21 @@ 'id' => $courseid, 'newcourse' => $newcourse)); -if ($contextid) { - $context = context::instance_by_id($contextid, MUST_EXIST); - if ($context->contextlevel != CONTEXT_COURSE) { - throw new \moodle_exception('invalidcontext'); +try { + if ($contextid) { + $context = context::instance_by_id($contextid, MUST_EXIST); + if ($context->contextlevel != CONTEXT_COURSE) { + throw new \moodle_exception('invalidcontext'); + } + $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST); + } else { + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $context = context_course::instance($course->id, MUST_EXIST); } - $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST); -} else { - $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); - $context = context_course::instance($course->id, MUST_EXIST); +} catch (\dml_exception $e) { + throw new \moodle_exception('invaliddata'); } + // Not needed anymore. unset($contextid); unset($courseid);