src/Security/Voter/AccountVoter.php line 12

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