Hi everyone,
I want to implement file protection depending on authenticated users. I know about the Protecting files recipe which is based on files located at a special route (downloads). For my project I need the protection for files at any location, but flagged as protected via file blueprint. Is there a way to serve flagged images or files for authenticated users? And 403 for others?
Here is my code:
site/blueprints/files/default.yml:
title: File
fields:
alt:
label: Alt Text
type: text
protected:
label: Protected file
type: toggle
content/1_home/my-image.png.txt:
Alt:
----
Protected: true
----
Uuid: qzistvufo5dkmr9e
----
Template: blocks/image
site/plugins/protected-files/index.php:
use Kirby\Cms\App as Kirby;
Kirby::plugin('devmount/protected-files', [
'components' => [
'file::url' => function ($kirby, $file) {
dump('Debug: ' . ($file->protected()->toBool() ? '1' : '0')); // <- This is always 0
if ($file->protected()->toBool() === true) {
return '';
}
return $file->mediaUrl();
},
'file::version' => function ($kirby, $file, array $options = []) {
static $original;
// if the file is protected, return the original file
if ($file->protected()->toBool() === true) {
return $file;
}
// if static $original is null, get the original component
if ($original === null) {
$original = $kirby->nativeComponent('file::version');
}
// and return it with the given options
return $original($kirby, $file, $options);
}
],
]);
I seem to cannot access the blueprint fields in the callback functions like this. The dump statement above is 0 for every single image, no matter if protected was toggled on or off.
I’m grateful for any hints or tipps on this.


