src/Security/Voter/PubVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Shareregister;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class PubVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject)
  10.     {
  11.         return in_array($attribute, ['PUB_EDIT''PUB_VIEW'])
  12.             && $subject instanceof \App\Entity\Pub;
  13.     }
  14.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  15.     {
  16.         $user $token->getUser();
  17.         // if the user is anonymous, do not grant access
  18.         if (!$user instanceof UserInterface) {
  19.             return false;
  20.         }
  21.         if(in_array('ROLE_MANAGER'$user->getRoles())) {
  22.             return true;
  23.         }
  24.         if(in_array('ROLE_ALTERNATIVE_MANAGER'$user->getRoles())) {
  25.             return true;
  26.         }
  27.         if(in_array('ROLE_ADMIN'$user->getRoles())) {
  28.             return true;
  29.         }
  30.         if(in_array('ROLE_SUPER_ADMIN'$user->getRoles())) {
  31.             return true;
  32.         }
  33.         // ... (check conditions and return true to grant permission) ...
  34.         switch ($attribute) {
  35.             case 'PUB_EDIT':
  36.                 // logic to determine if the user can EDIT
  37.                 // return true or false
  38.                 break;
  39.             case 'PUB_VIEW':
  40.                /** @var Shareregister $share */
  41.                 foreach ($subject->getShares() as $share ) {
  42.                     if($share->getInvestmentAmount() < 1) {
  43.                         continue;
  44.                     }
  45.                     if($share->getUser()->getUsername() == $user->getUsername()) {
  46.                         return true;
  47.                     }
  48.                 }
  49.                 return false;
  50.                 break;
  51.         }
  52.         return false;
  53.     }
  54. }