src/Security/Voter/EnterpriseVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use App\Entity\User;
  7. class EnterpriseVoter extends Voter
  8. {
  9.     protected function supports($attribute$subject)
  10.     {
  11.         return in_array($attribute, ['create''edit','view'])
  12.             && $subject instanceof \App\Entity\Enterprise;
  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.         $enterprise $user->getEnterprise();
  22.         if ($user->getRoles()[0] == User::ROLE_ADMIN) { return true;
  23.         }
  24.         if ($user->getRoles()[0] == User::ROLE_CUSTOMER){
  25.             return ($enterprise == $subject);
  26.         }
  27.         if ($user->getRoles()[0] == User::ROLE_BUSINESSLINEMANAGER){
  28.             foreach($subject->getComboCustomers() as $combo){
  29.                 if (in_array($combo->getProduct()->getFiliere()->getId(),$user->getAllowedFiliere()))
  30.                     return true;
  31.             }
  32.             foreach($subject->getComboProviders() as $combo){
  33.                 if (in_array($combo->getProduct()->getFiliere()->getId(),$user->getAllowedFiliere()))
  34.                     return true;
  35.             }            
  36.             return false;
  37.         }
  38.         if ($user->getRoles()[0] == User::ROLE_GROUPMANAGER){
  39.             return in_array($subject->getGroupEnterprise()->getId(),$user->getAllowedFiliere());
  40.         }
  41.         if ($user->getRoles()[0] == User::ROLE_REGIONMANAGER){
  42.             return in_array(substr($subject->getLocations()[0]->getZipCode(),0,2),$user->getAllowedFiliere());
  43.             return in_array(substr($subject->getLocations()[1]->getZipCode(),0,2),$user->getAllowedFiliere());
  44.             return false;
  45.         }        
  46.         return false;
  47.     }
  48. }