{"record":{"id":"788ea03472979def","repo":"moodle/moodle","slug":"cannoteditentryorblog-update-entry","errorCode":null,"errorMessage":"cannoteditentryorblog","messagePattern":"cannoteditentryorblog","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/blog/classes/external/update_entry.php","lineNumber":100,"sourceCode":"            array $options = []): array {\n\n        global $DB, $CFG;\n        require_once($CFG->dirroot . '/blog/lib.php');\n        require_once($CFG->dirroot . '/blog/locallib.php');\n\n        $params = self::validate_parameters(self::execute_parameters(), compact('entryid', 'subject', 'summary',\n            'summaryformat', 'options'));\n\n        if (empty($CFG->enableblogs)) {\n            throw new moodle_exception('blogdisable', 'blog');\n        }\n\n        if (!$entry = new \\blog_entry($params['entryid'])) {\n            throw new moodle_exception('wrongentryid', 'blog');\n        }\n\n        if (!blog_user_can_edit_entry($entry)) {\n            throw new \\moodle_exception('cannoteditentryorblog', 'blog');\n        }\n\n        // Prepare the entry object.\n        $entrydata = new \\stdClass();\n        $entrydata->id = $entry->id;\n        $entrydata->subject = $params['subject'];\n        $entrydata->summary_editor = [\n            'text' => $params['summary'],\n            'format' => $params['summaryformat'],\n        ];\n        $entrydata->publishstate = $entry->publishstate;\n        $entrydata->courseassoc = $entry->courseassoc;\n        $entrydata->modassoc = $entry->modassoc;\n        $entrydata->tags = \\core_tag_tag::get_item_tags_array('core', 'post', $entry->id);\n\n        // Options.\n        foreach ($params['options'] as $option) {\n            $name = trim($option['name']);","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/blog/classes/external/update_entry.php#L82-L118","documentation":"The blog external webservice function (update_entry) throws moodle_exception('cannoteditentryorblog','blog') when blog_user_can_edit_entry($entry) returns false, i.e. the current web-service user is not allowed to modify the given blog entry. The library treats edit rights as: the entry author, a user with moodle/blog:manageentries, or a user with moodle/blog:manageofficialentries for associated entries. It exists to enforce per-entry ownership rules inside the external API after the entryid has been resolved.","triggerScenarios":"Calling the blog_update_entry external function where $params['entryid'] resolves to a valid entry but the authenticated user is neither its author nor holds moodle/blog:manageentries (or manageofficialentries where applicable). Note the source also guards `if (!$entry = new \\blog_entry(...))` which only triggers on wrongentryid; the cannotedit exception comes strictly from the permission check.","commonSituations":"A web service token belonging to a user who did not write the entry; an admin creates a token for a service account that lacks moodle/blog:manageentries; trying to edit another user's entry via the external API assuming admin-level rights; capability changed by role overrides after the token was issued.","solutions":["Log in as (or issue a token for) the user who authored the entry","Grant the calling user's role the capability moodle/blog:manageentries at system context","Verify the entryid belongs to the expected user via blog_get_entries before calling update","Check role/capability overrides that may have removed blog capabilities for the service account"],"exampleFix":"// before\n$entrydata->id = $entry->id; // throws cannoteditentryorblog for non-authors\n// after\nif (!blog_user_can_edit_entry($entry)) {\n    // ensure the WS user has manage capability, or pick an entry they own\n    throw new moodle_exception('cannoteditentryorblog', 'blog');\n}","handlingStrategy":"validation","validationCode":"if (empty($CFG->enableblogs)) { throw new moodle_exception('blogdisable','blog'); }\n$entry = new blog_entry($entryid);\nif (empty($entry->id)) { /* wrongentryid: abort */ }\n$canEdit = blog_user_can_edit_entry($entry);","typeGuard":"function canEditBlogEntry(blog_entry $entry): bool {\n    return !empty($entry->id) && blog_user_can_edit_entry($entry);\n}","tryCatchPattern":"try {\n    blog_update_entry($entrydata);\n} catch (moodle_exception $e) {\n    if ($e->errorcode === 'cannoteditentryorblog') {\n        // fall back: request manageentries or edit as the author\n    }\n}","preventionTips":["Check blog_user_can_edit_entry before every external update call","Issue web service tokens only for users who own the entries they modify","Grant moodle/blog:manageentries to service/manager accounts that must edit any entry","Keep entryid-to-owner mappings verified in your integration"],"tags":["moodle","blog","webservice","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"}