src/Security/Voter/CourseDataVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\CourseData;
  4. use App\User\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class CourseDataVoter extends Voter
  8. {
  9.     public const VIEW 'view';
  10.     public const EDIT 'edit';
  11.     public const DELETE 'delete';
  12.     protected function supports(string $attribute$subject): bool
  13.     {
  14.         return in_array($attribute, [self::VIEWself::EDITself::DELETE], true)
  15.             && $subject instanceof CourseData;
  16.     }
  17.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  18.     {
  19.         $user $token->getUser();
  20.         if (!$user instanceof User) {
  21.             return false;
  22.         }
  23.         if (in_array('ROLE_SUPER_USER'$user->getRoles(), true)) {
  24.             return true;
  25.         }
  26.         /** @var CourseData $courseData */
  27.         $courseData $subject;
  28.         $userClient $user->getClient();
  29.         if (!$userClient || !$courseData->getClient()) {
  30.             return false;
  31.         }
  32.         if ($userClient->getId() !== $courseData->getClient()->getId()) {
  33.             return false;
  34.         }
  35.         return in_array('ROLE_MANAGER'$user->getRoles(), true);
  36.     }
  37. }