{"record":{"id":"0bce9cd776a8f607","repo":"moodle/moodle","slug":"nocapability-0bce9c","errorCode":"nocapability","errorMessage":"nocapability","messagePattern":"nocapability","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/report/stats/user.php","lineNumber":56,"sourceCode":"$pageheading = $course->fullname;\n$userfullname = fullname($user);\nif ($courseid == SITEID) {\n    $PAGE->set_context($personalcontext);\n    $pageheading = $userfullname;\n}\n\nif ($USER->id != $user->id and has_capability('moodle/user:viewuseractivitiesreport', $personalcontext)\n        and !is_enrolled($coursecontext, $USER) and is_enrolled($coursecontext, $user)) {\n    //TODO: do not require parents to be enrolled in courses - this is a hack!\n    require_login();\n    $PAGE->set_course($course);\n} else {\n    require_login($course);\n}\n\nif (!report_stats_can_access_user_report($user, $course)) {\n    // this should never happen\n    throw new \\moodle_exception('nocapability', 'report_stats');\n}\n\n$stractivityreport = get_string('activityreport');\n\n$PAGE->set_pagelayout('report');\n$PAGE->set_url('/report/stats/user.php', array('id'=>$user->id, 'course'=>$course->id));\n$PAGE->navigation->extend_for_user($user);\n$PAGE->navigation->set_userid_for_parent_checks($user->id); // see MDL-25805 for reasons and for full commit reference for reversal when fixed.\n// Breadcrumb stuff.\n$navigationnode = array(\n        'name' => get_string('stats'),\n        'url' => new moodle_url('/report/stats/user.php', array('id' => $user->id, 'course' => $course->id))\n    );\n$PAGE->add_report_nodes($user->id, $navigationnode);\n\n$PAGE->set_title(\"$course->shortname: $stractivityreport\");\n$PAGE->set_heading($pageheading);\necho $OUTPUT->header();","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/report/stats/user.php#L38-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nif (!report_stats_can_access_user_report($user, $course)) {\n    throw new \\moodle_exception('nocapability', 'report_stats');\n}\n// after\nif (!report_stats_can_access_user_report($user, $course)) {\n    if ($user->id == $USER->id || has_capability('report/stats:view', $coursecontext)) {\n        // proceed\n    } else {\n        throw new \\moodle_exception('nocapability', 'report_stats');\n    }\n}","handlingStrategy":"validation","validationCode":"if (!has_capability('report/stats:viewuser', context_course::instance($courseid))) {\n    // skip the report link/call\n}","typeGuard":null,"tryCatchPattern":"try {\n    // call report stats page/api\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'nocapability') {\n        // show access-denied UI\n    }\n}","preventionTips":["Check capability before linking to user stats report","Refresh role caches after capability changes","Avoid impersonation scripts without require_login"],"tags":["moodle","permissions","capability"],"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"}