src/Security/Voter/Purchase/PurchaseRequestVoter.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 PurchaseRequestVoter 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_REQUEST_CREATE',
  22.             'ROLE_PURCHASE_REQUEST_VIEW',
  23.             'ROLE_PURCHASE_REQUEST_EDIT',
  24.             'ROLE_PURCHASE_REQUEST_ENABLE',
  25.             'ROLE_PURCHASE_REQUEST_DISABLE',
  26.             'ROLE_PURCHASE_REQUEST_DELETE',
  27.             'ROLE_PURCHASE_REQUEST_ARCHIVE',
  28.             'ROLE_PURCHASE_REQUEST_NOT_ARCHIVE',
  29.             'ROLE_PURCHASE_REQUEST_DUPLICATE',
  30.             'ROLE_PURCHASE_REQUEST_QUOTATION',
  31.             'ROLE_PURCHASE_REQUEST_ORDER',
  32.             'ROLE_PURCHASE_REQUEST_GENERATE'
  33.             ])) {
  34.             return false;
  35.         }
  36.         // only vote on `Activity` objects
  37.         if (!$subject instanceof PurchaseRequest) {
  38.             return false;
  39.         }
  40.         return true;
  41.     }
  42.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  43.     {
  44.         $user $token->getUser();
  45.         // if the user is anonymous, do not grant access
  46.         if (!$user instanceof UserInterface) {
  47.             return false;
  48.         }
  49.         // ... (check conditions and return true to grant permission) ...
  50.         switch ($attribute) {
  51.             case 'ROLE_PURCHASE_REQUEST_CREATE':
  52.                 return $this->canCreate($subject$user);
  53.                 break;
  54.             case 'ROLE_PURCHASE_REQUEST_VIEW':
  55.                 return $this->canView($subject$user);
  56.                 break;
  57.             case 'ROLE_PURCHASE_REQUEST_EDIT':
  58.                 return $this->canEdit($subject$user);
  59.                 break;
  60.             case 'ROLE_PURCHASE_REQUEST_DELETE':
  61.                 return $this->canDelete($subject$user);
  62.                 break;
  63.             case 'ROLE_PURCHASE_REQUEST_DUPLICATE':
  64.                 return $this->canDuplicate($subject$user);
  65.                 break;
  66.             case 'ROLE_PURCHASE_REQUEST_QUOTATION':
  67.                 return $this->canQuotation($subject$user);
  68.                 break;
  69.             case 'ROLE_PURCHASE_REQUEST_ORDER':
  70.                 return $this->canOrder($subject$user);
  71.                 break;
  72.             case 'ROLE_PURCHASE_REQUEST_DISABLE':
  73.                 return $this->canDisable($subject$user);
  74.                 break;
  75.             case 'ROLE_PURCHASE_REQUEST_ARCHIVE':
  76.                 return $this->canArchive($subject$user);
  77.                 break;
  78.             case 'ROLE_PURCHASE_REQUEST_NOT_ARCHIVE':
  79.                 return $this->canNotArchive($subject$user);
  80.                 break;
  81.             case 'ROLE_PURCHASE_REQUEST_ENABLE':
  82.                 return $this->canEnable($subject$user);
  83.                 break;
  84.             case 'ROLE_PURCHASE_REQUEST_GENERATE':
  85.                 return $this->canGenerate($subject$user);
  86.                 break;
  87.         }
  88.         return false;
  89.     }
  90.     private function canCreate(PurchaseRequest $purchaseRequestUser $user)
  91.     {
  92.         if (in_array('ROLE_PURCHASE_REQUEST_CREATE'$this->session->get('privileges'))) {
  93.             return true;
  94.         }
  95.         return false;
  96.     }
  97.     private function canView(PurchaseRequest $purchaseRequestUser $user)
  98.     {
  99.         if (in_array('ROLE_PURCHASE_REQUEST_VIEW'$this->session->get('privileges'))) {
  100.             return true;
  101.         }
  102.         return false;
  103.     }
  104.     private function canEdit(PurchaseRequest $purchaseRequestUser $user)
  105.     {
  106.         if (in_array('ROLE_PURCHASE_REQUEST_EDIT'$this->session->get('privileges'))) {
  107.             return true;
  108.         }
  109.         return false;
  110.     }
  111.     private function canDelete(PurchaseRequest $purchaseRequestUser $user)
  112.     {
  113.         if (in_array('ROLE_PURCHASE_REQUEST_DELETE'$this->session->get('privileges'))) {
  114.             return true;
  115.         }
  116.         return false;
  117.     }
  118.     private function canQuotation(PurchaseRequest $purchaseRequestUser $user)
  119.     {
  120.         if (in_array('ROLE_PURCHASE_REQUEST_QUOTATION'$this->session->get('privileges'))) {
  121.             return true;
  122.         }
  123.         return false;
  124.     }
  125.     private function canDuplicate(PurchaseRequest $purchaseRequestUser $user)
  126.     {
  127.         if (in_array('ROLE_PURCHASE_REQUEST_DUPLICATE'$this->session->get('privileges'))) {
  128.             return true;
  129.         }
  130.         return false;
  131.     }
  132.     private function canDisable(PurchaseRequest $purchaseRequestUser $user)
  133.     {
  134.         if (in_array('ROLE_PURCHASE_REQUEST_DISABLE'$this->session->get('privileges'))) {
  135.             return true;
  136.         }
  137.         return false;
  138.     }
  139.     private function canArchive(PurchaseRequest $purchaseRequestUser $user)
  140.     {
  141.         if (in_array('ROLE_PURCHASE_REQUEST_ARCHIVE'$this->session->get('privileges'))) {
  142.             return true;
  143.         }
  144.         return false;
  145.     }
  146.     private function canNotArchive(PurchaseRequest $purchaseRequestUser $user)
  147.     {
  148.         if (in_array('ROLE_PURCHASE_REQUEST_NOT_ARCHIVE'$this->session->get('privileges'))) {
  149.             return true;
  150.         }
  151.         return false;
  152.     }
  153.     private function canOrder(PurchaseRequest $purchaseRequestUser $user)
  154.     {
  155.         if (in_array('ROLE_PURCHASE_REQUEST_ORDER'$this->session->get('privileges'))) {
  156.             return true;
  157.         }
  158.         return false;
  159.     }
  160.     private function canGenerate(PurchaseRequest $purchaseRequestUser $user)
  161.     {
  162.         if (in_array('ROLE_PURCHASE_REQUEST_GENERATE'$this->session->get('privileges'))) {
  163.             return true;
  164.         }
  165.         return false;
  166.     }
  167.     private function canEnable(PurchaseRequest $purchaseRequestUser $user)
  168.     {
  169.         if (in_array('ROLE_PURCHASE_REQUEST_ENABLE'$this->session->get('privileges'))) {
  170.             return true;
  171.         }
  172.         return false;
  173.     }
  174. }