src/School/Application/Security/TutoringScopeVoter.php line 30

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the educat package.
  4.  *
  5.  * (c) Solvee
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\School\Application\Security;
  12. use App\Common\Model\Core\UserInterface;
  13. use App\Common\Model\School\TutoringScopeInterface;
  14. use App\Common\Security\ObjectOwnerVoterInterface;
  15. use App\Common\Security\ObjectOwnerVoterTrait;
  16. use App\Common\Security\VoterTrait;
  17. use LogicException;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  20. use function in_array;
  21. /**
  22.  * Class TutoringScopeVoter
  23.  *
  24.  * @author Kamil KozaczyƄski <kozaczynski.kamil@gmail.com>
  25.  */
  26. class TutoringScopeVoter extends Voter implements ObjectOwnerVoterInterface
  27. {
  28.     use VoterTraitObjectOwnerVoterTrait;
  29.     /**
  30.      * @inheritDoc
  31.      */
  32.     protected function supports(string $attribute$subject): bool
  33.     {
  34.         if (!in_array(
  35.             $attribute,
  36.             [
  37.                 self::OBJECT_OWNER,
  38.             ],
  39.             true
  40.         )) {
  41.             return false;
  42.         }
  43.         return $subject instanceof TutoringScopeInterface;
  44.     }
  45.     /**
  46.      * @param string                 $attribute
  47.      * @param TutoringScopeInterface $subject
  48.      * @param TokenInterface         $token
  49.      *
  50.      * @return bool
  51.      */
  52.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  53.     {
  54.         $user $token->getUser();
  55.         if (!$user instanceof UserInterface) {
  56.             return false;
  57.         }
  58.         return match ($attribute) {
  59.             self::OBJECT_OWNER => $this->isObjectOwner($subject$user->getAccount()),
  60.             default => throw new LogicException('This code should not be reached.')
  61.         };
  62.     }
  63. }