I’m hitting permissions problems in a custom API endpoint that attempts to duplicate and then update a page.
TL;DR: It seems that using $kirby->impersonate('kirby');
is not actually elevating the permissions.
Here’s the code:
<?php
Kirby::plugin('my/customPreview', [
'api' => [
'routes' => function ($kirby) {
return [
[
'pattern' => 'save-custom-draft',
'method' => 'POST',
'action' => function () use ($kirby) {
// Creates a preview version of the file with the in-memory data (i.e. the data that's yet to be saved) for use
// in our Lambda function that builds a Netlify preview.
$kirby->impersonate('kirby');
// Create the name, using our naming structure of `internal-preview-[slug]`
$tempName = 'internal-preview-' . $this->requestBody('slug');
// Duplicate the published file (this creates a Kirby draft by default)
$tempCopy = $kirby->page($tempName) ? $this->page($tempName) : $kirby->page($this->requestBody('slug'))->duplicate($tempName);
// Update the duplicated file with our in-memory data
$tempCopy->update($this->requestBody('data'), $this->language(), false);
return new Response(null, null, 204);
}
]
];
}
]
]);
I suspect this may be a bug but I’m not sure. That returns a 403. The kirby->impersonate('kirby');
line doesn’t seem to actually elevate the permissions. Note that I’ve also tried using an existing admin’s email and an existing admin’s id in place of kirby
but am seeing the same issue.
For context: How to save a custom draft from the panel (using the API) - #10