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

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\Purchase;
  3. use App\Entity\Purchase\PurchaseSettlement;
  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 PurchaseSettlementVoter 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_SETTLEMENT_CREATE',
  22.             'ROLE_PURCHASE_SETTLEMENT_EDIT',
  23.             'ROLE_PURCHASE_SETTLEMENT_DELETE'])) {
  24.             return false;
  25.         }
  26.         // only vote on `Activity` objects
  27.         if (!$subject instanceof PurchaseSettlement) {
  28.             return false;
  29.         }
  30.         return true;
  31.     }
  32.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  33.     {
  34.         $user $token->getUser();
  35.         // if the user is anonymous, do not grant access
  36.         if (!$user instanceof UserInterface) {
  37.             return false;
  38.         }
  39.         // ... (check conditions and return true to grant permission) ...
  40.         switch ($attribute) {
  41.             case 'ROLE_PURCHASE_SETTLEMENT_CREATE':
  42.                 return $this->canCreate();
  43.                 break;
  44.             case 'ROLE_PURCHASE_SETTLEMENT_EDIT':
  45.                 return $this->canEdit($subject$user);
  46.                 break;
  47.             case 'ROLE_PURCHASE_SETTLEMENT_DELETE':
  48.                 return $this->canDelete($subject$user);
  49.                 break;
  50.         }
  51.         return false;
  52.     }
  53.     private function canCreate()
  54.     {
  55.         if (in_array('ROLE_PURCHASE_SETTLEMENT_CREATE'$this->session->get('privileges'))) {
  56.             return true;
  57.         }
  58.         return false;
  59.     }
  60.     private function canEdit(PurchaseSettlement $purchaseSettlementUser $user)
  61.     {
  62.         if (in_array('ROLE_PURCHASE_SETTLEMENT_EDIT'$this->session->get('privileges'))) {
  63.             return true;
  64.         }
  65.         return false;
  66.     }
  67.     private function canDelete(PurchaseSettlement $purchaseSettlementUser $user)
  68.     {
  69.         if (in_array('ROLE_PURCHASE_SETTLEMENT_DELETE'$this->session->get('privileges'))) {
  70.             return true;
  71.         }
  72.         return false;
  73.     }
  74. }