moodle/moodle · error · moodle_exception

nopermissionstodeleteentry

Error message

nopermissionstodeleteentry

What it means

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.

Solutions

  1. Grant moodle/blog:manageentries at system context to moderators who must delete others' entries
  2. Have the entry author perform the deletion, or act as that user appropriately
  3. For official/associated entries, also grant moodle/blog:manageofficialentries
  4. Re-render the delete confirmation page after role changes so the user sees the permission error before submitting

Example fix

// before: delete button shown to everyone
// after
if (blog_user_can_edit_entry($entry)) {
    echo $OUTPUT->single_button($deleteurl, get_string('delete'));
}
Defensive patterns

Strategy: validation

Validate before calling

if (blog_user_can_edit_entry($entry) && confirm_sesskey()) {
    // safe to show/submit delete confirmation
}

Try / catch

try {
    $entry->delete();
} catch (moodle_exception $e) {
    if ($e->errorcode === 'nopermissionstodeleteentry') {
        // need moodle/blog:manageentries or authorship
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "You do not have permission" / 403 Forbidden errors: authenticated but not allowed — causes and fixes across open-source libraries — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at public/blog/edit.php:130

}
$returnurl->param('userid', $userid);

// Blog renderer.
$output = $PAGE->get_renderer('blog');

$strblogs = get_string('blogs', 'blog');

if ($action === 'delete') {
    // Init comment JS strings.
    \core_comment\manager::init();

    if (empty($entry->id)) {
        throw new \moodle_exception('wrongentryid');
    }
    if (data_submitted() && $confirm && confirm_sesskey()) {
        // Make sure the current user is the author of the blog entry, or has some deleteanyentry capability.
        if (!blog_user_can_edit_entry($entry)) {
            throw new \moodle_exception('nopermissionstodeleteentry', 'blog');
        } else {
            $entry->delete();
            blog_rss_delete_file($userid);
            redirect($returnurl);
        }
    } else if (blog_user_can_edit_entry($entry)) {
        $optionsyes = array('entryid' => $id,
                            'action' => 'delete',
                            'confirm' => 1,
                            'sesskey' => sesskey(),
                            'courseid' => $courseid);
        $optionsno = array('userid' => $entry->userid, 'courseid' => $courseid);
        $PAGE->set_title($strblogs);
        $PAGE->set_heading($SITE->fullname);
        echo $OUTPUT->header();

        // Output edit mode title.
        echo $OUTPUT->heading($strblogs . ': ' . get_string('deleteentry', 'blog'), 2);

View on GitHub (pinned to 48ea8c3322)