src/Security/Voter/PageVoter.php line 12
<?php
namespace App\Security\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\Voter,
Symfony\Component\Security\Core\Authentication\Token\TokenInterface,
Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use App\Entity\Page,
App\Lib\Actions,
App\Lib\Roles;
final class PageVoter extends Voter
{
const CREATE_MESSAGE = 'Page cannot be created.';
const EDIT_MESSAGE = 'Page cannot be edited.';
const DELETE_MESSAGE = 'Page cannot be deleted.';
private AuthorizationCheckerInterface $authorizationChecker;
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
protected function supports($attribute, $subject): bool
{
if (! $subject instanceof Page) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
if ($this->authorizationChecker->isGranted(Roles::ROLE_OPENFORM)) {
return true;
}
/** @var Page $page */
$page = $subject;
return match($attribute) {
Actions::CREATE => $page->getParent()?->getIsSubPageable() ?? true,
Actions::EDIT, Actions::CLONE => $page->getIsEditable(),
Actions::DELETE => $page->getIsDeletable(),
default => false,
};
}
}