src/AppBundle/Services/Voter/WebsiteVoter.php line 9

Open in your IDE?
  1. <?php 
  2. namespace AppBundle\Services\Voter;
  3. use AppBundle\Entity\Website;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class WebsiteVoter extends Voter
  7. {
  8.     public const VIEW 'view';
  9.     public const EDIT 'edit';
  10.     protected function supports($attribute$subject)
  11.     {
  12.         // if the attribute isn't one we support, return false
  13.         if (!in_array($attribute, [self::VIEWself::EDIT])) {
  14.             return false;
  15.         }
  16.         // only vote on Post objects inside this voter
  17.         if (!$subject instanceof Website) {
  18.             return false;
  19.         }
  20.         return true;
  21.     }
  22.     protected function voteOnAttribute($attribute$websiteTokenInterface $token)
  23.     {
  24.         $user $token->getUser();
  25.         // To do : show if active else show only if user is owner
  26.         if ($attribute == self::VIEW && $website == $user) {
  27.             return true;
  28.         }
  29.         // To do : edit only if user is owner
  30.         if ($attribute == self::EDIT && $website == $user) {
  31.             return true;
  32.         }
  33.         return false;
  34.     }
  35. }