src/Security/Voter/AccountVoter.php line 23

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\Account;
  9. use App\Entity\Security\User;
  10. use LogicException;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Security;
  14. /**
  15.  * Description of AccountVoter
  16.  *
  17.  * @author NOUTCHEU Blaise
  18.  */
  19. class AccountVoter extends Voter {
  20.     // these strings are just invented: you can use anything
  21.     const LIST = 'ROLE_JIT_ACCOUNT_INDEX';
  22.     const ADD 'ROLE_JIT_ACCOUNT_ADD';
  23.     const VIEW 'ROLE_JIT_ACCOUNT_VIEW';
  24.     const CLONE = 'ROLE_JIT_ACCOUNT_CLONE';
  25.     const EDIT 'ROLE_JIT_ACCOUNT_EDIT';
  26.     const DELETE 'ROLE_JIT_ACCOUNT_DELETE';
  27.     const DEBIT 'ROLE_JIT_ACCOUNT_DEBIT';
  28.     const CREDIT 'ROLE_JIT_ACCOUNT_CREDIT';
  29.     const LISTT 'ROLE_JIT_TRANSACTION_INDEX';
  30.     const VIEWT 'ROLE_JIT_TRANSACTION_VIEW';
  31.     const VIEWM 'ROLE_JIT_MOVEMENT_VIEW';
  32.     private $security;
  33.     public function __construct(Security $security) {
  34.         $this->security $security;
  35.     }
  36.     protected function supports(string $attribute$subject) {
  37.         // only a teacher can add account on his subject
  38.         if (in_array($attribute, [
  39.                     self::LIST,
  40.                     self::ADD,
  41.                     self::VIEW,
  42.                     self::CLONE,
  43.                     self::EDIT,
  44.                     self::DELETE,
  45.                     self::DEBIT,
  46.                     self::CREDIT,
  47.                     self::LISTT,
  48.                     self::VIEWT,
  49.                     self::VIEWM,
  50.                 ])) {
  51.             return true;
  52.         }
  53.         if (!in_array($attribute, [
  54. //                    self::VIEW,
  55.                 ])) {
  56.             return false;
  57.         }
  58.         // only vote on `Account` objects
  59.         if (!$subject instanceof Account) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  65.         $user $token->getUser();
  66.         if (!$user instanceof User) {
  67.             // the user must be logged in; if not, deny access
  68.             return false;
  69.         }
  70.         // ROLE_JIT_ACCOUNT_MANAGE can do anything on account! The power!
  71.         if ($this->security->isGranted('ROLE_MANAGER')) {
  72.             return true;
  73.         }
  74.         switch ($attribute) {
  75.             case self::ADD:
  76.                 return $this->canAdd($user);
  77.             case self::LIST:
  78.                 return true;
  79.             case self::VIEW:
  80.                 return $this->canView($subject$user);
  81.             case self::CLONE:
  82.                 return $this->canClone($subject$user);
  83.             case self::EDIT:
  84.                 return $this->canEdit($subject$user);
  85.             case self::DELETE:
  86.                 return $this->canDelete($subject$user);
  87.             case self::DEBIT:
  88.                 return $this->canDebit($subject$user);
  89.             case self::CREDIT:
  90.                 return $this->canCredit($subject$user);
  91.             case self::LISTT:
  92.                 return $this->canListT($user);
  93.             case self::VIEWT:
  94.                 return $this->canViewT($subject$user);
  95.             case self::VIEWM:
  96.                 return $this->canViewM($subject$user);
  97.         }
  98.         throw new LogicException('This code should not be reached!');
  99.     }
  100.     private function canAdd(User $user): bool {
  101.         return true;
  102.     }
  103.     private function canView(Account $accountUser $user) {
  104.         //Vérifier si c'est lié à une accountne (ie account null)
  105. //        if($account == null){
  106. //            return false;
  107. //        }
  108.         // vérifier que c'est une account de l'utilisateur connecté
  109.         if ($user->getAccounts()->contains($account)) {
  110.             return true;
  111.         }
  112.         // vérifier que c'est un employé de l'utilisateur connecté
  113.         foreach ($user->getMyEmployees() as $person){
  114.             if ($person->getAccounts()->contains($account)) {
  115.                 return true;
  116.             }
  117.         }
  118. //        // vérifier que c'est un employé de l'utilisateur connecté
  119. //        if ($user->getMyEmployees()->contains($account)) {
  120. //            return true;
  121. //        }
  122. //
  123. //        // vérifier que c'est un propriétaire de l'utilisateur connecté
  124. //        if ($user->getMyAOwners()->contains($account)) {
  125. //            return true;
  126. //        }
  127.         return false;
  128.     }
  129.     private function canClone(Account $accountUser $user) {
  130.         return $this->canAdd($user) && $this->canView($account$user);
  131.     }
  132.     private function canEdit(Account $accountUser $user) {
  133.         //Uniquement le chef d'agence peut modifier une accountne
  134. //        if ($user->getMyAgencies()->contains($account->getAgency())) {
  135. //            return true;
  136. //        }
  137.         return false;
  138.     }
  139.     private function canDelete(Account $accountUser $user) {
  140.         //Uniquement le chef d'agence peut modifier une accountne
  141. //        if ($user->getMyAgencies()->contains($account->getAgency())) {
  142. //            return true;
  143. //        }
  144.         return false;
  145.     }
  146.     private function canDebit(Account $accountUser $user) {
  147.         // Tout le monde peut débiter ses comptes
  148.         if ($user->getAccounts()->contains($account) && $account->getBalance() >= Account::MIN_AMT) {
  149.             return true;
  150.         }
  151.         return false;
  152.     }
  153.     private function canCredit(Account $accountUser $user) {
  154.         return true;
  155.     }
  156.     private function canListT(User $user) {
  157.         return true;
  158.     }
  159.     private function canViewT(\App\Entity\JIT\Transaction $transactionUser $user) {
  160. //         if ($this->security->isGranted('ROLE_COMPTABLE_ADM')) {
  161. //            return true;
  162. //        }
  163.         
  164.         foreach ($transaction->getMovements() as $movement) {
  165.             if ($this->canViewM($movement$user)) {
  166.                 return true;
  167.             }
  168.         }
  169.         return false;
  170.     }
  171.     private function canViewM(\App\Entity\JIT\Movement $movementUser $user) {
  172. //         if ($this->security->isGranted('ROLE_COMPTABLE_ADM')) {
  173. //            return true;
  174. //        }
  175.         
  176.         if ($movement->getAccount() != null) {
  177.             return $this->canView($movement->getAccount(), $user);
  178.         }
  179.         return false;
  180.     }
  181. }