{"record":{"id":"1e2bfedb9f5fac46","repo":"moodle/moodle","slug":"accessdenied-1e2bfe","errorCode":"accessdenied","errorMessage":"accessdenied","messagePattern":"accessdenied","errorType":"error_code","errorClass":"\\moodle_exception","httpStatus":null,"severity":"error","filePath":"public/backup/view.php","lineNumber":67,"sourceCode":"\necho $OUTPUT->header();\necho $OUTPUT->heading(get_string('coursereuse'));\n\n// Check if there is at least one displayable course reuse action.\n$hasactions = false;\nif ($coursereusenode = $PAGE->settingsnav->find('coursereuse', \\navigation_node::TYPE_CONTAINER)) {\n    foreach ($coursereusenode->children as $child) {\n        if ($child->display) {\n            $hasactions = true;\n            break;\n        }\n    }\n}\n\nif ($hasactions) {\n    echo $OUTPUT->render_from_template('core/report_link_page', ['node' => $coursereusenode]);\n} else {\n    throw new \\moodle_exception(\n        'accessdenied',\n        'admin',\n        new moodle_url('/course/view.php', ['id' => $courseid])\n    );\n}\necho $OUTPUT->footer();\n","sourceCodeStart":49,"sourceCodeEnd":74,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/backup/view.php#L49-L74","documentation":"At the end of backup/view.php, the page builds a navigation node of allowed backup/restore 'actions' filtered by the user's capabilities. If $hasactions is false — the user lacks every capability the page checks — it throws moodle_exception 'accessdenied' with the 'admin' string and redirects back to the course page. It is a deliberate authorization gate, not a bug.","triggerScenarios":"A logged-in user with no backup-related capabilities (e.g. moodle/backup:backupactivity, moodle/restore:restorecourse, moodle/site:managefiles etc. as checked by the page's node building) opens /backup/view.php?id=..., so $coursereusenode ends up with no children and $hasactions stays false.","commonSituations":"Student or non-editing teacher following a shared link; role changes or capability overrides that removed backup permissions; testing as a user without manager/teacher roles; a course where all backup actions were disabled by role configuration.","solutions":["Grant the user the required capability (e.g. moodle/backup:backupcourse or moodle/restore:restorecourse) via the course or system role definitions.","Log in as a manager/teacher with backup permissions, or ask an admin for access.","Check Site administration > Users > Permissions and any capability overrides on the course context that removed the backup actions.","Accept it as intended behavior: without any backup/restore capability the page is unusable, hence the redirect to the course page."],"exampleFix":"// before\n// link shown to everyone\n$url = new moodle_url('/backup/view.php', ['id' => $courseid]);\necho $OUTPUT->single_button($url, get_string('coursereuse'));\n\n// after\nif (has_capability('moodle/backup:backupcourse', context_course::instance($courseid))) {\n    $url = new moodle_url('/backup/view.php', ['id' => $courseid]);\n    echo $OUTPUT->single_button($url, get_string('coursereuse'));\n}","handlingStrategy":"try-catch","validationCode":"$context = context_course::instance($courseid);\n$hasbackup = has_capability('moodle/backup:backupcourse', $context);\n$hasrestore = has_capability('moodle/restore:restorecourse', $context);\nif (!$hasbackup && !$hasrestore) {\n    // User has no backup actions; don't link to view.php.\n}","typeGuard":null,"tryCatchPattern":"try {\n    echo $renderer->render_backup_view($courseid);\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'accessdenied') {\n        notice(get_string('nopermissions', 'error', $e->debuginfo),\n            new moodle_url('/course/view.php', ['id' => $courseid]));\n    }\n    throw $e;\n}","preventionTips":["Check capabilities with has_capability() before showing links to /backup/view.php.","Audit role definitions after upgrades to confirm backup capabilities were not removed.","Avoid sharing backup page links with users who hold no backup-related roles.","Test pages as a low-privileged user to catch capability gates early."],"tags":["moodle","permissions","capability","access-control"],"backgroundTag":"permission-denied","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"}