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

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