src/Security/Voter/AliasVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Util\AccountTools;
  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. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Symfony\Component\Security\Core\Security;
  9. class AliasVoter extends Voter
  10. {
  11.     private $security;
  12.     
  13.     public function __construct(Security $security)
  14.     {
  15.         $this->security $security;
  16.     }
  17.     
  18.     protected function supports($attribute$subject) : bool
  19.     {
  20.         // NEW actions don't yet have objects
  21.         if(in_array($attribute, ['NEW_ALIAS']))
  22.             return true;
  23.             
  24.             return in_array($attribute, ['EDIT''DELETE''EDITGLOBAL''MASS_SEND'])
  25.             && $subject instanceof \App\Model\Alias;
  26.     }
  27.     
  28.     protected function voteOnAttribute($attribute$subjectTokenInterface $token) : bool
  29.     {
  30.         $user $token->getUser();
  31.         // if the user is anonymous, do not grant access
  32.         if (!$user instanceof UserInterface) {
  33.             return false;
  34.         }
  35.         
  36.         switch ($attribute) {
  37.             case 'EDIT':
  38.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  39.                     return true;
  40.                 }
  41.                 
  42.                 if($this->security->isGranted('ROLE_GROUPMASTER')) {
  43.                     foreach($user->getAccounts() as $acc) {
  44.                         if($acc == $subject->getDest()) {
  45.                             return true;
  46.                         }
  47.                     }
  48.                     return false;
  49.                 }
  50.                 break;
  51.                     
  52.             case 'DELETE':
  53.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  54.                     // check the list of system aliases
  55.                     if(!isset($_SERVER['APP_SYSTEM_ALIASES'])) {
  56.                         return true;
  57.                     }
  58.                     
  59.                     $systemAliases AccountTools::envToArray($_SERVER['APP_SYSTEM_ALIASES']);
  60.                     $aliasSrcArr explode('@'$subject->getSrc());
  61.                                         
  62.                     if(count($aliasSrcArr) != 2) {
  63.                         return false;
  64.                     }
  65.                     
  66.                     foreach($systemAliases as $sa) {
  67.                         if($aliasSrcArr[0] == $sa) {
  68.                             return false;
  69.                         }
  70.                     }
  71.                     
  72.                     return true;
  73.                 }
  74.                 break;
  75.                                         
  76.             case 'NEW_ALIAS':
  77.                 if($this->security->isGranted('ROLE_POSTMASTER'))
  78.                     return true;
  79.                 break;
  80.                     
  81.             case 'EDITGLOBAL':
  82.                 if($this->security->isGranted('ROLE_ADMIN')) {
  83.                     return true;
  84.                 }
  85.                 
  86.                 if($this->security->isGranted('ROLE_POSTMASTER_MANY')) {
  87.                     if($subject->getSrc() == "") { // new alias
  88.                         return true;
  89.                     }
  90.                     
  91.                     $userDomainArr explode('@'$subject->getSrc());
  92.                     
  93.                     if(sizeof($userDomainArr) != 2) {
  94.                         return false;
  95.                     }
  96.                     
  97.                     foreach($user->getDomains() as $d) {
  98.                         if($userDomainArr[1] == $d) {
  99.                             return true;
  100.                         }
  101.                     }
  102.                     return false;
  103.                 }
  104.                 
  105.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  106.                     if($subject->getSrc() == "") { // new alias
  107.                         return true;
  108.                     }
  109.                     
  110.                     $subjUserDomainArr explode('@'$subject->getSrc());
  111.                     $userDomainArr explode('@'$user->getUsername());
  112.                     
  113.                     if(sizeof($subjUserDomainArr) != || sizeof($userDomainArr) != 2) {
  114.                         return false;
  115.                     }
  116.                     
  117.                     if($userDomainArr[1] == $subjUserDomainArr[1]) {
  118.                         return true;
  119.                     }
  120.                     
  121.                     return false;                    
  122.                 }
  123.                 break;
  124.                 
  125.             case 'MASS_SEND':
  126.                 $userDomainArr explode('@'$subject->getSrc());
  127.                 
  128.                 if(sizeof($userDomainArr) != 2) {
  129.                     return false;
  130.                 }
  131.                 
  132.                 if($this->security->isGranted('ROLE_ADMIN')) {
  133.                     return true;
  134.                 }
  135.                 
  136.                 if($this->security->isGranted('ROLE_POSTMASTER')) {
  137.                     if(in_array($userDomainArr[1], $user->getDomains())) {
  138.                         return true;
  139.                     }
  140.                 }
  141.                 if($this->security->isGranted('ROLE_POSTMASTER_MIN')) {
  142.                     if(in_array($userDomainArr[1], $user->getDomains())) {
  143.                         return true;
  144.                     }
  145.                 }
  146.                 
  147.                 if($this->security->isGranted('ROLE_GROUPMASTER')) {
  148.                     if(in_array($userDomainArr[1], $user->getDomains())) {
  149.                         return true;
  150.                         /*if(in_array($subject->getSrc(), $user->getAccounts()))
  151.                             return true;*/
  152.                     }
  153.                 }
  154.                 
  155.                 if($this->security->isGranted('ROLE_USER')) {
  156.                     // only possible to "mass send" to his own account
  157.                     if($subject->getSrc() == $user->getUsername()) {
  158.                         return true;
  159.                     }
  160.                 }
  161.                 break;
  162.         }
  163.         
  164.         return false;
  165.     }
  166. }