src/Security/Voter/PartVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Part;
  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 App\Entity\User;
  8. use Symfony\Component\Security\Core\Security;
  9. class PartVoter extends Voter
  10. {
  11.     const VIEW ="ver";
  12.     const CREATE ="crear";
  13.     const EDIT ="editar";
  14.     const DELETE "eliminar";
  15.     const IMPORT "importar";
  16.     private $security;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.     }
  21.     protected function supports($attribute$subject)
  22.     {
  23.         // if the attribute isn't one we support, return false
  24.         if (!in_array($attribute, [self::VIEWself::CREATEself::EDITself::DELETEself::IMPORT])) {
  25.             return false;
  26.         }
  27.         // only vote on `Part` objects
  28.         if (!$subject instanceof Part) {
  29.             return false;
  30.         }
  31.         return true;
  32.     }
  33.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  34.     {
  35.         $admin $token->getUser();
  36.         // if the user is anonymous, do not grant access
  37.         if (!$admin instanceof UserInterface) {
  38.             return false;
  39.         }
  40.         // ROLE_ADMIN can do anything! The power!
  41.         if ($this->security->isGranted('ROLE_ADMIN')) {
  42.             return true;
  43.         }
  44.         // ... (check conditions and return true to grant permission) ...
  45.         switch ($attribute) {
  46.             case self::VIEW:
  47.                 return $this->canView($subject$admin);
  48.             case self::CREATE:
  49.                 return $this->canCreate($admin);
  50.             case self::EDIT:
  51.                 return $this->canEdit($subject$admin);
  52.             case self::DELETE:
  53.                 return $this->canDelete($subject$admin);
  54.             case self::IMPORT:
  55.                 return $this->canImport($subject$admin);
  56.         }
  57.         return false
  58.     }
  59.     private function canCreate(User $admin)
  60.     {      
  61.         if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_USER' ) {
  62.         
  63.             return true;
  64.         }
  65.         return false;        
  66.     }
  67.     
  68.     private function canView(Part $partUser $admin)
  69.     { 
  70.         
  71.         if ( in_array($admin->getRole(),['ROLE_CHIEF','ROLE_USER','ROLE_CLIENT']) ) {
  72.            
  73.             return $admin->getCompany() === $part->getCompany() ;
  74.         }else if ($admin->getRole() == 'ROLE_SUPPLIER_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER'){
  75.             
  76.             return $admin->getId() === $part->getSupplier()->getUser()->getId();
  77.             
  78.         }
  79.         return false;   
  80.     }
  81.     private function canEdit(Part $partUser $admin)
  82.     {        
  83.         if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_USER' ) {
  84.             return $admin->getCompany() === $part->getCompany() ;
  85.         }
  86.         return false;        
  87.     }
  88.     private function canDelete(Part $partUser $admin)
  89.     {
  90.         if ( $admin->getRole() == 'ROLE_CHIEF') {
  91.             return $admin->getCompany() === $part->getCompany();
  92.         }
  93.         return false
  94.     }
  95.     
  96.     private function canImport(Part $partUser $admin)
  97.     { 
  98.         $cs $admin->getCompany()->getLicenseCompany();
  99.         if($cs){
  100.             if($cs->getLicense()->getMassiveUpload()){
  101.                 return true;
  102.             }
  103.         }
  104.         return false;   
  105.     }
  106. }