src/Security/Voter/InvoiceVoter.php line 26

Open in your IDE?
  1. <?php
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. namespace App\Security\Voter;
  8. use App\Entity\JIT\Charge;
  9. use App\Entity\JIT\Incident;
  10. use App\Entity\JIT\Invoice;
  11. use App\Entity\JIT\Tenancy;
  12. use App\Entity\Security\User;
  13. use LogicException;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  16. use Symfony\Component\Security\Core\Security;
  17. /**
  18.  * Description of InvoiceVoter
  19.  *
  20.  * @author NOUTCHEU Blaise
  21.  */
  22. class InvoiceVoter extends Voter {
  23.     // these strings are just invented: you can use anything
  24. //    const LIST = 'ROLE_JIT_INVOICE_INDEX';
  25.     const LIST = 'ROLE_JIT_INVOICE_INDEX';
  26.     const ALL 'ROLE_JIT_INVOICE_ALL';
  27.     const LUNPAY 'ROLE_JIT_INVOICE_LUNPAY';
  28.     const LVALIDATE 'ROLE_JIT_INVOICE_LVALIDATE';
  29.     const LPAY 'ROLE_JIT_INVOICE_LPAY';
  30.     const ADD 'ROLE_JIT_INVOICE_ADD';
  31.     const ADDBI 'ROLE_JIT_INVOICE_ADDBI';
  32.     const VIEW 'ROLE_JIT_INVOICE_VIEW';
  33.     const CLONE = 'ROLE_JIT_INVOICE_CLONE';
  34.     const EDIT 'ROLE_JIT_INVOICE_EDIT';
  35.     const DELETE 'ROLE_JIT_INVOICE_DELETE';
  36.     const PAY 'ROLE_JIT_INVOICE_PAY';
  37.     const VALIDATE 'ROLE_JIT_INVOICE_VALIDATE';
  38.     const GENERATE 'ROLE_JIT_INVOICE_GENERATE';
  39.     const GENERATE2 'ROLE_JIT_INVOICE_GENERATE2';
  40.     private $security;
  41.     public function __construct(Security $security) {
  42.         $this->security $security;
  43.     }
  44.     protected function supports(string $attribute$subject) {
  45.         // only a teacher can add invoice on his subject
  46.         if (in_array($attribute, [
  47.                     self::LIST,
  48.                     self::ALL,
  49.                     self::LUNPAY,
  50.                     self::LVALIDATE,
  51.                     self::LPAY,
  52.                     self::ADD,
  53.                     self::ADDBI,
  54.                     self::VIEW,
  55.                     self::CLONE,
  56.                     self::EDIT,
  57.                     self::PAY,
  58.                     self::VALIDATE,
  59.                     self::GENERATE,
  60.                     self::GENERATE2,
  61.                     self::DELETE,
  62.                 ])) {
  63.             return true;
  64.         }
  65.         if (!in_array($attribute, [
  66. //                    self::VIEW,
  67.                 ])) {
  68.             return false;
  69.         }
  70.         // only vote on `Invoice` objects
  71.         if (!$subject instanceof Invoice) {
  72.             return false;
  73.         }
  74.         return true;
  75.     }
  76.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  77.         $user $token->getUser();
  78.         if (!$user instanceof User) {
  79.             // the user must be logged in; if not, deny access
  80.             return false;
  81.         }
  82.         // ROLE_JIT_INVOICE_MANAGE can do anything on invoice! The power!
  83.         if ($this->security->isGranted('ROLE_MANAGER')) {
  84.             return true;
  85.         }
  86.         switch ($attribute) {
  87.             case self::ALL:
  88.             case self::LIST:
  89.             case self::LUNPAY:
  90.                 return true;
  91.             case self::LVALIDATE:
  92.             case self::LPAY:
  93.                 return $this->canList($user);
  94.             case self::ADD:
  95.                 return $this->canAdd($user);
  96.             case self::ADDBI:
  97.                 return $this->canAddBI($subject$user);
  98.             case self::VIEW:
  99.                 return $this->canView($subject$user);
  100.             case self::CLONE:
  101.                 return $this->canClone($subject$user);
  102.             case self::EDIT:
  103.                 return $this->canEdit($subject$user);
  104.             case self::DELETE:
  105.                 return $this->canDelete($subject$user);
  106.             case self::PAY:
  107.                 return $this->canPay($subject$user);
  108.             case self::VALIDATE:
  109.                 return $this->canValidate($subject$user);
  110.             case self::GENERATE:
  111.                 return $this->canGenerate($subject$user);
  112.             case self::GENERATE2:
  113.                 return $this->canGenerate2($subject$user);
  114.         }
  115.         throw new LogicException('This code should not be reached!');
  116.     }
  117.     private function canList(User $user) {
  118.         return true;
  119.     }
  120.     private function canAdd(User $user) {
  121.         return true;
  122.     }
  123.     private function canAddBI(Incident $incidentUser $user) {
  124.         return true;
  125.     }
  126.     private function canView(Invoice $invoiceUser $user) {
  127.         return true;
  128.     }
  129.     private function canClone(Invoice $invoiceUser $user) {
  130.         return $this->canAdd($user) && $this->canView($invoice$user);
  131.     }
  132.     private function canEdit(Invoice $invoiceUser $user) {
  133.         //Uniquement le chef d'agence peut modifier une invoicene
  134. //        if ($user->getMyAgencies()->contains($invoice->getInvoice())) {
  135. //            return true;
  136. //        }
  137.         return $this->canView($invoice$user);
  138.     }
  139.     private function canDelete(Invoice $invoiceUser $user) {
  140.         //Uniquement le chef d'agence peut modifier une invoicene
  141. //        if ($user->getMyAgencies()->contains($invoice->getInvoice())) {
  142. //            return true;
  143. //        }
  144.         return $this->canView($invoice$user);
  145.     }
  146.     private function canPay(Invoice $invoiceUser $user) {
  147.         return $this->canView($invoice$user);
  148.     }
  149.     private function canValidate(Invoice $invoiceUser $user) {
  150.         return $this->canView($invoice$user);
  151.     }
  152.     private function canGenerate(Tenancy $tenancyUser $user) {
  153. //        return $this->canView($tenancy, $user);
  154.         return true;
  155.     }
  156.     private function canGenerate2(Charge $chargeUser $user) {
  157. //        return $this->canView($invoice, $user);
  158.         return true;
  159.     }
  160. }