src/Security/UserAccessControlVoter.php line 13

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\NotificationSystem;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. /**
  7.  * Class TendererAccessControlVoter
  8.  * @package App\Security\UserAccess
  9.  */
  10. class UserAccessControlVoter extends Voter
  11. {
  12.     /**
  13.      * @param string $attribute
  14.      * @param mixed $subject
  15.      * @return bool
  16.      */
  17.     protected function supports(string $attribute$subject) :bool
  18.     {
  19.         if ($attribute === 'user_access_control') {
  20.             return true;
  21.         }
  22.         return false;
  23.     }
  24.     /**
  25.      * @param string $attribute
  26.      * @param mixed $subject
  27.      * @param TokenInterface $token
  28.      * @return bool
  29.      */
  30.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) :bool
  31.     {
  32.         /**
  33.          * @var int|bool $institutionId
  34.          * Vraća id od trenutnog grada(korisnika)
  35.          */
  36.         $requester $subject->getReference();
  37.         $user $token->getUser();
  38.         if ($subject instanceof NotificationSystem) {
  39.             if ((int)$requester && is_object($user)) {
  40.                 if ((int)$requester === $user->getId()) {
  41.                     return true;
  42.                 }
  43.             }
  44.         }
  45.         if ((int)$requester && is_object($user)) {
  46.             if ((int)$requester === $user->getCity()->getId()) {
  47.                 return true;
  48.             }
  49.         }
  50.         return false;
  51.     }
  52. }