moodle/moodle · error · \moodle_exception
accessdenied
accessdenied
Error message
accessdenied
What it means
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.
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.
Example fix
// before
// link shown to everyone
$url = new moodle_url('/backup/view.php', ['id' => $courseid]);
echo $OUTPUT->single_button($url, get_string('coursereuse'));
// after
if (has_capability('moodle/backup:backupcourse', context_course::instance($courseid))) {
$url = new moodle_url('/backup/view.php', ['id' => $courseid]);
echo $OUTPUT->single_button($url, get_string('coursereuse'));
} Defensive patterns
Strategy: try-catch
Validate before calling
$context = context_course::instance($courseid);
$hasbackup = has_capability('moodle/backup:backupcourse', $context);
$hasrestore = has_capability('moodle/restore:restorecourse', $context);
if (!$hasbackup && !$hasrestore) {
// User has no backup actions; don't link to view.php.
} Try / catch
try {
echo $renderer->render_backup_view($courseid);
} catch (\moodle_exception $e) {
if ($e->errorcode === 'accessdenied') {
notice(get_string('nopermissions', 'error', $e->debuginfo),
new moodle_url('/course/view.php', ['id' => $courseid]));
}
throw $e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.
Related errors
AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14).
Data as JSON: /api/errors/1e2bfedb9f5fac46.
Report an issue: GitHub.
Appendix: source
Thrown at public/backup/view.php:67
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('coursereuse'));
// Check if there is at least one displayable course reuse action.
$hasactions = false;
if ($coursereusenode = $PAGE->settingsnav->find('coursereuse', \navigation_node::TYPE_CONTAINER)) {
foreach ($coursereusenode->children as $child) {
if ($child->display) {
$hasactions = true;
break;
}
}
}
if ($hasactions) {
echo $OUTPUT->render_from_template('core/report_link_page', ['node' => $coursereusenode]);
} else {
throw new \moodle_exception(
'accessdenied',
'admin',
new moodle_url('/course/view.php', ['id' => $courseid])
);
}
echo $OUTPUT->footer();
View on GitHub (pinned to 48ea8c3322)