src/Schedule/Application/Security/ScheduleInstanceVoter.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\Schedule\Application\Security;
  12. use App\Common\Model\Core\UserInterface;
  13. use App\Common\Model\Schedule\ScheduleInstanceInterface;
  14. use App\Common\Security\ObjectOwnerVoterTrait;
  15. use App\Common\Security\VoterTrait;
  16. use LogicException;
  17. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  18. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  19. use Symfony\Component\Security\Core\Security;
  20. use function in_array;
  21. /**
  22.  * Class ScheduleInstanceVoter
  23.  *
  24.  * @author  MichaƂ Rybnik <michal.rybnik@solvee.pl>
  25.  */
  26. class ScheduleInstanceVoter extends Voter
  27. {
  28.     use VoterTrait;
  29.     use ObjectOwnerVoterTrait;
  30.     public const GET 'get_schedule_instance';
  31.     /**
  32.      * @param Security $security
  33.      */
  34.     public function __construct(Security $security)
  35.     {
  36.         $this->security $security;
  37.     }
  38.     /**
  39.      * @inheritDoc
  40.      */
  41.     protected function supports(string $attribute$subject): bool
  42.     {
  43.         if (!in_array(
  44.             $attribute,
  45.             [
  46.                 self::GET,
  47.             ],
  48.             true
  49.         )) {
  50.             return false;
  51.         }
  52.         return $subject instanceof ScheduleInstanceInterface;
  53.     }
  54.     /**
  55.      * @param string                    $attribute
  56.      * @param ScheduleInstanceInterface $subject
  57.      * @param TokenInterface            $token
  58.      *
  59.      * @return bool
  60.      */
  61.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  62.     {
  63.         $user $token->getUser();
  64.         if (!$user instanceof UserInterface) {
  65.             return false;
  66.         }
  67.         switch ($attribute) {
  68.             case self::GET:
  69.                 return $this->canGet($subject$user);
  70.         }
  71.         throw new LogicException('This code should not be reached.');
  72.     }
  73.     /**
  74.      * @param ScheduleInstanceInterface $subject
  75.      * @param UserInterface             $user
  76.      *
  77.      * @return bool
  78.      */
  79.     private function canGet(ScheduleInstanceInterface $subjectUserInterface $user): bool
  80.     {
  81.         return $this->isAdminOrOwner($subject$user);
  82.     }
  83.     /**
  84.      * @param ScheduleInstanceInterface $scheduleInstance
  85.      * @param UserInterface             $user
  86.      *
  87.      * @return bool
  88.      */
  89.     private function isAdminOrOwner(ScheduleInstanceInterface $scheduleInstanceUserInterface $user): bool
  90.     {
  91.         return $this->isAdmin() || $this->isObjectOwner($scheduleInstance$user->getAccount());
  92.     }
  93. }