src/Security/Authorization/Voter/ViewVehicleVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Authorization\Voter;
  3. use App\Entity\BuyersGroup;
  4. use App\Entity\User;
  5. use App\Entity\Vehicle;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class ViewVehicleVoter extends Voter
  10. {
  11.     public const VIEW 'view';
  12.     public function __construct(private readonly AccessDecisionManagerInterface $decisionManager)
  13.     {
  14.     }
  15.     protected function supports($attribute$vehicle)
  16.     {
  17.         if (self::VIEW !== $attribute) {
  18.             return false;
  19.         }
  20.         if (!$vehicle instanceof Vehicle) {
  21.             return false;
  22.         }
  23.         return true;
  24.     }
  25.     protected function voteOnAttribute($attribute$vehicleTokenInterface $token)
  26.     {
  27.         $sale $vehicle->getSale();
  28.         $user $token->getUser();
  29.         if (in_array(BuyersGroup::PUBLIC_GROUP$sale->getBuyersGroups()->toArray())) {
  30.             return true;
  31.         }
  32.         if ($this->decisionManager->decide($token, ['ROLE_ACCESS'])) {
  33.             return true;
  34.         }
  35.         if ($user instanceof User) {
  36.             return [] !== array_intersect($user->getBuyersGroups()->toArray(), $sale->getBuyersGroups()->toArray());
  37.         }
  38.         if ('anon.' === $user) {
  39.             return true;
  40.         }
  41.         return false;
  42.     }
  43. }