src/Security/Voter/Purchase/PurchaseQuotationVoter.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 PurchaseQuotationVoter 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_QUOTATION_CREATE',
  22.             'ROLE_PURCHASE_QUOTATION_VIEW',
  23.             'ROLE_PURCHASE_QUOTATION_EDIT',
  24.             'ROLE_PURCHASE_QUOTATION_DELETE',
  25.             'ROLE_PURCHASE_QUOTATION_ENABLE',
  26.             'ROLE_PURCHASE_QUOTATION_DISABLE',
  27.             'ROLE_PURCHASE_QUOTATION_ARCHIVE',
  28.             'ROLE_PURCHASE_QUOTATION_NOT_ARCHIVE',
  29.             'ROLE_PURCHASE_QUOTATION_ATTACHMENT',
  30.             'ROLE_PURCHASE_QUOTATION_ORDER'
  31.             ])) {
  32.             return false;
  33.         }
  34.         // only vote on `Activity` objects
  35.         if (!$subject instanceof PurchaseRequest) {
  36.             return false;
  37.         }
  38.         return true;
  39.     }
  40.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  41.     {
  42.         $user $token->getUser();
  43.         // if the user is anonymous, do not grant access
  44.         if (!$user instanceof UserInterface) {
  45.             return false;
  46.         }
  47.         // ... (check conditions and return true to grant permission) ...
  48.         switch ($attribute) {
  49.             case 'ROLE_PURCHASE_QUOTATION_CREATE':
  50.                 return $this->canCreate();
  51.                 break;
  52.             case 'ROLE_PURCHASE_QUOTATION_VIEW':
  53.                 return $this->canView();
  54.                 break;
  55.             case 'ROLE_PURCHASE_QUOTATION_EDIT':
  56.                 return $this->canEdit($subject$user);
  57.                 break;
  58.             case 'ROLE_PURCHASE_QUOTATION_DELETE':
  59.                 return $this->canDelete($subject$user);
  60.                 break;
  61.             case 'ROLE_PURCHASE_QUOTATION_ENABLE':
  62.                 return $this->canEnable($subject$user);
  63.                 break;
  64.             case 'ROLE_PURCHASE_QUOTATION_DISABLE':
  65.                 return $this->canDisable($subject$user);
  66.                 break;
  67.             case 'ROLE_PURCHASE_QUOTATION_ARCHIVE':
  68.                 return $this->canArchive($subject$user);
  69.                 break;
  70.             case 'ROLE_PURCHASE_QUOTATION_NOT_ARCHIVE':
  71.                 return $this->canNotArchive($subject$user);
  72.                 break;
  73.             case 'ROLE_PURCHASE_QUOTATION_ATTACHMENT':
  74.                 return $this->canAttachFile($subject$user);
  75.                 break;
  76.             case 'ROLE_PURCHASE_QUOTATION_ORDER':
  77.                 return $this->canOrder($subject$user);
  78.                 break;
  79.         }
  80.         return false;
  81.     }
  82.     private function canCreate()
  83.     {
  84.         if (in_array('ROLE_PURCHASE_QUOTATION_CREATE'$this->session->get('privileges'))) {
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.     private function canView()
  90.     {
  91.         if (in_array('ROLE_PURCHASE_QUOTATION_VIEW'$this->session->get('privileges'))) {
  92.             return true;
  93.         }
  94.         return false;
  95.     }
  96.     private function canEdit(PurchaseRequest $purchaseQuotationUser $user)
  97.     {
  98.         if (in_array('ROLE_PURCHASE_QUOTATION_EDIT'$this->session->get('privileges'))) {
  99.             return true;
  100.         }
  101.         return false;
  102.     }
  103.     private function canDelete(PurchaseRequest $purchaseQuotationUser $user)
  104.     {
  105.         if (in_array('ROLE_PURCHASE_QUOTATION_DELETE'$this->session->get('privileges'))) {
  106.             return true;
  107.         }
  108.         return false;
  109.     }
  110.     private function canDisable(PurchaseRequest $purchaseQuotationUser $user)
  111.     {
  112.         if (in_array('ROLE_PURCHASE_QUOTATION_DISABLE'$this->session->get('privileges'))) {
  113.             return true;
  114.         }
  115.         return false;
  116.     }
  117.     private function canArchive(PurchaseRequest $purchaseQuotationUser $user)
  118.     {
  119.         if (in_array('ROLE_PURCHASE_QUOTATION_ARCHIVE'$this->session->get('privileges'))) {
  120.             return true;
  121.         }
  122.         return false;
  123.     }
  124.     private function canNotArchive(PurchaseRequest $purchaseQuotationUser $user)
  125.     {
  126.         if (in_array('ROLE_PURCHASE_QUOTATION_NOT_ARCHIVE'$this->session->get('privileges'))) {
  127.             return true;
  128.         }
  129.         return false;
  130.     }
  131.     private function canOrder(PurchaseRequest $purchaseQuotationUser $user)
  132.     {
  133.         if (in_array('ROLE_PURCHASE_QUOTATION_ORDER'$this->session->get('privileges'))) {
  134.             return true;
  135.         }
  136.         return false;
  137.     }
  138.     private function canEnable(PurchaseRequest $purchaseQuotationUser $user)
  139.     {
  140.         if (in_array('ROLE_PURCHASE_QUOTATION_ENABLE'$this->session->get('privileges'))) {
  141.             return true;
  142.         }
  143.         return false;
  144.     }
  145.     private function canAttachFile(PurchaseRequest $purchaseQuotationUser $user)
  146.     {
  147.         if (in_array('ROLE_PURCHASE_QUOTATION_ORDER'$this->session->get('privileges'))) {
  148.             return true;
  149.         }
  150.         return false;
  151.     }
  152. }