src/Security/Voter/OrderVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Order;
  4. use App\Entity\User;
  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 Symfony\Component\Security\Core\Security;
  9. use App\Repository\CompanySupplierRepository;
  10. class OrderVoter extends Voter
  11. {
  12.     const VIEW ="ver";
  13.     const CREATE ="crear";
  14.     const EDIT ="editar";
  15.     const ACCEPT ="aceptar";
  16.     const REJECT ="rechazar";
  17.     const ACCEPT_SUPPLIER ="aceptar proveedor";
  18.     const COMPLETE ="completar";
  19.     const DELETE "eliminar";
  20.     const SEND "enviar";
  21.     private $security;
  22.     private $csRepository;
  23.     public function __construct(Security $securityCompanySupplierRepository $csRepository)
  24.     {
  25.         $this->security $security;
  26.         $this->csRepository $csRepository;
  27.     }
  28.     protected function supports($attribute$subject)
  29.     {
  30.         // if the attribute isn't one we support, return false
  31.         if (!in_array($attribute, [self::VIEWself::CREATEself::EDITself::ACCEPTself::REJECTself::ACCEPT_SUPPLIERself::SENDself::COMPLETEself::DELETE])) {
  32.             return false;
  33.         }
  34.         // only vote on `Order` objects
  35.         if (!$subject instanceof Order) {
  36.             return false;
  37.         }
  38.         return true;
  39.     }
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  41.     {
  42.         $admin $token->getUser();
  43.         // if the user is anonymous, do not grant access
  44.         if (!$admin instanceof UserInterface) {
  45.             return false;
  46.         }
  47.         // ROLE_ADMIN can do anything! The power!
  48.         if ($this->security->isGranted('ROLE_ADMIN')) {
  49.             return true;
  50.         }
  51.         // ... (check conditions and return true to grant permission) ...
  52.         switch ($attribute) {
  53.             case self::VIEW:
  54.                 return $this->canView($subject$admin);
  55.             case self::CREATE:
  56.                 return $this->canCreate($admin);
  57.             case self::EDIT:
  58.                 return $this->canEdit($subject$admin);
  59.             case self::ACCEPT:
  60.                 return $this->canAccept($subject$admin);
  61.             case self::REJECT:
  62.                 return $this->canReject($subject$admin);
  63.             case self::ACCEPT_SUPPLIER;
  64.                 return $this->canAcceptSupplier($subject$admin);
  65.             case self::SEND:
  66.                 return $this->canSend($subject$admin);
  67.             case self::COMPLETE:
  68.                 return $this->canComplete($subject$admin);
  69.             case self::DELETE:
  70.                 return $this->canDelete($subject$admin);
  71.         }
  72.         return false
  73.     }
  74.     
  75.     private function canView(Order $orderUser $admin)
  76.     { 
  77.         
  78.         if ( in_array($admin->getRole(),['ROLE_CHIEF','ROLE_USER','ROLE_CLIENT']) ) {
  79.            
  80.             return $admin->getCompany() === $order->getCompany() || $admin->getCompany() === $order->getSupplier()->getCompany();
  81.         }else if ($admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER'){
  82.             
  83.             return $admin === $order->getSupplier()->getUser();
  84.             
  85.         }
  86.         return false;   
  87.     }
  88.     private function canCreate(User $admin)
  89.     {      
  90.         if ( in_array($admin->getRole(),['ROLE_CHIEF','ROLE_USER','ROLE_CLIENT']) ) {
  91.         
  92.             return true;
  93.         }
  94.         return false;        
  95.     }
  96.     private function canEdit(Order $orderUser $admin)
  97.     {        
  98.         if ( $admin->getRole() == 'ROLE_CHIEF') {
  99.             return $this->canDelete($order$admin);
  100.         }
  101.         return false;        
  102.     }
  103.     private function canAccept(Order $orderUser $admin)
  104.     { 
  105.         if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_USER' ) {
  106.             return $admin->getCompany() === $order->getCompany();
  107.         }
  108.         return false;        
  109.     }
  110.     private function canReject(Order $orderUser $admin)
  111.     { 
  112.         return $admin->getCompany() === $order->getCompany();
  113.     }
  114.     
  115.     private function canAcceptSupplier(Order $orderUser $admin)
  116.     { 
  117.         if ( $admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER' ) {
  118.             return $admin === $order->getSupplier()->getUser();
  119.         }
  120.         return false;        
  121.     }
  122.     private function canSend(Order $orderUser $admin)
  123.     { 
  124.         if ( $admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER' ) {
  125.             return $admin === $order->getSupplier()->getUser();
  126.         }
  127.         return false;        
  128.     }
  129.     private function canComplete(Order $orderUser $admin)
  130.     {        
  131.         if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_USER' ) {
  132.             return $admin->getCompany() === $order->getCompany();
  133.         }
  134.         return false;        
  135.     }
  136.     private function canDelete(Order $orderUser $admin)
  137.     {
  138.         if ( $order->getState() == && $admin->getRole() == 'ROLE_CHIEF') {
  139.                 return $admin->getCompany() === $order->getCompany();
  140.         }
  141.         
  142.         return false
  143.     }
  144. }