src/Common/Security/Voter/PlaceholderVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace PaperKite\Common\Security\Voter;
  3. use PaperKite\Common\Entity\Support\Placeholder;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class PlaceholderVoter extends Voter
  9. {
  10.     public const ADD 'ROLE_ADMIN_PLACEHOLDER_ADD';
  11.     public const EDIT 'ROLE_ADMIN_PLACEHOLDER_EDIT';
  12.     public const DELETE 'ROLE_ADMIN_PLACEHOLDER_DELETE';
  13.     public function __construct(
  14.         private Security $security,
  15.     ) {
  16.     }
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         // replace with your own logic
  20.         // https://symfony.com/doc/current/security/voters.html
  21.         return in_array($attribute, [self::EDITself::DELETE])
  22.             && $subject instanceof Placeholder;
  23.     }
  24.     /**
  25.      * @param Placeholder $subject
  26.      */
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  28.     {
  29.         $user $token->getUser();
  30.         // if the user is anonymous, do not grant access
  31.         if (!$user instanceof UserInterface) {
  32.             return false;
  33.         }
  34.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  35.             return true;
  36.         }
  37.         if (true === str_starts_with($subject->getDescriptiveId(), 'member.')) {
  38.             return false;
  39.         }
  40.         if (true === str_starts_with($subject->getDescriptiveId(), 'dossier.')) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45. }