src/Security/Voter/CourseSubscriptionVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\CourseSubscription;
  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 CourseSubscriptionVoter 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 CourseSubscription;
  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 CourseSubscription $subscription */
  27.         $subscription $subject;
  28.         $userClient $user->getClient();
  29.         if (!$userClient) {
  30.             return false;
  31.         }
  32.         $subscriptionClient $subscription->getClient();
  33.         if (!$subscriptionClient) {
  34.             return false;
  35.         }
  36.         if ($userClient->getId() !== $subscriptionClient->getId()) {
  37.             return false;
  38.         }
  39.         return in_array('ROLE_MANAGER'$user->getRoles(), true)
  40.             || in_array('ROLE_ADMIN'$user->getRoles(), true);
  41.     }
  42. }