<?php
namespace PaperKite\Common\Security\Voter;
use PaperKite\Common\Entity\Support\Placeholder;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class PlaceholderVoter extends Voter
{
public const ADD = 'ROLE_ADMIN_PLACEHOLDER_ADD';
public const EDIT = 'ROLE_ADMIN_PLACEHOLDER_EDIT';
public const DELETE = 'ROLE_ADMIN_PLACEHOLDER_DELETE';
public function __construct(
private Security $security,
) {
}
protected function supports(string $attribute, $subject): bool
{
// replace with your own logic
// https://symfony.com/doc/current/security/voters.html
return in_array($attribute, [self::EDIT, self::DELETE])
&& $subject instanceof Placeholder;
}
/**
* @param Placeholder $subject
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
return true;
}
if (true === str_starts_with($subject->getDescriptiveId(), 'member.')) {
return false;
}
if (true === str_starts_with($subject->getDescriptiveId(), 'dossier.')) {
return false;
}
return true;
}
}