Hi, i couldn’t find any solutions on this topic:
I have 2 roles for my project admin and editor. The editor should only see and have access to a subpage that the admin has setup via custom field in the blueprints/users/editor.yml file. I already tried to debug or find the error with Ai but my setup has no impact with limiting the editor. I found this post: Extending permissions | Kirby CMS
My files:
editor.yml (site/blueprints/users/editor.yml)
title: Editor
#Fields
fields:
assignedPage:
label: Assigned page
type: pages
max: 1
readonly: true
My user.php (site/models/user.php)
<?php
class UserPage extends Kirby\Cms\User
{
public function assignedPage(): ?Kirby\Cms\Page
{
return $this->content()->get('assignedPage')->toPage();
}
}
My page.php (site/models/page.php)
<?php
class Page extends Kirby\Cms\Page
{
public function isReadable(): bool
{
$user = kirby()->user();
if (!$user) {
return false;
}
if ($user->role()->name() === 'admin') {
return true;
}
if ($user->role()->name() === 'editor') {
$assignedPage = $user->assignedPage();
return $assignedPage && $assignedPage->is($this);
}
return false;
}
public function isWritable(): bool
{
$user = kirby()->user();
if (!$user) {
return false;
}
if ($user->role()->name() === 'admin') {
return true;
}
if ($user->role()->name() === 'editor') {
$assignedPage = $user->assignedPage();
return $assignedPage && $assignedPage->is($this);
}
return false;
}
}
My config.php (site/config/config.php)
<?php
return [
'debug' => false,
'hooks' => [
'user.update:before' => function ($user, $values) {
if ($user->role()->name() === 'editor') {
if (isset($values['assignedPage'])) {
throw new \Kirby\Exception\PermissionException('You do not have permission to change the assigned page');
}
}
},
],
'permissions' => [
'user.changeRole' => function ($user) {
return kirby()->user()->isAdmin();
},
],
'autoload' => [
'page' => 'site/models/page.php',
'user' => 'site/models/user.php'
]
];
It’s also important that i tried to implement that the user editor can’t change his assigned page on the …/panel/account site.