src/Voters/CompanyObjectsVoter.php line 15

  1. <?php
  2. /*
  3.  *  @author Guerby Duval <info@tranzaksyon.com>
  4.  *  @link https://tranzaksyon.com
  5.  *  @copyright You are not allowed to remove this author "Guerby Duval <info@tranzaksyon.com>", the link "https://tranzaksyon.com" neither this copyright.
  6.  */
  7. namespace App\Voters;
  8. use App\Interfaces\OwnerCompanyInterface;
  9. use App\Repository\UserRepository;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. class CompanyObjectsVoter extends Voter
  13. {
  14.     const UPDATE 'Update';
  15.     const VIEW 'View';
  16.     public function __construct(private UserRepository $userRepository)
  17.     {
  18.     }
  19.     protected function supports(string $attributemixed $subject): bool
  20.     {
  21.         return in_array($attribute, [self::UPDATEself::VIEW]) && $subject instanceof OwnerCompanyInterface;
  22.     }
  23.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  24.     {
  25.         $company $this->userRepository->fetchSriInfo($token->getUser());
  26.         $hasRole false;
  27.         if ($token->getUser() === null) {
  28.             return false;
  29.         }
  30.         foreach ($token->getUser()->getRoles() as $role) {
  31.             if ($role === 'ROLE_SELLER' || $role === 'ROLE_INVOICER' || $role === 'ROLE_ADMIN' || $role === 'ROLE_CHAT') {
  32.                 $hasRole true;
  33.                 break;
  34.             }
  35.         }
  36.         return $hasRole && $subject->getSriInfo() !== null && $subject->getSriInfo()->getId() === $company->getId();
  37.     }
  38. }