{"record":{"id":"fd0249dc4e58f2c0","repo":"moodle/moodle","slug":"nopermissiontodelentry","errorCode":"nopermissiontodelentry","errorMessage":"nopermissiontodelentry","messagePattern":"nopermissiontodelentry","errorType":"error_code","errorClass":"core_comment\\comment_exception","httpStatus":null,"severity":"warning","filePath":"public/comment/classes/external.php","lineNumber":346,"sourceCode":"            // Validate the context.\n            list($context, $course, $cm) = get_context_info_array($commentrecord->contextid);\n            if ($context->id == SYSCONTEXTID) {\n                $course = $SITE;\n            }\n            self::validate_context($context);\n\n            // Make sure the user is allowed to delete the comment.\n            $args = new stdClass;\n            $args->context   = $context;\n            $args->course    = $course;\n            $args->cm        = $cm;\n            $args->component = $commentrecord->component;\n            $args->itemid    = $commentrecord->itemid;\n            $args->area      = $commentrecord->commentarea;\n            $manager = new \\core_comment\\manager($args);\n\n            if (!$manager->can_delete($commentrecord)) {\n                throw new \\core_comment\\comment_exception('nopermissiontodelentry');\n            }\n\n            // User is allowed to delete it, so store the comment object, for use below in final deletion.\n            $comments[$commentrecord->id] = $manager;\n        }\n\n        // All comments can be deleted by the user. Make it so.\n        foreach ($comments as $commentid => $comment) {\n            $comment->delete($commentid);\n        }\n\n        return [];\n    }\n\n    /**\n     * Returns description of method result value for the delete_comments() method.\n     *\n     * @return \\core_external\\external_description","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/comment/classes/external.php#L328-L364","documentation":"The comments external service delete_comments throws comment_exception 'nopermissiontodelentry' when core_comment\\manager::can_delete() reports the current user is not allowed to delete the target comment. This is an authorization gate before any deletion happens, so nothing is removed when it fires. It protects comments from being deleted by users who neither own them nor hold moderation rights in the context.","triggerScenarios":"Calling the external function delete_comments (or the WS equivalent) with a commentid whose comment belongs to another user while the caller lacks mod/comment:delete capability in that context; also hit by tests test_delete_student_external, test_delete_admin_external, test_delete_comments_own_user, test_delete_comment_other_student and test_delete_comments_as_teacher for various permission setups.","commonSituations":"A student tries to delete another student's comment via the web service; a teacher role lacks the delete capability after capability overrides were changed; a client passes a comment id from a different course/module context where the user is a mere participant.","solutions":["Grant the calling user the required capability (moodle/comment:delete or module equivalent) in the comment's context, e.g. via role capability overrides.","Verify the user actually owns the comment if ownership is the intended deletion path.","Check in client code that the comment id and context belong to the acting user's scope before calling delete_comments.","If the exception is expected, catch core_comment\\comment_exception and surface a friendly 'not allowed' message.","Re-test with test_delete_comment_other_student-style fixtures to confirm role assignment and capability settings."],"exampleFix":"// before\ndelete_comments([['commentid' => $otherUserCommentId]]); // throws nopermissiontodelentry\n// after\nif (comment_delete_allowed($otherUserCommentId)) { // pre-check ownership/capability\n    delete_comments([['commentid' => $otherUserCommentId]]);\n} else {\n    throw new moodle_exception('nopermissiontodelentry', 'moodle');\n}","handlingStrategy":"try-catch","validationCode":"// Moodle: check capability before calling the external function\n$context = context::instance_by_id($contextid);\nif (!has_capability('moodle/comment:delete', $context) && !comment_is_owned_by_user($commentid, $USER->id)) {\n    throw new moodle_exception('nopermissiontodelentry', 'moodle');\n}","typeGuard":null,"tryCatchPattern":"try {\n    delete_comments([['commentid' => $commentid]]);\n} catch (\\core_comment\\comment_exception $e) {\n    if ($e->errorcode === 'nopermissiontodelentry') {\n        // surface a friendly denial message\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Confirm role capabilities (moodle/comment:delete) in the exact comment context before calling.","Only delete comments owned by the current user unless acting as a moderator.","Pass the correct commentid and context, not ids from another course.","Write tests mirroring the shipped permission matrix (teacher/student/other-user cases)."],"tags":["moodle","permissions","comments","authorization"],"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"}