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

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Purchase;
  3. use App\Entity\Purchase\PurchaseReturnInvoice;
  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 PurchaseReturnInvoiceVoter 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_RETURN_INVOICE_CREATE',
  22.             'ROLE_PURCHASE_RETURN_INVOICE_VIEW',
  23.             'ROLE_PURCHASE_RETURN_INVOICE_EDIT',
  24.             'ROLE_PURCHASE_RETURN_INVOICE_DELETE',
  25.             'ROLE_PURCHASE_RETURN_INVOICE_ENABLE',
  26.             'ROLE_PURCHASE_RETURN_INVOICE_DISABLE',
  27.             'ROLE_PURCHASE_RETURN_INVOICE_ARCHIVE',
  28.             'ROLE_PURCHASE_RETURN_INVOICE_NOT_ARCHIVE',
  29.             'ROLE_PURCHASE_RETURN_INVOICE_SETTLEMENT',
  30.             'ROLE_PURCHASE_RETURN_INVOICE_VALIDATE',
  31.             'ROLE_PURCHASE_RETURN_INVOICE_REJECT',
  32.             'ROLE_PURCHASE_RETURN_INVOICE_REVERSE'])) {
  33.             return false;
  34.         }
  35.         // only vote on `Activity` objects
  36.         if (!$subject instanceof PurchaseReturnInvoice) {
  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_RETURN_INVOICE_CREATE':
  51.                 return $this->canCreate();
  52.                 break;
  53.             case 'ROLE_PURCHASE_RETURN_INVOICE_VIEW':
  54.                 return $this->canView();
  55.                 break;
  56.             case 'ROLE_PURCHASE_RETURN_INVOICE_EDIT':
  57.                 return $this->canEdit($subject$user);
  58.                 break;
  59.             case 'ROLE_PURCHASE_RETURN_INVOICE_DELETE':
  60.                 return $this->canDelete($subject$user);
  61.                 break;
  62.             case 'ROLE_PURCHASE_RETURN_INVOICE_ENABLE':
  63.                 return $this->canEnable($subject$user);
  64.                 break;
  65.             case 'ROLE_PURCHASE_RETURN_INVOICE_DISABLE':
  66.                 return $this->canDisable($subject$user);
  67.                 break;
  68.             case 'ROLE_PURCHASE_RETURN_INVOICE_ARCHIVE':
  69.                 return $this->canArchive($subject$user);
  70.                 break;
  71.             case 'ROLE_PURCHASE_RETURN_INVOICE_NOT_ARCHIVE':
  72.                 return $this->canNotArchive($subject$user);
  73.                 break;
  74.             case 'ROLE_PURCHASE_RETURN_INVOICE_VALIDATE':
  75.                 return $this->canValidate($subject$user);
  76.                 break;
  77.             case 'ROLE_PURCHASE_RETURN_INVOICE_REJECT':
  78.                 return $this->canReject($subject$user);
  79.                 break;
  80.             case 'ROLE_PURCHASE_RETURN_INVOICE_REVERSE':
  81.                 return $this->canReverse($subject$user);
  82.                 break;
  83.             case 'ROLE_PURCHASE_RETURN_INVOICE_SETTLEMENT':
  84.                 return $this->canSettle($subject$user);
  85.                 break;
  86.         }
  87.         return false;
  88.     }
  89.     private function canCreate()
  90.     {
  91.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_CREATE'$this->session->get('privileges'))) {
  92.             return true;
  93.         }
  94.         return false;
  95.     }
  96.     private function canView()
  97.     {
  98.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_VIEW'$this->session->get('privileges'))) {
  99.             return true;
  100.         }
  101.         return false;
  102.     }
  103.     private function canEdit(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  104.     {
  105.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_EDIT'$this->session->get('privileges'))) {
  106.             return true;
  107.         }
  108.         return false;
  109.     }
  110.     private function canDelete(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  111.     {
  112.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_DELETE'$this->session->get('privileges'))) {
  113.             return true;
  114.         }
  115.         return false;
  116.     }
  117.     private function canEnable(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  118.     {
  119.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_ENABLE'$this->session->get('privileges'))) {
  120.             return true;
  121.         }
  122.         return false;
  123.     }
  124.     private function canDisable(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  125.     {
  126.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_DISABLE'$this->session->get('privileges'))) {
  127.             return true;
  128.         }
  129.         return false;
  130.     }
  131.     private function canArchive(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  132.     {
  133.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_ARCHIVE'$this->session->get('privileges'))) {
  134.             return true;
  135.         }
  136.         return false;
  137.     }
  138.     private function canNotArchive(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  139.     {
  140.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_NOT_ARCHIVE'$this->session->get('privileges'))) {
  141.             return true;
  142.         }
  143.         return false;
  144.     }
  145.     private function canValidate(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  146.     {
  147.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_VALIDATE'$this->session->get('privileges'))) {
  148.             return true;
  149.         }
  150.         return false;
  151.     }
  152.     private function canReject(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  153.     {
  154.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_REJECT'$this->session->get('privileges'))) {
  155.             return true;
  156.         }
  157.         return false;
  158.     }
  159.     private function canReverse(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  160.     {
  161.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_REVERSE'$this->session->get('privileges'))) {
  162.             return true;
  163.         }
  164.         return false;
  165.     }
  166.     private function canSettle(PurchaseReturnInvoice $purchaseReturnInvoiceUser $user)
  167.     {
  168.         if (in_array('ROLE_PURCHASE_RETURN_INVOICE_SETTLEMENT'$this->session->get('privileges'))) {
  169.             return true;
  170.         }
  171.         return false;
  172.     }
  173. }