src/V4/Voters/ContactVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\V4\Voters;
  3. use App\Security\User;
  4. use App\V4\Model\Contact\Contact;
  5. use App\V4\Model\Security\UserInfo;
  6. use LogicException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class ContactVoter extends Voter
  11. {
  12.     const CONTACT_MANAGE_LIST 'contact_manage_list';
  13.     const CONTACT_SHOW_LIST 'contact_show_list';
  14.     const CONTACT_ADD_EDIT 'contact_add_edit';
  15.     const CONTACT_MANAGE_FIELDS 'contact_manage_fields';
  16.     const CONTACT_STATE_ADD_EDIT 'contact_state_add_edit';
  17.     const CONTACT_MOVE 'contact_move';
  18.     /**
  19.      * @var Security
  20.      */
  21.     private $security;
  22.     /**
  23.      * @param Security $security
  24.      */
  25.     public function __construct(Security $security)
  26.     {
  27.         $this->security $security;
  28.     }
  29.     /**
  30.      * @param $attribute
  31.      * @param $subject
  32.      *
  33.      * @return bool
  34.      */
  35.     protected function supports($attribute$subject): bool
  36.     {
  37.         if ($subject instanceof Contact && in_array($attribute, [
  38.                 self::CONTACT_MOVE,
  39.             ], true)) {
  40.             return true;
  41.         }
  42.         return in_array($attribute, [
  43.             self::CONTACT_MANAGE_LIST,
  44.             self::CONTACT_SHOW_LIST,
  45.             self::CONTACT_ADD_EDIT,
  46.             self::CONTACT_MANAGE_FIELDS,
  47.             self::CONTACT_STATE_ADD_EDIT,
  48.         ], true);
  49.     }
  50.     /**
  51.      * @param $attribute
  52.      * @param $subject
  53.      * @param TokenInterface $token
  54.      *
  55.      * @return bool
  56.      *
  57.      * @throws LogicException
  58.      */
  59.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  60.     {
  61.         $user $token->getUser();
  62.         if (!$user instanceof User) {
  63.             return false;
  64.         }
  65.         switch ($attribute) {
  66.             case self::CONTACT_MANAGE_LIST:
  67.                 return $this->canContactManageList();
  68.             case self::CONTACT_SHOW_LIST:
  69.                 return $this->canContactShowList();
  70.             case self::CONTACT_ADD_EDIT:
  71.                 return $this->canContactAddEdit();
  72.             case self::CONTACT_MANAGE_FIELDS:
  73.                 return $this->canContactManageFields();
  74.             case self::CONTACT_STATE_ADD_EDIT:
  75.                 return $this->canContactStateAddEdit();
  76.             case self::CONTACT_MOVE:
  77.                 return $this->canContactMove($subject$user);
  78.         }
  79.         throw new LogicException('This should never happen');
  80.     }
  81.     /**
  82.      * @return bool
  83.      */
  84.     private function canContactManageList(): bool
  85.     {
  86.         return $this->canAccessContact();
  87.     }
  88.     /**
  89.      * @return bool
  90.      */
  91.     private function canContactShowList(): bool
  92.     {
  93.         return $this->canAccessContact();
  94.     }
  95.     /**
  96.      * @return bool
  97.      */
  98.     private function canContactAddEdit(): bool
  99.     {
  100.         return $this->canAccessContact();
  101.     }
  102.     /**
  103.      * @return bool
  104.      */
  105.     private function canContactManageFields(): bool
  106.     {
  107.         return $this->canAccessContact();
  108.     }
  109.     /**
  110.      * @return bool
  111.      */
  112.     private function canContactStateAddEdit(): bool
  113.     {
  114.         return $this->canAccessContact();
  115.     }
  116.     /**
  117.      * @param Contact  $subject
  118.      * @param UserInfo $user
  119.      *
  120.      * @return bool
  121.      */
  122.     private function canContactMove(Contact $subjectUser $user): bool
  123.     {
  124.         if ($subject->getCustomerId() !== $user->getCustomerId()) {
  125.             return false;
  126.         }
  127.         return $this->canAccessContact();
  128.     }
  129.     /**
  130.      * @TODO: Si le client n'a pas le droit "contact show", il peut tout de même voir les contacts rattachés aux prospects qu'il gère.
  131.      *
  132.      * @return bool
  133.      */
  134.     private function canAccessContact(): bool
  135.     {
  136.         return true;
  137.     }
  138. }