src/Security/Voter/Webmaster/WebmasterVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Webmaster;
  3. use App\Entity\Webmaster;
  4. use App\Security\ApiUser;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class WebmasterVoter extends Voter
  8. {
  9.     public const STATS 'stats';
  10.     public const TRAFFIC 'traffic';
  11.     public const ADS 'ads';
  12.     public const CAMPAIGNS 'campaigns';
  13.     public const EDIT 'edit';
  14.     public const PERMISSION_READ_SENSITIVE 'read_sensitive';
  15.     public const PERMISSION_WRITE_SENSITIVE 'write_sensitive';
  16.     protected function supports(string $attribute$subject): bool
  17.     {
  18.         if (!$subject instanceof Webmaster) {
  19.             return false;
  20.         }
  21.         return in_array($attribute, [
  22.             self::STATSself::TRAFFIC,
  23.             self::ADS,
  24.             self::CAMPAIGNS,
  25.             self::EDIT,
  26.             self::PERMISSION_READ_SENSITIVE,
  27.             self::PERMISSION_WRITE_SENSITIVE,
  28.         ], true);
  29.     }
  30.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  31.     {
  32.         $user $token->getUser();
  33.         if (!$user instanceof ApiUser) {
  34.             return false;
  35.         }
  36.         // admin should be able to do anything
  37.         if ($user->getIsAdmin()) {
  38.             return true;
  39.         }
  40.         /*
  41.          * @var $subject Webmaster
  42.          */
  43.         return $user->getIsWebmaster() && $user->getWebmaster()->getId() === $subject->getId();
  44.     }
  45. }