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

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Purchase;
  3. use App\Entity\Purchase\PurchaseInvoice;
  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 PurchaseInvoiceVoter 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_INVOICE_CREATE',
  22.             'ROLE_PURCHASE_INVOICE_VIEW',
  23.             'ROLE_PURCHASE_INVOICE_EDIT',
  24.             'ROLE_PURCHASE_INVOICE_DELETE',
  25.             'ROLE_PURCHASE_INVOICE_ENABLE',
  26.             'ROLE_PURCHASE_INVOICE_DISABLE',
  27.             'ROLE_PURCHASE_INVOICE_ARCHIVE',
  28.             'ROLE_PURCHASE_INVOICE_NOT_ARCHIVE',
  29.             
  30.             'ROLE_PURCHASE_INVOICE_ATTACHMENT',
  31.             'ROLE_PURCHASE_INVOICE_SETTLEMENT'
  32.             ])) {
  33.             return false;
  34.         }
  35.         // only vote on `Activity` objects
  36.         if (!$subject instanceof PurchaseInvoice) {
  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_INVOICE_CREATE':
  51.                 return $this->canCreate();
  52.                 break;
  53.             case 'ROLE_PURCHASE_INVOICE_VIEW':
  54.                 return $this->canView();
  55.                 break;
  56.             case 'ROLE_PURCHASE_INVOICE_EDIT':
  57.                 return $this->canEdit($subject$user);
  58.                 break;
  59.             case 'ROLE_PURCHASE_INVOICE_DELETE':
  60.                 return $this->canDelete($subject$user);
  61.                 break;
  62.             case 'ROLE_PURCHASE_INVOICE_ENABLE':
  63.                 return $this->canEnable($subject$user);
  64.                 break;
  65.             case 'ROLE_PURCHASE_INVOICE_DISABLE':
  66.                 return $this->canDisable($subject$user);
  67.                 break;
  68.             case 'ROLE_PURCHASE_INVOICE_ARCHIVE':
  69.                 return $this->canArchive($subject$user);
  70.                 break;
  71.             case 'ROLE_PURCHASE_INVOICE_NOT_ARCHIVE':
  72.                 return $this->canNotArchive($subject$user);
  73.                 break;
  74.             case 'ROLE_PURCHASE_INVOICE_ATTACHMENT':
  75.                 return $this->canUpload($subject$user);
  76.                 break;
  77.             case 'ROLE_PURCHASE_INVOICE_SETTLEMENT':
  78.                 return $this->canSettlement($subject$user);
  79.                 break;
  80.         }
  81.         return false;
  82.     }
  83.     private function canCreate()
  84.     {
  85.         if (in_array('ROLE_PURCHASE_INVOICE_CREATE'$this->session->get('privileges'))) {
  86.             return true;
  87.         }
  88.         return false;
  89.     }
  90.     private function canView()
  91.     {
  92.         if (in_array('ROLE_PURCHASE_INVOICE_VIEW'$this->session->get('privileges'))) {
  93.             return true;
  94.         }
  95.         return false;
  96.     }
  97.     private function canEdit(PurchaseInvoice $purchaseInvoiceUser $user)
  98.     {
  99.         if (in_array('ROLE_PURCHASE_INVOICE_EDIT'$this->session->get('privileges'))) {
  100.             return true;
  101.         }
  102.         return false;
  103.     }
  104.     private function canDelete(PurchaseInvoice $purchaseInvoiceUser $user)
  105.     {
  106.         if (in_array('ROLE_PURCHASE_INVOICE_DELETE'$this->session->get('privileges'))) {
  107.             return true;
  108.         }
  109.         return false;
  110.     }
  111.     private function canUpload(PurchaseInvoice $purchaseInvoiceUser $user)
  112.     {
  113.         if (in_array('ROLE_PURCHASE_INVOICE_ATTACHMENT'$this->session->get('privileges'))) {
  114.             return true;
  115.         }
  116.         return false;
  117.     }
  118.     private function canDisable(PurchaseInvoice $purchaseInvoiceUser $user)
  119.     {
  120.         if (in_array('ROLE_PURCHASE_INVOICE_DISABLE'$this->session->get('privileges'))) {
  121.             return true;
  122.         }
  123.         return false;
  124.     }
  125.     
  126.     private function canArchive(PurchaseInvoice $purchaseInvoiceUser $user)
  127.     {
  128.         if (in_array('ROLE_PURCHASE_INVOICE_ARCHIVE'$this->session->get('privileges'))) {
  129.             return true;
  130.         }
  131.         return false;
  132.     }
  133.     private function canNotArchive(PurchaseInvoice $purchaseInvoiceUser $user)
  134.     {
  135.         if (in_array('ROLE_PURCHASE_INVOICE_NOT_ARCHIVE'$this->session->get('privileges'))) {
  136.             return true;
  137.         }
  138.         return false;
  139.     }
  140.     private function canEnable(PurchaseInvoice $purchaseInvoiceUser $user)
  141.     {
  142.         if (in_array('ROLE_PURCHASE_INVOICE_ENABLE'$this->session->get('privileges'))) {
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.     private function canSettlement(PurchaseInvoice $purchaseInvoiceUser $user)
  148.     {
  149.         if (in_array('ROLE_PURCHASE_INVOICE_SETTLEMENT'$this->session->get('privileges'))) {
  150.             return true;
  151.         }
  152.         return false;
  153.     }
  154. }