src/Security/Voter/Purchase/PurchaseDeliveryVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Purchase;
  3. use App\Entity\Purchase\PurchaseRequest;
  4. use App\Entity\Security\User;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class PurchaseDeliveryVoter extends Voter
  10. {
  11.     private $session;
  12.     public function __construct(SessionInterface $session)
  13.     {
  14.         $this->session $session;
  15.     }
  16.     protected function supports($attribute$subject)
  17.     {
  18.         // replace with your own logic
  19.         // https://symfony.com/doc/current/security/voters.html
  20.         if (!in_array($attribute, [
  21.             'ROLE_PURCHASE_DELIVERY_CREATE',
  22.             'ROLE_PURCHASE_DELIVERY_VIEW',
  23.             'ROLE_PURCHASE_DELIVERY_EDIT',
  24.             'ROLE_PURCHASE_DELIVERY_DELETE',
  25.             'ROLE_PURCHASE_DELIVERY_ENABLE',
  26.             'ROLE_PURCHASE_DELIVERY_DISABLE',
  27.             'ROLE_PURCHASE_DELIVERY_ARCHIVE',
  28.             'ROLE_PURCHASE_DELIVERY_NOT_ARCHIVE',
  29.             'ROLE_PURCHASE_DELIVERY_ATTACHMENT',
  30.             
  31.             'ROLE_PURCHASE_DELIVERY_INVOICE',
  32.             'ROLE_PURCHASE_DELIVERY_RECEPTION'])) {
  33.             return false;
  34.         }
  35.         // only vote on `Activity` objects
  36.         if (!$subject instanceof PurchaseRequest) {
  37.             return false;
  38.         }
  39.         return true;
  40.     }
  41.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  42.     {
  43.         $user $token->getUser();
  44.         // if the user is anonymous, do not grant access
  45.         if (!$user instanceof UserInterface) {
  46.             return false;
  47.         }
  48.         // ... (check conditions and return true to grant permission) ...
  49.         switch ($attribute) {
  50.             case 'ROLE_PURCHASE_DELIVERY_CREATE':
  51.                 return $this->canCreate();
  52.                 break;
  53.             case 'ROLE_PURCHASE_DELIVERY_VIEW':
  54.                 return $this->canView();
  55.                 break;
  56.             case 'ROLE_PURCHASE_DELIVERY_EDIT':
  57.                 return $this->canEdit($subject$user);
  58.                 break;
  59.             case 'ROLE_PURCHASE_DELIVERY_DELETE':
  60.                 return $this->canDelete($subject$user);
  61.                 break;
  62.             case 'ROLE_PURCHASE_DELIVERY_ENABLE':
  63.                 return $this->canEnable($subject$user);
  64.                 break;
  65.             case 'ROLE_PURCHASE_DELIVERY_DISABLE':
  66.                 return $this->canDisable($subject$user);
  67.                 break;
  68.             case 'ROLE_PURCHASE_DELIVERY_ARCHIVE':
  69.                 return $this->canArchive($subject$user);
  70.                 break;
  71.             case 'ROLE_PURCHASE_DELIVERY_NOT_ARCHIVE':
  72.                 return $this->canNotArchive($subject$user);
  73.                 break;
  74.             case 'ROLE_PURCHASE_DELIVERY_ATTACHMENT':
  75.                 return $this->canUpload($subject$user);
  76.                 break;
  77.             case 'ROLE_PURCHASE_DELIVERY_INVOICE':
  78.                 return $this->canInvoice($subject$user);
  79.                 break;
  80.             case 'ROLE_PURCHASE_DELIVERY_RECEPTION':
  81.                 return $this->canReception($subject$user);
  82.                 break;
  83.         }
  84.         return false;
  85.     }
  86.     private function canCreate()
  87.     {
  88.         if (in_array('ROLE_PURCHASE_DELIVERY_CREATE'$this->session->get('privileges'))) {
  89.             return true;
  90.         }
  91.         return false;
  92.     }
  93.     private function canView()
  94.     {
  95.         if (in_array('ROLE_PURCHASE_DELIVERY_VIEW'$this->session->get('privileges'))) {
  96.             return true;
  97.         }
  98.         return false;
  99.     }
  100.     private function canEdit(PurchaseRequest $purchaseDeliveryUser $user)
  101.     {
  102.         if (in_array('ROLE_PURCHASE_DELIVERY_EDIT'$this->session->get('privileges'))) {
  103.             return true;
  104.         }
  105.         return false;
  106.     }
  107.     private function canDelete(PurchaseRequest $purchaseDeliveryUser $user)
  108.     {
  109.         if (in_array('ROLE_PURCHASE_DELIVERY_DELETE'$this->session->get('privileges'))) {
  110.             return true;
  111.         }
  112.         return false;
  113.     }
  114.     private function canInvoice(PurchaseRequest $purchaseDeliveryUser $user)
  115.     {
  116.         if (in_array('ROLE_PURCHASE_DELIVERY_INVOICE'$this->session->get('privileges'))) {
  117.             return true;
  118.         }
  119.         return false;
  120.     }
  121.     private function canReception(PurchaseRequest $purchaseDeliveryUser $user)
  122.     {
  123.         if (in_array('ROLE_PURCHASE_DELIVERY_RECEPTION'$this->session->get('privileges'))) {
  124.             return true;
  125.         }
  126.         return false;
  127.     }
  128.     private function canUpload(PurchaseRequest $purchaseDeliveryUser $user)
  129.     {
  130.         if (in_array('ROLE_PURCHASE_DELIVERY_ATTACHMENT'$this->session->get('privileges'))) {
  131.             return true;
  132.         }
  133.         return false;
  134.     }
  135.     private function canDisable(PurchaseRequest $purchaseDeliveryUser $user)
  136.     {
  137.         if (in_array('ROLE_PURCHASE_DELIVERY_DISABLE'$this->session->get('privileges'))) {
  138.             return true;
  139.         }
  140.         return false;
  141.     }
  142.     private function canArchive(PurchaseRequest $purchaseDeliveryUser $user)
  143.     {
  144.         if (in_array('ROLE_PURCHASE_DELIVERY_ARCHIVE'$this->session->get('privileges'))) {
  145.             return true;
  146.         }
  147.         return false;
  148.     }
  149.     private function canNotArchive(PurchaseRequest $purchaseDeliveryUser $user)
  150.     {
  151.         if (in_array('ROLE_PURCHASE_DELIVERY_NOT_ARCHIVE'$this->session->get('privileges'))) {
  152.             return true;
  153.         }
  154.         return false;
  155.     }
  156.     private function canEnable(PurchaseRequest $purchaseDeliveryUser $user)
  157.     {
  158.         if (in_array('ROLE_PURCHASE_DELIVERY_ENABLE'$this->session->get('privileges'))) {
  159.             return true;
  160.         }
  161.         return false;
  162.     }
  163. }