moodle/moodle · error · moodle_exception

cannotoverridebaserole

cannotoverridebaserole

Error message

cannotoverridebaserole

What it means

Same guard as override.php but in admin/roles/permissions.php: when the page is asked to show role permissions/overrides at the system context (CONTEXT_SYSTEM), it throws cannotoverridebaserole because system-level capability changes belong to role definition, not per-context overrides.

Solutions

  1. Review system-level permissions via admin/roles/define.php (role capabilities) instead.
  2. Target a course/category/block/module/user context for per-context permissions.
  3. Avoid linking to permissions.php with the system context id.
  4. If front-page overrides are needed, use the front-page course context, not CONTEXT_SYSTEM.

Example fix

// before
$url = new moodle_url('/admin/roles/permissions.php', ['contextid' => SYSCONTEXTID]);
// after
if ($context->contextlevel == CONTEXT_SYSTEM) {
    $url = new moodle_url('/admin/roles/define.php', ['roleid' => $roleid]);
} else {
    $url = new moodle_url('/admin/roles/permissions.php', ['contextid' => $context->id]);
}
Defensive patterns

Strategy: validation

Validate before calling

if (context_system::instance()->id == $context->id) {
    redirect(new moodle_url('/admin/roles/define.php'));
}

Try / catch

try {
    // open permissions page for context
} catch (\moodle_exception $e) {
    if ($e->errorcode === 'cannotoverridebaserole') {
        // fall back to role definitions page
        redirect(new moodle_url('/admin/roles/define.php'));
    }
}

Prevention

When it happens

Trigger: Visiting admin/roles/permissions.php?contextid=<system context id> — the permissions/overrides review for the front-page/system context.

Common situations: Bookmark or breadcrumb link carrying the system context id; scripts enumerating contexts and calling permissions.php for each; confusion between front-page course context and the true system context.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/9ae9a18047fd1b7d. Report an issue: GitHub.

Appendix: source

Thrown at public/admin/roles/permissions.php:104

$allowoverrides     = has_capability('moodle/role:override', $context);
$allowsafeoverrides = has_capability('moodle/role:safeoverride', $context);

$contextname = $context->get_context_name();
$title = get_string('permissionsincontext', 'core_role', $contextname);
$straction = get_string('permissions', 'core_role'); // Used by tabs.php.
$currenttab = 'permissions';

$PAGE->set_pagelayout('admin');
if ($context->contextlevel == CONTEXT_BLOCK) {
    // Do not show blocks when changing block's settings, it is confusing.
    $PAGE->blocks->show_only_fake_blocks(true);
}

$PAGE->set_title($title);
$PAGE->activityheader->disable();
switch ($context->contextlevel) {
    case CONTEXT_SYSTEM:
        throw new \moodle_exception('cannotoverridebaserole', 'error');
        break;
    case CONTEXT_USER:
        $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
        $PAGE->set_heading($fullname);
        $showroles = 1;
        break;
    case CONTEXT_COURSECAT:
        core_course_category::page_setup();
        break;
    case CONTEXT_COURSE:
        if ($isfrontpage) {
            $PAGE->set_heading(get_string('frontpage', 'admin'));
        } else {
            $PAGE->set_heading($course->fullname);
        }
        break;
    case CONTEXT_MODULE:
        $PAGE->set_heading($context->get_context_name(false));

View on GitHub (pinned to 48ea8c3322)