src/Security/Voter/DocumentVoter.php line 13

  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Document;
  4. use App\Enum\RoleType;
  5. use Symfony\Bundle\SecurityBundle\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use function PHPUnit\Framework\isInstanceOf;
  10. class DocumentVoter extends Voter
  11. {
  12.     public const EDIT 'DOCUMENT_EDIT';
  13.     public const SHOW 'DOCUMENT_SHOW';
  14.     public const DELETE 'DOCUMENT_DELETE';
  15.     private $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports(string $attributemixed $subject): bool
  21.     {
  22. //        dd($subject);
  23.         return in_array($attribute, [self::EDITself::SHOWself::DELETE]) &&
  24.             isInstanceOf(Document::class);
  25.     }
  26.     /**
  27.      * @param string $attribute
  28.      * @param array $subject
  29.      * @param TokenInterface $token
  30.      * @return bool
  31.      */
  32.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  33.     {
  34.         $user $token->getUser();
  35.         // if the user is anonymous, do not grant access
  36.         if (!$user instanceof UserInterface) {
  37.             return false;
  38.         }
  39.         /**
  40.          * @var $isRelated bool controls if the document belongs to the student or parent
  41.          * @var $allow bool controls if the user is allowed to access the document
  42.          * both variables will help determine who can access the document
  43.          */
  44.         $isRelated false;
  45.         $allow false;
  46.         // Who it belongs to:
  47.         // Does it belong to a student?
  48.         if ($subject['document']->getStudent()) {
  49.             // does it belong to the student itself?
  50.             if ($subject['document']->getStudent()->getId() === $subject['id']) {
  51.                 $isRelated true;
  52.                 // Who can access it
  53.                 // Currently only the guardian who made the document can access it
  54.                 // When Parent and Student are associated to User, they should be able to access their related documents
  55.                 // Is the user the guardian who made this document?
  56.                 if ($user === $subject['document']->getStudent()->getGuardian()->getUser()) {
  57.                     $allow true;
  58.                 }
  59.             }
  60.         } // Does it belong to a parent?
  61.         elseif ($subject['document']->getParentalDocument()) {
  62.             // does it belong to the parent itself?
  63.             if ($subject['document']->getParentalDocument()->getParentData()->getId() === $subject['id']) {
  64.                 $isRelated true;
  65.                 // Is the user the guardian who made this document?
  66.                 if ($user === $subject['document']->getParentalDocument()->getParentData()->getGuardian()->getUser()) {
  67.                     $allow true;
  68.                 }
  69.             }
  70.         } else
  71.             return false;
  72.         $accessIsGranted = match ($attribute) {
  73.             'DOCUMENT_SHOW' =>
  74.                 $allow
  75.                 ||
  76.                 $isRelated && (
  77.                     $this->security->isGranted(RoleType::ROLE_ADMIN) ||
  78.                     $this->security->isGranted(RoleType::ROLE_EMPLOYEE) ||
  79.                     $this->security->isGranted(RoleType::ROLE_FINANCIAL_DIRECTOR) ||
  80.                     $this->security->isGranted(RoleType::ROLE_PEDAGOGICAL_DIRECTOR)
  81.                 ),
  82.             'DOCUMENT_EDIT' =>
  83.                 $allow
  84.                 ||
  85.                 $isRelated && $this->security->isGranted(RoleType::ROLE_ADMIN),
  86.             'DOCUMENT_DELETE' => $isRelated && $this->security->isGranted(RoleType::ROLE_ADMIN),
  87.         };
  88.         return $accessIsGranted;
  89.     }
  90. }