src/V4/Voters/ProspectVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\V4\Voters;
  3. use App\Security\User;
  4. use LogicException;
  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 ProspectVoter extends Voter
  9. {
  10.     const PROSPECT_MANAGE_LIST 'prospect_manage_list';
  11.     const PROSPECT_SHOW_LIST 'prospect_show_list';
  12.     const PROSPECT_ADD_EDIT 'prospect_add_edit';
  13.     const PROSPECT_MANAGE_FIELDS 'prospect_manage_fields';
  14.     const PROSPECT_STATE_ADD_EDIT 'prospect_state_add_edit';
  15.     /**
  16.      * @var Security
  17.      */
  18.     private $security;
  19.     public function __construct(Security $security)
  20.     {
  21.         $this->security $security;
  22.     }
  23.     /**
  24.      * @param $attribute
  25.      * @param $subject
  26.      *
  27.      * @return bool
  28.      */
  29.     protected function supports($attribute$subject): bool
  30.     {
  31.         return in_array($attribute, [
  32.             self::PROSPECT_MANAGE_LIST,
  33.             self::PROSPECT_SHOW_LIST,
  34.             self::PROSPECT_ADD_EDIT,
  35.             self::PROSPECT_MANAGE_FIELDS,
  36.             self::PROSPECT_STATE_ADD_EDIT,
  37.         ], true);
  38.     }
  39.     /**
  40.      * @param $attribute
  41.      * @param $subject
  42.      * @param TokenInterface $token
  43.      *
  44.      * @return bool
  45.      *
  46.      * @throws LogicException
  47.      */
  48.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  49.     {
  50.         $user $token->getUser();
  51.         if (!$user instanceof User) {
  52.             return false;
  53.         }
  54.         switch ($attribute) {
  55.             case self::PROSPECT_MANAGE_LIST:
  56.                 return $this->canProspectManageList();
  57.             case self::PROSPECT_SHOW_LIST:
  58.                 return $this->canProspectShowList();
  59.             case self::PROSPECT_ADD_EDIT:
  60.                 return $this->canProspectAddEdit();
  61.             case self::PROSPECT_MANAGE_FIELDS:
  62.                 return $this->canProspectManageFields();
  63.             case self::PROSPECT_STATE_ADD_EDIT:
  64.                 return $this->canProspectStateAddEdit();
  65.         }
  66.         throw new LogicException('This should never happen');
  67.     }
  68.     private function canProspectManageList(): bool
  69.     {
  70.         return $this->canAccessProspect();
  71.     }
  72.     private function canProspectShowList(): bool
  73.     {
  74.         return $this->canAccessProspect();
  75.     }
  76.     private function canProspectAddEdit(): bool
  77.     {
  78.         return $this->canAccessProspect();
  79.     }
  80.     private function canProspectManageFields(): bool
  81.     {
  82.         return $this->canAccessProspect();
  83.     }
  84.     private function canProspectStateAddEdit(): bool
  85.     {
  86.         return $this->canAccessProspect();
  87.     }
  88.     /**
  89.      * @TODO: Si le client n'a pas le droit "prospect show", il peut tout de même voir les contacts qu'il gère.
  90.      *
  91.      * @return bool
  92.      */
  93.     private function canAccessProspect(): bool
  94.     {
  95.         return true;
  96.     }
  97. }