But this doesn’t seem to be doing anything.
Any file of the ‘intern’ subdirectory should be inaccessible unless certain user roles are logged in.
How can I achieve this?
I’ve copied the cookbook snippet and replaced the template name with the file template and page slug that I want to make inaccessible.
I’m trying it with a file that is a part of the /intern subdirectory and hope for it to redirect once I reload on the browser where I am not logged in.
However it doesn’t seem to be working like this
<?php
use Kirby\Cms\Response;
Kirby::plugin('cookbook/files-firewall', [
'routes' => [
[
'pattern' => 'intern/(:any)',
'action' => function ($filename) {
if ($user = kirby()->user()) {
if (
in_array($user->role(), ['admin', 'employee']) &&
($page = page('intern')) &&
$file = $page->files()->findBy('filename', $filename)
) {
return $file->download();
}
}
return page('login');
},
],
],
'components' => [
'file::url' => function ($kirby, $file) {
if ($file->template() === 'intern') {
return $kirby->url() . '/' . $file->parent()->id() . '/' . $file->filename();
}
return $file->mediaUrl();
},
'file::version' => function ($kirby, $file, array $options = []) {
static $original;
// if the file is protected, return the original file
if ($file->template() === 'intern') {
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);
}
],
]);
sorry, that was the wrong word.
But the user should end up at a login page if they aren’t logged in.
I think I figured it out… kind of.
It only works with files on the first level. The issue now is, that the /intern subdirectory has multiple subdirectories itself. The files within those are still accessible
But that is probably just a routing issue on my part.
<?php
use Kirby\Cms\Response;
Kirby::plugin('cookbook/files-firewall', [
'routes' => [
[
'pattern' => 'intern/(:any)',
'action' => function ($filename) {
if ($user = kirby()->user()) {
if (
in_array($user->role(), ['admin', 'employee', 'employee-intitution']) &&
in_array($this->template(), ['intern', 'internpage', 'internnews', 'internnewspage']) &&
$file = $this->files()->findBy('filename', $filename)
) {
return $file->download();
}
return $this->next();
} else{
return go('login');
}
},
],
],
'components' => [
'file::url' => function ($kirby, $file) {
if ($file->template() === 'intern') {
return $kirby->url() . '/' . $file->parent()->id() . '/' . $file->filename();
}
return $file->mediaUrl();
},
'file::version' => function ($kirby, $file, array $options = []) {
static $original;
// if the file is protected, return the original file
if ($file->template() === 'intern') {
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);
}
],
]);
Is there no way to directly block access to the media subdirectory?
If so I guess I’ll just have to close it off for a while via htaccess until Google removes these links ://
Is the file still created when you call the media url and remove the files from the media folder? I recall we had this issue in the past, but can’t find it at the moment.
Yes, even after deleting the entire media folder.
When I try to open the same URL, which has been indexed by Google, it creates the media subdirectory and file once again,