moodle/moodle · error · moodle_exception
nocapability
nocapability
Error message
nocapability
What it means
Moodle's report_stats user report page throws \moodle_exception('nocapability','report_stats') when report_stats_can_access_user_report() returns false. Access to another user's stats report requires capability (report/stats:viewuser or similar) checked against the course context; the code comments this branch as 'should never happen' because accesslib usually redirects earlier. It is a defensive guard against direct URL access without the required capability.
Solutions
- Grant the user the capability needed to view user stats reports (e.g. report/stats:viewuser) via role assignment in the course context, if access is legitimate
- Confirm the 'user' URL parameter belongs to the current user or that the role override was intended
- Clear the session/re-login after role changes so capability caches refresh
- If access should always be denied, fix the link/UI that exposes this URL to unauthorized users
Example fix
// before
if (!report_stats_can_access_user_report($user, $course)) {
throw new \moodle_exception('nocapability', 'report_stats');
}
// after
if (!report_stats_can_access_user_report($user, $course)) {
if ($user->id == $USER->id || has_capability('report/stats:view', $coursecontext)) {
// proceed
} else {
throw new \moodle_exception('nocapability', 'report_stats');
}
} Defensive patterns
Strategy: validation
Validate before calling
if (!has_capability('report/stats:viewuser', context_course::instance($courseid))) {
// skip the report link/call
} Try / catch
try {
// call report stats page/api
} catch (\moodle_exception $e) {
if ($e->errorcode === 'nocapability') {
// show access-denied UI
}
} Prevention
- Check capability before linking to user stats report
- Refresh role caches after capability changes
- Avoid impersonation scripts without require_login
When it happens
Trigger: A logged-in user requests /report/stats/user.php with a userid+courseid while lacking the capability to view other users' stats reports in that course; the pre-check elsewhere (require_login plus capability check) was bypassed or the user passed their own/mismatched user id in an unexpected state.
Common situations: Shared or bookmarked URLs to a peer's report after roles were downgraded; admin scripts impersonating users; role capability changes mid-session; custom themes linking directly to user stats URLs.
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/0bce9cd776a8f607.
Report an issue: GitHub.
Appendix: source
Thrown at public/report/stats/user.php:56
$pageheading = $course->fullname;
$userfullname = fullname($user);
if ($courseid == SITEID) {
$PAGE->set_context($personalcontext);
$pageheading = $userfullname;
}
if ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)
and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {
//TODO: do not require parents to be enrolled in courses - this is a hack!
require_login();
$PAGE->set_course($course);
} else {
require_login($course);
}
if (!report_stats_can_access_user_report($user, $course)) {
// this should never happen
throw new \moodle_exception('nocapability', 'report_stats');
}
$stractivityreport = get_string('activityreport');
$PAGE->set_pagelayout('report');
$PAGE->set_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));
$PAGE->navigation->extend_for_user($user);
$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.
// Breadcrumb stuff.
$navigationnode = array(
'name' => get_string('stats'),
'url' => new moodle_url('/report/stats/user.php', array('id' => $user->id, 'course' => $course->id))
);
$PAGE->add_report_nodes($user->id, $navigationnode);
$PAGE->set_title("$course->shortname: $stractivityreport");
$PAGE->set_heading($pageheading);
echo $OUTPUT->header();View on GitHub (pinned to 48ea8c3322)