src/Security/Voter/MandatVoter.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\Affair;
  9. use App\Entity\JIT\Mandat;
  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 MandatVoter
  17.  *
  18.  * @author NOUTCHEU Blaise
  19.  */
  20. class MandatVoter extends Voter {
  21.     // these strings are just invented: you can use anything
  22.     const ADD 'ROLE_JIT_MANDAT_ADD';
  23.     const VIEW 'ROLE_JIT_MANDAT_VIEW';
  24.     const EDIT 'ROLE_JIT_MANDAT_EDIT';
  25.     const DELETE 'ROLE_JIT_MANDAT_DELETE';
  26.     private $security;
  27.     public function __construct(Security $security) {
  28.         $this->security $security;
  29.     }
  30.     protected function supports(string $attribute$subject) {
  31.         // only a teacher can add mandat on his subject
  32.         if (in_array($attribute, [
  33.                     self::ADD,
  34.                     self::VIEW,
  35.                     self::EDIT,
  36.                     self::DELETE,
  37.                 ])) {
  38.             return true;
  39.         }
  40.         if (!in_array($attribute, [
  41. //                    self::VIEW,
  42.                 ])) {
  43.             return false;
  44.         }
  45.         // only vote on `Mandat` objects
  46.         if (!$subject instanceof Mandat) {
  47.             return false;
  48.         }
  49.         return true;
  50.     }
  51.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token) {
  52.         $user $token->getUser();
  53.         if (!$user instanceof User) {
  54.             // the user must be logged in; if not, deny access
  55.             return false;
  56.         }
  57.         // ROLE_JIT_MANDAT_MANAGE can do anything on mandat! The power!
  58.         if ($this->security->isGranted('ROLE_MANAGER')) {
  59.             return true;
  60.         }
  61.         switch ($attribute) {
  62.             case self::ADD:
  63.                 return $this->canAdd($subject$user);
  64.             case self::VIEW:
  65.                 return $this->canView($subject$user);
  66.             case self::EDIT:
  67.                 return $this->canEdit($subject$user);
  68.             case self::DELETE:
  69.                 return $this->canDelete($subject$user);
  70.         }
  71.         throw new LogicException('This code should not be reached!');
  72.     }
  73.     private function canAdd(Affair $affairUser $user) {
  74.         return true;
  75.     }
  76.     private function canView(Mandat $mandatUser $user) {
  77.        
  78.         return true;
  79.     }
  80.     private function canClone(Mandat $mandatUser $user) {
  81.         if($mandat->getAffair() !== null){
  82.             return $this->canAdd($mandat->getAffair(),$user) && $this->canView($mandat$user);
  83.         }else{
  84.             return false;
  85.         }
  86.     }
  87.     
  88.     private function canEdit(Mandat $mandatUser $user) {
  89.         //Uniquement le chef d'agence peut modifier une mandatne
  90. //        if ($user->getMyAgencies()->contains($mandat->getAgency())) {
  91. //            return true;
  92. //        }
  93.         return $this->canView($mandat$user);
  94.     }
  95.     private function canDelete(Mandat $mandatUser $user) {
  96.         //Uniquement le chef d'agence peut modifier une mandatne
  97. //        if ($user->getMyAgencies()->contains($mandat->getAgency())) {
  98. //            return true;
  99. //        }
  100.         return $this->canView($mandat$user);
  101.     }
  102. }