src/Security/Voter/SwitchUserVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Util\AppLogger;
  4. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Symfony\Component\Security\Core\Security;
  10. class SwitchUserVoter extends Voter
  11. {
  12.     private $security;
  13.     private $logger;
  14.     
  15.     public function __construct(Security $securityAppLogger $logger)
  16.     {
  17.         $this->security $security;
  18.         $this->logger $logger;
  19.     }
  20.     
  21.     protected function supports($attribute$subject) : bool
  22.     {
  23.         return in_array($attribute, ['CAN_SWITCH_USER'])
  24.         && $subject instanceof UserInterface;
  25.     }
  26.     
  27.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) : bool
  28.     {
  29.         $user $token->getUser();
  30.         // if the user is anonymous, do not grant access
  31.         if (!$user instanceof UserInterface || !$subject instanceof UserInterface) {
  32.             return false;
  33.         }
  34.         // admin can switch to any user
  35.         if($this->security->isGranted('ROLE_ADMIN') && $this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  36.             return true;
  37.         }
  38.         
  39.         // postmaster - we have to check domain
  40.         if($this->security->isGranted('ROLE_POSTMASTER') && $this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  41.             $domains $user->getDomains();
  42.             $domainToCheck $subject->getUserDomainPart();
  43.             
  44.             foreach($domains as $domain) {
  45.                 if($domain == $domainToCheck) {
  46.                     // don't allow switching to more powerful role
  47.                     foreach($subject->getRoleNames() as $role) {
  48.                         if($role == 'ROLE_ADMIN' || $role == 'ROLE_SUPER_ADMIN') {
  49.                             $this->logger->log('access denied: POSTMASTER switching to more powerful user ' $subject->getUsername() . ' from ' $user->getUsername(), null, [], 'alert');
  50.                             return false;
  51.                         }
  52.                     }
  53.                     return true;
  54.                 }
  55.             }
  56.             
  57.             $this->logger->log('access denied: POSTMASTER switching to user ' $subject->getUsername() . ' from ' $user->getUsername(), null, [], 'alert');
  58.             return false;
  59.         }
  60.         
  61.         // groupmaster && usermaster - we have to check accounts this user has access to
  62.         if($this->security->isGranted('ROLE_USERMASTER') && $this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  63.             $accounts $user->getAccounts();
  64.             $accountToCheck $subject->getUsername();
  65.             
  66.             foreach($accounts as $account) {
  67.                 if($account == $accountToCheck) {
  68.                     return true;
  69.                 }
  70.             }
  71.             
  72.             $this->logger->log('access denied: USERMASTER switching to user ' $subject->getUsername() . ' from ' $user->getUsername(), null, [], 'alert');
  73.             return false;
  74.         }
  75.         
  76.         $this->logger->log('access denied: switching to user ' $subject->getUsername() . ' from ' $user->getUsername(), null, [], 'alert');
  77.         return false;
  78.         
  79.     }
  80. }