src/Security/Voter/AddSiteVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\DTO\Site\AddSite;
  4. use App\Entity\User;
  5. use App\Service\Manager\AccountManager;
  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 AddSiteVoter extends Voter
  10. {
  11.     const ADD_SITE 'ADD_SITE';
  12.     const EDIT_SITE 'EDIT_SITE';
  13.     /**
  14.      * @var AccountManager
  15.      */
  16.     private $accountManager;
  17.     public function __construct(AccountManager $accountManager)
  18.     {
  19.         $this->accountManager $accountManager;
  20.     }
  21.     protected function supports($attribute$subject): bool
  22.     {
  23.         if (!in_array($attribute, [self::ADD_SITEself::EDIT_SITE])) {
  24.             return false;
  25.         }
  26.         if (!$subject instanceof AddSite) {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.     /**
  32.      * @param string         $attribute
  33.      * @param AddSite        $subject
  34.      * @param TokenInterface $token
  35.      *
  36.      * @return bool
  37.      */
  38.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  39.     {
  40.         /** @var User $user */
  41.         $user $token->getUser();
  42.         if (!$user instanceof UserInterface) {
  43.             return false;
  44.         }
  45.         if (null === $subject->account) {
  46.             return true;
  47.         }
  48.         if (null === $account $this->accountManager->getAccountById($subject->account)) {
  49.             return false;
  50.         }
  51.         return $this->accountManager->hasEditPermission($account$user);
  52.     }
  53. }