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