src/App/Api/Security/Voter/UserBelongToCompanyVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Api\Security\Voter;
  4. use App\Api\Entity\User;
  5. use App\Api\Entity\UserAbstract;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class UserBelongToCompanyVoter extends Voter
  10. {
  11.     const VIEW 'view';
  12.     protected function supports($attribute$subject)
  13.     {
  14.         if (!in_array($attribute, [self::VIEW])) {
  15.             return false;
  16.         }
  17.         if (!$subject instanceof UserAbstract) {
  18.             return false;
  19.         }
  20.         return true;
  21.     }
  22.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  23.     {
  24.         $authenticatedUser $token->getUser();
  25.         if (!$authenticatedUser instanceof UserAbstract) {
  26.             return false;
  27.         }
  28.         /** @var User $userFromRequest */
  29.         $userFromRequest $subject;
  30.         switch ($attribute) {
  31.             case self::VIEW:
  32.                 return $this->canView($userFromRequest$authenticatedUser);
  33.         }
  34.     }
  35.     private function canView(UserInterface $userFromRequestUserInterface $authenticatedUser): bool
  36.     {
  37.         return $userFromRequest->getTdc1Company() === $authenticatedUser;
  38.     }
  39. }