MDL Shield

Restriction by course role

availability_role

Print Report
Plugin Information

Availability condition plugin ("Restriction by role") that lets teachers restrict course activities and sections to users holding — or not holding — a specific course, course category, or system role, including optional support for the guest and not-logged-in roles.

Privacy API
Unit Tests
Behat Tests
Reviewed:2026-11-10
14 files·2,000 lines
Grade Justification

The plugin is small and follows Moodle's availability condition API closely. No security vulnerabilities were found.

  • Role names sent to the YUI form go through core role_get_name(), which runs both the role name and any course alias through format_string(). That keeps the client-side HTML concatenation safe.
  • The condition constructor rejects any id that is not an integer, so stored JSON cannot inject values.
  • Switched roles are only honoured when the condition is evaluated for the current user, and a test covers this.
  • The site course (category 0) is handled explicitly in both the condition and the frontend.
  • The allow_add() gate uses a dedicated capability, settings are admin-only, and the null privacy provider is accurate.

The only finding is low severity: is_available() runs uncached role and guest lookups every time it is evaluated and ignores the $grabthelot hint. That is a scalability concern, not a security risk.

AI Summary

availability_role is a compact availability condition plugin: two PHP classes (condition, frontend), a settings page, a null privacy provider, one upgrade step, a YUI form module, PHPUnit tests and a Behat feature. It lets teachers restrict activities or sections by course, category or system role.

The security posture is good. Role names reach the client-side form only after core role_get_name() has run them through format_string(). The stored condition data is checked strictly (integer role id). Switched roles stored in the session are applied only when the condition is evaluated for the current user, never for another user. The capability that gates adding the condition and the admin settings for selectable roles are both wired correctly.

The one finding is a low-severity performance note. Each time is_available() runs, it issues fresh role_assignments queries, plus a guest-role lookup for guest users, and it ignores the $grabthelot hint. On course pages with many role-restricted items this adds a few queries per item on every page load. No action is needed for security.

Findings

best practiceLow
is_available() runs uncached role queries on every evaluation

Each call to condition::is_available() runs get_user_roles(), which executes a fresh SQL query against role_assignments, role and context. For users viewing a course as guest it also runs is_guest() and then get_guest_role(), which fetches the role record from the database. The $grabthelot parameter, which core sets when many items are evaluated for the same user, is ignored, and nothing is cached per request. Core rebuilds dynamic course-module data on each request, so a course page with many role-restricted activities or sections pays several queries per restricted item for every viewer.

Risk Assessment

Low risk. This affects performance only and has no security impact. The extra load grows with the number of role-restricted items per course and the number of concurrent viewers, so it matters mainly on large, busy courses.

Context

is_available() is called by core for every restricted activity or section whenever course module info is built for a user, e.g. on the course page or in navigation. The plugin applies the condition separately to each item it is attached to.

Identified Code
                    foreach (get_user_roles($context, $userid) as $role) {
                        if ($role->roleid == $this->roleid) {
                            $allow = true;
                            break;
                        }
                    }
Suggested Fix

Cache the role IDs per user and context for the request, e.g. in a static array or a request-scoped MUC cache, and reuse them across condition instances:

static $cache = [];
$key = $userid . '_' . $context->id;
if (!isset($cache[$key])) {
    $cache[$key] = array_map('intval', array_column(get_user_roles($context, $userid), 'roleid'));
}
$allow = in_array((int)$this->roleid, $cache[$key], true);
Identified Code
        $context = \context_course::instance($info->get_course()->id);
        if (is_guest($context, $userid)) {
            if (get_guest_role()->id == $this->roleid) {
                $allow = true;
            }
        }
Suggested Fix

Compare against $CFG->guestroleid (which get_guest_role() itself relies on) instead of fetching the full role record on each evaluation, falling back to get_guest_role() only when that config is empty.

Additional AI Notes

availability/role:addinstance is a UI gate, not server-side enforcement. Like the core group and grouping conditions, the plugin checks this capability only in frontend::allow_add(), which decides whether the condition button appears in the form. Core does not re-check it when restriction JSON is saved. A user who can edit activity settings could still submit a role condition by hand, including one for a role the administrator has not enabled in the plugin settings. A role condition can only restrict access, never grant it, so this is not a privilege escalation. Administrators should not treat the capability or the role allow-list as a hard boundary.

Role inheritance semantics. get_user_roles() is called with its default $checkparentcontexts = true. As a result, a condition of type course role also matches users who hold that role at category or system level, and a category role condition also matches system-level assignments. This looks intentional, consistent with how Moodle role inheritance works, but site administrators may want to know about it.

Guest account and the not-logged-in role. is_available() treats the site guest account (isguestuser()) as matching the not-logged-in role, as well as matching the guest role through is_guest(). This is a reasonable design choice but differs from core, which gives the guest account the guest role rather than the not-logged-in role.

This review was generated by an AI system and may contain inaccuracies. Findings should be verified by a human reviewer before acting on them.