src/Security/Voter/Purchase/PurchaseOrderVoter.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 PurchaseOrderVoter 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_ORDER_CREATE',
  22.             'ROLE_PURCHASE_ORDER_VIEW',
  23.             'ROLE_PURCHASE_ORDER_EDIT',
  24.             'ROLE_PURCHASE_ORDER_ENABLE',
  25.             'ROLE_PURCHASE_ORDER_DISABLE',
  26.             'ROLE_PURCHASE_ORDER_DELETE',
  27.             'ROLE_PURCHASE_ORDER_ARCHIVE',
  28.             'ROLE_PURCHASE_ORDER_NOT_ARCHIVE',
  29.             'ROLE_PURCHASE_ORDER_VALIDATE',
  30.             'ROLE_PURCHASE_ORDER_REJECTED',
  31.             'ROLE_PURCHASE_ORDER_REVERSE',
  32.             'ROLE_PURCHASE_ORDER_GENERATE',
  33.             'ROLE_PURCHASE_ORDER_DUPLICATE',
  34.             'ROLE_PURCHASE_ORDER_DELIVERY',
  35.             'ROLE_PURCHASE_ORDER_RECEPTION',
  36.             'ROLE_PURCHASE_ORDER_INVOICE'])) {
  37.             return false;
  38.         }
  39.         // only vote on `Activity` objects
  40.         if (!$subject instanceof PurchaseRequest) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  46.     {
  47.         $user $token->getUser();
  48.         // if the user is anonymous, do not grant access
  49.         if (!$user instanceof UserInterface) {
  50.             return false;
  51.         }
  52.         // ... (check conditions and return true to grant permission) ...
  53.         switch ($attribute) {
  54.             case 'ROLE_PURCHASE_ORDER_CREATE':
  55.                 return $this->canCreate();
  56.                 break;
  57.             case 'ROLE_PURCHASE_ORDER_VIEW':
  58.                 return $this->canView();
  59.                 break;
  60.             case 'ROLE_PURCHASE_ORDER_EDIT':
  61.                 return $this->canEdit($subject$user);
  62.                 break;
  63.             case 'ROLE_PURCHASE_ORDER_ENABLE':
  64.                 return $this->canEnable($subject$user);
  65.                 break;
  66.             case 'ROLE_PURCHASE_ORDER_DISABLE':
  67.                 return $this->canDisable($subject$user);
  68.                 break;
  69.             case 'ROLE_PURCHASE_ORDER_DELETE':
  70.                 return $this->canDelete($subject$user);
  71.                 break;
  72.             case 'ROLE_PURCHASE_ORDER_ARCHIVE':
  73.                 return $this->canArchive($subject$user);
  74.                 break;
  75.             case 'ROLE_PURCHASE_ORDER_NOT_ARCHIVE':
  76.                 return $this->canNotArchive($subject$user);
  77.                 break;
  78.             case 'ROLE_PURCHASE_ORDER_VALIDATE':
  79.                 return $this->canValidate($subject$user);
  80.                 break;
  81.             case 'ROLE_PURCHASE_ORDER_REJECTED':
  82.                 return $this->canReject($subject$user);
  83.                 break;
  84.             case 'ROLE_PURCHASE_ORDER_REVERSE':
  85.                 return $this->canReverse($subject$user);
  86.                 break;
  87.             case 'ROLE_PURCHASE_ORDER_DUPLICATE':
  88.                 return $this->canDuplicate($subject$user);
  89.                 break;
  90.             case 'ROLE_PURCHASE_ORDER_DELIVERY':
  91.                 return $this->canDelivery($subject$user);
  92.                 break;
  93.             case 'ROLE_PURCHASE_ORDER_RECEPTION':
  94.                 return $this->canReception($subject$user);
  95.                 break;
  96.             case 'ROLE_PURCHASE_ORDER_GENERATE':
  97.                 return $this->canGenerate($subject$user);
  98.                 break;
  99.             case 'ROLE_PURCHASE_ORDER_INVOICE':
  100.                 return $this->canInvoice($subject$user);
  101.                 break;
  102.         }
  103.         return false;
  104.     }
  105.     private function canCreate()
  106.     {
  107.         if (in_array('ROLE_PURCHASE_ORDER_CREATE'$this->session->get('privileges'))) {
  108.             return true;
  109.         }
  110.         return false;
  111.     }
  112.     private function canView()
  113.     {
  114.         if (in_array('ROLE_PURCHASE_ORDER_VIEW'$this->session->get('privileges'))) {
  115.             return true;
  116.         }
  117.         return false;
  118.     }
  119.     private function canEdit(PurchaseRequest $purchaseOrderUser $user)
  120.     {
  121.         if (in_array('ROLE_PURCHASE_ORDER_EDIT'$this->session->get('privileges'))) {
  122.             return true;
  123.         }
  124.         return false;
  125.     }
  126.     private function canEnable(PurchaseRequest $purchaseOrderUser $user)
  127.     {
  128.         if (in_array('ROLE_PURCHASE_ORDER_ENABLE'$this->session->get('privileges'))) {
  129.             return true;
  130.         }
  131.         return false;
  132.     }
  133.     private function canDisable(PurchaseRequest $purchaseOrderUser $user)
  134.     {
  135.         if (in_array('ROLE_PURCHASE_ORDER_DISABLE'$this->session->get('privileges'))) {
  136.             return true;
  137.         }
  138.         return false;
  139.     }
  140.     private function canDelete(PurchaseRequest $purchaseOrderUser $user)
  141.     {
  142.         if (in_array('ROLE_PURCHASE_ORDER_DELETE'$this->session->get('privileges'))) {
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.     private function canArchive(PurchaseRequest $purchaseOrderUser $user)
  148.     {
  149.         if (in_array('ROLE_PURCHASE_ORDER_ARCHIVE'$this->session->get('privileges'))) {
  150.             return true;
  151.         }
  152.         return false;
  153.     }
  154.     private function canNotArchive(PurchaseRequest $purchaseOrderUser $user)
  155.     {
  156.         if (in_array('ROLE_PURCHASE_ORDER_NOT_ARCHIVE'$this->session->get('privileges'))) {
  157.             return true;
  158.         }
  159.         return false;
  160.     }
  161.     private function canValidate(PurchaseRequest $purchaseOrderUser $user)
  162.     {
  163.         if (in_array('ROLE_PURCHASE_ORDER_VALIDATE'$this->session->get('privileges'))) {
  164.             return true;
  165.         }
  166.         return false;
  167.     }
  168.     private function canReject(PurchaseRequest $purchaseOrderUser $user)
  169.     {
  170.         if (in_array('ROLE_PURCHASE_ORDER_REJECTED'$this->session->get('privileges'))) {
  171.             return true;
  172.         }
  173.         return false;
  174.     }
  175.     private function canReverse(PurchaseRequest $purchaseOrderUser $user)
  176.     {
  177.         if (in_array('ROLE_PURCHASE_ORDER_REVERSE'$this->session->get('privileges'))) {
  178.             return true;
  179.         }
  180.         return false;
  181.     }
  182.     private function canDuplicate(PurchaseRequest $purchaseOrderUser $user)
  183.     {
  184.         if (in_array('ROLE_PURCHASE_ORDER_DUPLICATE'$this->session->get('privileges'))) {
  185.             return true;
  186.         }
  187.         return false;
  188.     }
  189.     private function canDelivery(PurchaseRequest $purchaseOrderUser $user)
  190.     {
  191.         if (in_array('ROLE_PURCHASE_ORDER_DELIVERY'$this->session->get('privileges'))) {
  192.             return true;
  193.         }
  194.         return false;
  195.     }
  196.     private function canReception(PurchaseRequest $purchaseOrderUser $user)
  197.     {
  198.         if (in_array('ROLE_PURCHASE_ORDER_RECEPTION'$this->session->get('privileges'))) {
  199.             return true;
  200.         }
  201.         return false;
  202.     }
  203.     private function canGenerate(PurchaseRequest $purchaseOrderUser $user)
  204.     {
  205.         if (in_array('ROLE_PURCHASE_ORDER_GENERATE'$this->session->get('privileges'))) {
  206.             return true;
  207.         }
  208.         return false;
  209.     }
  210.     private function canInvoice(PurchaseRequest $purchaseOrderUser $user)
  211.     {
  212.         if (in_array('ROLE_PURCHASE_ORDER_INVOICE'$this->session->get('privileges'))) {
  213.             return true;
  214.         }
  215.         return false;
  216.     }
  217. }