src/Security/Voter/PChargeVoter.php line 24

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\Property;
  10. use App\Entity\Security\User;
  11. use LogicException;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. use Symfony\Component\Security\Core\Security;
  15. /**
  16.  * Description of ChargeVoter
  17.  *
  18.  * @author NOUTCHEU Blaise
  19.  */
  20. class PChargeVoter extends Voter {
  21.     // these strings are just invented: you can use anything
  22. //    const LIST = 'ROLE_JIT_CHARGE_INDEX';
  23.     const ADD 'ROLE_JIT_CHARGE_ADD';
  24.     const VIEW 'ROLE_JIT_CHARGE_VIEW';
  25.     const EDIT 'ROLE_JIT_CHARGE_EDIT';
  26.     const DELETE 'ROLE_JIT_CHARGE_DELETE';
  27.     private $security;
  28.     public function __construct(Security $security) {
  29.         $this->security $security;
  30.     }
  31.     protected function supports(string $attribute$subject) {
  32.         // only a teacher can add charge on his subject
  33.         if (in_array($attribute, [
  34.                     self::ADD,
  35.                     self::VIEW,
  36.                     self::EDIT,
  37.                     self::DELETE,
  38.                 ])) {
  39.             return true;
  40.         }
  41.         if (!in_array($attribute, [
  42. //                    self::VIEW,
  43.                 ])) {
  44.             return false;
  45.         }
  46.         // only vote on `Charge` objects
  47.         if (!$subject instanceof Charge) {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  53.         $user $token->getUser();
  54.         if (!$user instanceof User) {
  55.             // the user must be logged in; if not, deny access
  56.             return false;
  57.         }
  58.         // ROLE_JIT_CHARGE_MANAGE can do anything on charge! The power!
  59.         if ($this->security->isGranted('ROLE_MANAGER')) {
  60.             return true;
  61.         }
  62.         switch ($attribute) {
  63.             case self::ADD:
  64.                 return $this->canAdd($subject$user);
  65.             case self::VIEW:
  66.                 return $this->canView($subject$user);
  67.             case self::EDIT:
  68.                 return $this->canEdit($subject$user);
  69.             case self::DELETE:
  70.                 return $this->canDelete($subject$user);
  71.         }
  72.         throw new LogicException('This code should not be reached!');
  73.     }
  74.     private function canAdd(Property $propertyUser $user) {
  75.         return true;
  76.     }
  77.     private function canView(Charge $chargeUser $user) {
  78.        
  79.         return true;
  80.     }
  81.     
  82.     private function canEdit(Charge $chargeUser $user) {
  83.         //Uniquement le chef d'agence peut modifier une chargene
  84. //        if ($user->getMyAgencies()->contains($charge->getAgency())) {
  85. //            return true;
  86. //        }
  87.         return $this->canView($charge$user);
  88.     }
  89.     private function canDelete(Charge $chargeUser $user) {
  90.         //Uniquement le chef d'agence peut modifier une chargene
  91. //        if ($user->getMyAgencies()->contains($charge->getAgency())) {
  92. //            return true;
  93. //        }
  94.         return $this->canView($charge$user);
  95.     }
  96. }