{"record":{"id":"bdf3efe7bf99c767","repo":"moodle/moodle","slug":"nopermissiontoupdatecalendar-bdf3ef","errorCode":"nopermissiontoupdatecalendar","errorMessage":"nopermissiontoupdatecalendar","messagePattern":"nopermissiontoupdatecalendar","errorType":"error_code","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/calendar/externallib.php","lineNumber":909,"sourceCode":"\n        self::validate_context($context);\n        parse_str($params['formdata'], $data);\n\n        if (WS_SERVER) {\n            // Request via WS, ignore sesskey checks in form library.\n            $USER->ignoresesskey = true;\n        }\n\n        $eventtype = isset($data['eventtype']) ? $data['eventtype'] : null;\n        $coursekey = ($eventtype == 'group') ? 'groupcourseid' : 'courseid';\n        $courseid = (!empty($data[$coursekey])) ? $data[$coursekey] : null;\n        $editoroptions = \\core_calendar\\local\\event\\forms\\create::build_editor_options($context);\n        $formoptions = ['editoroptions' => $editoroptions, 'courseid' => $courseid];\n        $allowedeeventtypes = calendar_get_allowed_event_types($courseid);\n\n        // Event type validation.\n        if (in_array(true, $allowedeeventtypes, true) === false) {\n            throw new \\moodle_exception('nopermissiontoupdatecalendar');\n        }\n        if (empty($eventtype) || !isset($allowedeeventtypes[$eventtype]) || $allowedeeventtypes[$eventtype] == false) {\n            return ['validationerror' => true];\n        }\n\n        $formoptions['eventtypes'] = $allowedeeventtypes;\n        if ($courseid) {\n            require_once($CFG->libdir . '/grouplib.php');\n            $groupcoursedata = groups_get_all_groups($courseid);\n            if (!empty($groupcoursedata)) {\n                $formoptions['groups'] = [];\n                foreach ($groupcoursedata as $groupid => $groupdata) {\n                    $formoptions['groups'][$groupid] = $groupdata->name;\n                }\n            }\n        }\n\n        if (!empty($data['id'])) {","sourceCodeStart":891,"sourceCodeEnd":927,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/calendar/externallib.php#L891-L927","documentation":"moodle_exception('nopermissiontoupdatecalendar') is thrown by calendar_external::submit_create_update_form when the current user has no allowed calendar event types at all for the given course. calendar_get_allowed_event_types() returns an associative array of eventtype=>bool based on capabilities; if none are true the user cannot create or update any calendar event, so the external API aborts before form validation.","triggerScenarios":"Calling the core_calendar_submit_create_update_form external function as a user whose capabilities grant zero event types (no calendar:patholeusrvents/calendar:manageentries etc.) for the resolved context, e.g. a guest or a user with only view capability, with any courseid including SITEID.","commonSituations":"Developers testing the webservices/AJAX form create path with a default student or guest account; roles stripped of calendar capabilities after upgrades; tests that log in as a user lacking moodle/calendar:manageownentries before calling the external function.","solutions":["Grant the user an event-creation capability for the target context (e.g. moodle/calendar:manageownentries for user events, moodle/calendar:manageentries for course/site events) via role assignment or capability override.","Check calendar_get_allowed_event_types($courseid) before calling submit_create_update_form and show an appropriate UI error instead of invoking the external function.","If the intent is to test the no-permission path, expect this exception and catch it in the test rather than treating it as a failure.","Verify $courseid resolves to the intended course; passing a course the user cannot act in narrows allowed types to empty."],"exampleFix":"// before\n$result = core_calendar_external::submit_create_update_form($formdata);\n// after\n$allowed = calendar_get_allowed_event_types($courseid);\nif (!in_array(true, $allowed, true)) {\n    throw new require_login_exception('nopermissiontoupdatecalendar');\n}\n$result = core_calendar_external::submit_create_update_form($formdata);","handlingStrategy":"validation","validationCode":"$allowed = calendar_get_allowed_event_types($courseid);\nif (!in_array(true, $allowed, true)) { // user cannot create any event type\n    throw new moodle_exception('nopermissiontoupdatecalendar', 'calendar');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $result = core_calendar_external::submit_create_update_form($formdata);\n} catch (moodle_exception $e) {\n    if ($e->errorcode === 'nopermissiontoupdatecalendar') {\n        redirect($returnurl, get_string('nopermissions', 'error'), null, \\core\\output\\notification::NOTIFY_WARNING);\n    }\n    throw $e;\n}","preventionTips":["Check calendar_get_allowed_event_types() before rendering any event-create UI","Assign calendar capabilities to roles during site setup","Catch moodle_exception in webservice wrappers and translate to a permission message","Test create flows with least-privileged accounts"],"tags":["moodle","calendar","permissions","webservice"],"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"}