src/Security/Voter/ProspectOnStoreVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\ProspectOnStore;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class ProspectOnStoreVoter extends AbstractVoter
  9. {
  10.     const VIEW 'view';
  11.     const EDIT 'edit';
  12.     const DELETE 'delete';
  13.     private $security;
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.     }
  18.     protected function supports($attribute$subject)
  19.     {
  20.         if (is_null($subject) || !$subject instanceof ProspectOnStore) {
  21.             return false;
  22.         }
  23.         $attribute $this->getActionFromAttribut($attribute);
  24.         // if the attribute isn't one we support, return false
  25.         if (!in_array($attribute, array(self::VIEWself::EDITself::DELETE))) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  31.     {
  32.         $attribute $this->getActionFromAttribut($attribute);
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             return false;
  36.         }
  37.         if ($this->security->isGranted('ROLE_ADMIN')) {
  38.             return true;
  39.         }
  40.         switch ($attribute) {
  41.             case self::VIEW:
  42.                 return $this->canView($subject$user);
  43.             case self::EDIT:
  44.                 return $this->canEdit($subject$user);
  45.             case self::DELETE:
  46.                 return $this->canDelete($subject$user);
  47.         }
  48.         throw new \LogicException('This code should not be reached!');
  49.     }
  50.     /**
  51.      * l'utilisateur peut voir le prospectOnStore s'il lui est associé
  52.      */
  53.     private function canView(ProspectOnStore $prospectOnStoreUser $user)
  54.     {
  55.         $store $prospectOnStore->getStore();
  56.         //a freelancer can see all the POS attached to one of his stores
  57.         if($user->isTypeFreelancer()){
  58.             if( $user->getAdministrator() == $store->getAdministrator() ){
  59.                 return true;
  60.             }
  61.         }elseif ($user->isTypeFreelancerAssistant()){
  62.             //an assistant can see a POS if it s attached to a store he's affected to
  63.             if( $user->getFreelancerAssistant()->getStores()->contains($store)){
  64.                 return true;
  65.             }
  66.         }
  67.         return false;
  68.     }
  69.     /**
  70.      * Même règle que pour voir
  71.      */
  72.     private function canEdit(ProspectOnStore $prospectOnStoreUser $user)
  73.     {
  74.         return $this->canView($prospectOnStore$user);
  75.     }
  76.     /**
  77.      *  Même règle que pour voir
  78.      */
  79.     private function canDelete(ProspectOnStore $prospectOnStoreUser $user)
  80.     {
  81.         return $this->canView($prospectOnStore$user);
  82.     }
  83. }