src/Security/Voter/SupplierVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Supplier;
  4. use App\Entity\Company;
  5. use App\Entity\Equipment;
  6. use App\Entity\Material;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use App\Entity\User;
  11. use Symfony\Component\Security\Core\Security;
  12. use App\Repository\CompanySupplierRepository;
  13. use App\Service\UtilService;
  14. class SupplierVoter extends Voter
  15. {
  16.     const VIEW ="ver";
  17.     const EDIT ="editar";
  18.     const CONFIG ="configurar";
  19.     const DELETE "eliminar";
  20.     const VIEW_EQ 'ver_equipos';
  21.     const MANAGE_EQ 'equipos';
  22.     const VIEW_MA 'ver_materiales';
  23.     const MANAGE_MA 'materiales';
  24.     private $security;
  25.     private $csRepository;
  26.     private $util;
  27.     public function __construct(
  28.         Security $security
  29.         CompanySupplierRepository $csRepository,
  30.         UtilService $utilService)
  31.     {
  32.         $this->security $security;
  33.         $this->csRepository $csRepository;
  34.         $this->util $utilService;
  35.     }
  36.     protected function supports($attribute$subject)
  37.     {
  38.         // if the attribute isn't one we support, return false
  39.         if (!in_array($attribute, [
  40.             self::VIEWself::EDITself::CONFIGself::DELETEself::VIEW_EQself::VIEW_MAself::MANAGE_EQself::MANAGE_MA])) {
  41.             return false;
  42.         }
  43.         // only vote on `Supplier, Equipment or Material` objects
  44.         if (!$subject instanceof Supplier && !$subject instanceof Equipment && !$subject instanceof Material) {
  45.             return false;
  46.         }
  47.         return true;
  48.     }
  49.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  50.     {
  51.         $admin $token->getUser();
  52.         // if the user is anonymous, do not grant access
  53.         if (!$admin instanceof UserInterface) {
  54.             return false;
  55.         }
  56.         // ROLE_ADMIN can do anything! The power!
  57.         if ($this->security->isGranted('ROLE_ADMIN')) {
  58.             return true;
  59.         }
  60.         
  61.         // ... (check conditions and return true to grant permission) ...
  62.         switch ($attribute) {
  63.             case self::VIEW:
  64.                 return $this->canView($subject$admin);
  65.             case self::EDIT:
  66.                 return $this->canEdit($subject$admin);
  67.             case self::CONFIG:
  68.                     return $this->canConfig($subject$admin);
  69.             case self::DELETE:
  70.                 return $this->canDelete($subject$admin);
  71.             case self::VIEW_EQ:
  72.                 return $this->canViewEq($subject$admin);
  73.             case self::VIEW_MA:
  74.                 return $this->canViewMa($subject$admin);
  75.             case self::MANAGE_EQ:
  76.                 return $this->canManageEq($subject$admin);
  77.             case self::MANAGE_MA:
  78.                 return $this->canManageMa($subject$admin);
  79.         }
  80.         return false
  81.     }
  82.     
  83.     private function canView(Supplier $supplierUser $admin)
  84.     {      
  85.         return $this->checkCompanySupplier($admin$supplier);
  86.     }
  87.     private function canEdit(Supplier $supplierUser $admin)
  88.     {  
  89.         if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER_CHIEF') {
  90.             return $this->checkCompanySupplier($admin$supplier1);
  91.         }
  92.         return false;        
  93.     }
  94.     private function canConfig(Supplier $supplierUser $admin)
  95.     {  
  96.         if ( $admin->getRole() == 'ROLE_CHIEF') {
  97.             return $this->checkCompanySupplier($admin$supplier);
  98.         }
  99.         return false;        
  100.     }
  101.     private function canDelete(Supplier $supplierUser $admin)
  102.     {
  103.         return $this->checkCompanySupplier($admin$supplier);
  104.     }
  105.     private function checkCompanySupplier(User $adminSupplier $supplier$type=0){
  106.         $suppliersId $this->csRepository->findByCompany($admin->getCompany(), $typetrue);
  107.         if(in_array($supplier->getId(),$suppliersId)){
  108.             return true;
  109.         }
  110.         else{
  111.             return false;
  112.         }
  113.     }
  114.     private function canViewEq(Equipment $eqUser $user)
  115.     {
  116.         $isAllowed $this->util->getLicenseField($user->getCompany(),'marketplace');
  117.         if($eq->getId()){
  118.             $isAllowed $user->getCompany() === $eq->getCompany();
  119.         }        
  120.         return $isAllowed;
  121.     }
  122.     private function canViewMa(Material $maUser $user)
  123.     {        
  124.         $isAllowed $this->util->getLicenseField($user->getCompany(),'marketplace');
  125.         if($ma->getId()){
  126.             $isAllowed $user->getCompany() === $ma->getCompany();
  127.         }        
  128.         return $isAllowed;
  129.     }
  130.     private function canManageEq(Equipment $eqUser $user)
  131.     {
  132.         $isAdmin in_array($user->getRole(),['ROLE_CHIEF','ROLE_SUPPLIER_CHIEF']);
  133.         $isAllowed $this->util->getLicenseField($user->getCompany(),'marketplace') && $isAdmin;
  134.         if($eq->getId()){
  135.             $isAllowed $user->getCompany() === $eq->getCompany();
  136.         }        
  137.         return $isAllowed;
  138.     }
  139.     private function canManageMa(Material $maUser $user)
  140.     {
  141.         $isAdmin in_array($user->getRole(),['ROLE_CHIEF','ROLE_SUPPLIER_CHIEF']);        
  142.         $isAllowed $this->util->getLicenseField($user->getCompany(),'marketplace') && $isAdmin;
  143.         if($ma->getId()){
  144.             $isAllowed $user->getCompany() === $ma->getCompany();
  145.         }        
  146.         return $isAllowed;
  147.     }
  148. }