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

Open in your IDE?
  1. <?php
  2. namespace App\Security\Authorization\Voter;
  3. use App\Entity\BuyersGroup;
  4. use App\Entity\Sale;
  5. use App\Entity\User;
  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 ViewSaleVoter extends Voter
  10. {
  11.     public const VIEW 'view';
  12.     public function __construct(private readonly AccessDecisionManagerInterface $decisionManager)
  13.     {
  14.     }
  15.     protected function supports($attribute$sale)
  16.     {
  17.         if (self::VIEW !== $attribute) {
  18.             return false;
  19.         }
  20.         if (!$sale instanceof Sale) {
  21.             return false;
  22.         }
  23.         return true;
  24.     }
  25.     protected function voteOnAttribute($attribute$saleTokenInterface $token)
  26.     {
  27.         $user $token->getUser();
  28.         if (in_array(BuyersGroup::PUBLIC_GROUP$sale->getBuyersGroups()->toArray())) {
  29.             return true;
  30.         }
  31.         if ($this->decisionManager->decide($token, ['ROLE_ACCESS'])) {
  32.             return true;
  33.         }
  34.         if ($user instanceof User) {
  35.             return [] !== array_intersect($user->getBuyersGroups()->toArray(), $sale->getBuyersGroups()->toArray());
  36.         }
  37.         return false;
  38.     }
  39. }