src/Security/Contract/ContractAccessControlVoter.php line 12

  1. <?php
  2. namespace App\Security\Contract;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. /**
  6.  * Class ContractAccessControlVoter
  7.  * @package App\Security\Contract
  8.  */
  9. class ContractAccessControlVoter extends Voter
  10. {
  11.     /**
  12.      * @param string $attribute
  13.      * @param mixed $subject
  14.      * @return bool
  15.      */
  16.     public function supports(string $attributemixed $subject) :bool
  17.     {
  18.         if ($attribute === 'contract_access_control_contract') {
  19.             return true;
  20.         }
  21.         return false;
  22.     }
  23.     /**
  24.      * @param string $attribute
  25.      * @param mixed $subject
  26.      * @param TokenInterface $token
  27.      * @return bool
  28.      */
  29.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) :bool
  30.     {
  31.         /**
  32.          * @var int|bool $institutionId
  33.          * Vraća id od trenutnog operatera
  34.          */
  35.         $requester $subject->getReference();
  36.         /**
  37.          * Vraća id o trenutnog korisnika
  38.          */
  39.         $requesterCity $subject->getCity()->getId();
  40.         /**
  41.          * Prijavljeni korisnik
  42.          */
  43.         $user $token->getUser();
  44.         // Provjera jel je korisnik uopče logiran
  45.         if ((int)$requester && (int)$requesterCity && is_object($user)) {
  46.             // Provjera jel je korisnikov trenutni operater isti kao operater od tog ugovora
  47.             if ((int)$requesterCity === $user->getCity()->getId()) {
  48.                 // Provjera kreatora ugovora jel isti kao trenutno logirani korisnik ($requester)
  49.                 if ((int)$requester === $user->getReference())
  50.                     return true;
  51.             }
  52.         }
  53.         return false;
  54.     }
  55. }