{"record":{"id":"0fa11833e2163f8a","repo":"moodle/moodle","slug":"contextnotallowed","errorCode":"contextnotallowed","errorMessage":"contextnotallowed","messagePattern":"contextnotallowed","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/contentbank/classes/form/upload_files.php","lineNumber":77,"sourceCode":"            $errors['file'] = get_string('userquotalimit', 'error');\n        }\n        return $errors;\n    }\n\n    /**\n     * Check if current user has access to this form, otherwise throw exception\n     *\n     * Sometimes permission check may depend on the action and/or id of the entity.\n     * If necessary, form data is available in $this->_ajaxformdata or\n     * by calling $this->optional_param()\n     */\n    protected function check_access_for_dynamic_submission(): void {\n        require_capability('moodle/contentbank:upload', $this->get_context_for_dynamic_submission());\n\n        // Check the context used by the content bank is allowed.\n        $cb = new \\core_contentbank\\contentbank();\n        if (!$cb->is_context_allowed($this->get_context_for_dynamic_submission())) {\n            throw new \\moodle_exception('contextnotallowed', 'core_contentbank');\n        }\n\n        // If $id is defined, the file content will be replaced (instead of uploading a new one).\n        // Check that the user has the right permissions to replace this content file.\n        $id = $this->optional_param('id', null, PARAM_INT);\n        if ($id) {\n            $content = $cb->get_content_from_id($id);\n            $contenttype = $content->get_content_type_instance();\n            if (!$contenttype->can_manage($content) || !$contenttype->can_upload()) {\n                throw new \\moodle_exception('nopermissions', 'error', '', null, get_string('replacecontent', 'contentbank'));\n            }\n        }\n    }\n\n    /**\n     * Returns form context\n     *\n     * If context depends on the form data, it is available in $this->_ajaxformdata or","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/contentbank/classes/form/upload_files.php#L59-L95","documentation":"The content bank upload form's check_access_for_dynamic_submission() verifies the submitted context is one the content bank is allowed to operate in via core_contentbank::is_context_allowed(). If not, it throws moodle_exception 'contextnotallowed' from core_contentbank. This guards against uploading content into system/user contexts or contexts lacking content-bank support.","triggerScenarios":"Submitting the upload_files dynamic form with a contextid parameter pointing at a context type the content bank disallows (e.g. system context, user context without enabled content bank usage).","commonSituations":"Misconfigured page parameters, crafted URLs/forms posting an arbitrary contextid, or plugins embedding the content bank in unsupported contexts.","solutions":["Pass a valid, content-bank-allowed contextid (typically a course or module context) to the form.","Check (new \\core_contentbank\\contentbank())->is_context_allowed($context) before rendering/submitting the form.","If embedding the content bank in a custom plugin, ensure the plugin's context is registered as allowed for content bank use."],"exampleFix":"// before\n$mform = new \\contentbank\\form\\upload_files(null, ['contextid' => $contextid]); // contextid=1 (system)\n// after\n$context = context::instance_by_id($contextid, MUST_EXIST);\nif (!(new \\core_contentbank\\contentbank())->is_context_allowed($context)) {\n    $contextid = $coursecontext->id; // fall back to an allowed context\n}","handlingStrategy":"validation","validationCode":"$context = context::instance_by_id($contextid, MUST_EXIST);\nif (!(new \\core_contentbank\\contentbank())->is_context_allowed($context)) {\n    throw new \\moodle_exception('contextnotallowed', 'core_contentbank');\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always derive contextid from the page/course context, never from raw user input without validation.","Call is_context_allowed() before rendering any content-bank form or link.","Restrict embedded content-bank usage to course and module contexts."],"tags":["moodle","contentbank","context","permissions"],"backgroundTag":"invalid-argument-value","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}