{"record":{"id":"0b1230a9f1786f8e","repo":"moodle/moodle","slug":"nopermissionstodeleteentry-edit","errorCode":null,"errorMessage":"nopermissionstodeleteentry","messagePattern":"nopermissionstodeleteentry","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/blog/edit.php","lineNumber":130,"sourceCode":"}\n$returnurl->param('userid', $userid);\n\n// Blog renderer.\n$output = $PAGE->get_renderer('blog');\n\n$strblogs = get_string('blogs', 'blog');\n\nif ($action === 'delete') {\n    // Init comment JS strings.\n    \\core_comment\\manager::init();\n\n    if (empty($entry->id)) {\n        throw new \\moodle_exception('wrongentryid');\n    }\n    if (data_submitted() && $confirm && confirm_sesskey()) {\n        // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.\n        if (!blog_user_can_edit_entry($entry)) {\n            throw new \\moodle_exception('nopermissionstodeleteentry', 'blog');\n        } else {\n            $entry->delete();\n            blog_rss_delete_file($userid);\n            redirect($returnurl);\n        }\n    } else if (blog_user_can_edit_entry($entry)) {\n        $optionsyes = array('entryid' => $id,\n                            'action' => 'delete',\n                            'confirm' => 1,\n                            'sesskey' => sesskey(),\n                            'courseid' => $courseid);\n        $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);\n        $PAGE->set_title($strblogs);\n        $PAGE->set_heading($SITE->fullname);\n        echo $OUTPUT->header();\n\n        // Output edit mode title.\n        echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/blog/edit.php#L112-L148","documentation":"blog/edit.php throws moodle_exception('nopermissionstodeleteentry','blog') when a confirmed delete request (data_submitted() && $confirm && confirm_sesskey()) targets a valid entry, but blog_user_can_edit_entry($entry) returns false — the current user is neither the entry's author nor holds moodle/blog:manageentries (or manageofficialentries for associated entries). Deletion deliberately reuses the edit-rights function rather than a separate capability.","triggerScenarios":"Confirming deletion of another user's blog entry without moodle/blog:manageentries; a confirmed delete POST whose session user differs from the author (e.g. different account resumed mid-flow); deleting an 'official' associated entry without moodle/blog:manageofficialentries.","commonSituations":"Moderators lacking manageentries trying to remove spam posts; users whose roles were downgraded between viewing the delete confirmation and submitting it; shared moderation accounts not actually granted the capability.","solutions":["Grant moodle/blog:manageentries at system context to moderators who must delete others' entries","Have the entry author perform the deletion, or act as that user appropriately","For official/associated entries, also grant moodle/blog:manageofficialentries","Re-render the delete confirmation page after role changes so the user sees the permission error before submitting"],"exampleFix":"// before: delete button shown to everyone\n// after\nif (blog_user_can_edit_entry($entry)) {\n    echo $OUTPUT->single_button($deleteurl, get_string('delete'));\n}","handlingStrategy":"validation","validationCode":"if (blog_user_can_edit_entry($entry) && confirm_sesskey()) {\n    // safe to show/submit delete confirmation\n}","typeGuard":null,"tryCatchPattern":"try {\n    $entry->delete();\n} catch (moodle_exception $e) {\n    if ($e->errorcode === 'nopermissionstodeleteentry') {\n        // need moodle/blog:manageentries or authorship\n    }\n}","preventionTips":["Grant moderators moodle/blog:manageentries (and manageofficialentries for associated blogs)","Show the delete button only when blog_user_can_edit_entry passes","Re-check permissions between showing the confirmation and processing it","Keep session identity stable across the confirm flow"],"tags":["moodle","blog","delete","permissions"],"backgroundTag":"insufficient-permissions","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"}