src/Voter/User/UserCommercantVoter.php line 12

  1. <?php
  2. namespace App\Voter\User;
  3. use App\Entity\User\UserCommercant;
  4. use App\Repository\User\UserStatutRepository;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Bundle\SecurityBundle\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class UserCommercantVoter extends Voter
  10. {
  11.     const INDEX 'user_user_commercant_index';
  12.     const FICHE 'user_user_commercant_fiche';
  13.     const ADD 'user_user_commercant_add';
  14.     const EDIT 'user_user_commercant_edit';
  15.     const DELETE 'user_user_commercant_delete';
  16.     const ATTRIBUTES = [
  17.         self::INDEX,
  18.         self::FICHE,
  19.         self::ADD,
  20.         self::EDIT,
  21.         self::DELETE,
  22.     ];
  23.     /**
  24.      * @var Security
  25.      */
  26.     private $theSecurity;
  27.     public function __construct(Security $theSecurity)
  28.     {
  29.         $this->theSecurity $theSecurity;
  30.     }
  31.     protected function supports(string $attribute$subject): bool
  32.     {
  33.         if (!in_array($attributeself::ATTRIBUTES)) {
  34.             return false;
  35.         }
  36.         if (
  37.             !$subject instanceof UserCommercant &&
  38.             (($attribute != self::INDEX || $attribute != self::ADD) && $subject != null)
  39.         ) {
  40.             return false;
  41.         }
  42.         return true;
  43.     }
  44.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  45.     {
  46.         $theUserCurrent $token->getUser();
  47.         if (!$theUserCurrent instanceof UserInterface) {
  48.             return false;
  49.         }
  50.         /** @var UserCommercant $theUserCommercant */
  51.         $theUserCommercant $subject;
  52.         switch ($attribute) {
  53.             case self::INDEX:
  54.                 return $this->accessIndex();
  55.             case self::FICHE:
  56.                 return $this->accessFiche($theUserCommercant);
  57.             case self::ADD:
  58.                 return $this->accessAdd();
  59.             case self::EDIT:
  60.                 return $this->accessEdit($theUserCommercant);
  61.             case self::DELETE:
  62.                 return $this->accessDelete($theUserCommercant);
  63.             default:
  64.                 return false;
  65.         }
  66.     }
  67.     protected function accessUser()
  68.     {
  69.         return $this->theSecurity->getUser()->getTheStatut()->getId() == UserStatutRepository::DATA['actif'];
  70.     }
  71.     
  72.     protected function accessAdmin()
  73.     {
  74.         return
  75.             $this->theSecurity->isGranted('ROLE_ADMIN');
  76.     }
  77.     protected function accessCommercant(?UserCommercant $theUserCommercant null)
  78.     {
  79.         return
  80.             $this->accessUser()
  81.             && $this->theSecurity->isGranted('ROLE_COMMERCANT')
  82.             && (
  83.                 $theUserCommercant != null
  84.                 && $theUserCommercant->getId() == $this->theSecurity->getUser()->getId()
  85.             );
  86.     }
  87.     protected function accessWrite(?UserCommercant $theUserCommercant null)
  88.     {
  89.         return
  90.             $this->accessRead($theUserCommercant);
  91.     }
  92.     protected function accessRead(?UserCommercant $theUserCommercant null)
  93.     {
  94.         return
  95.             $this->accessAdmin()
  96.             || $this->accessCommercant($theUserCommercant);
  97.     }
  98.     protected function accessIndex() : bool
  99.     {
  100.         return $this->accessRead();
  101.     }
  102.     protected function accessFiche(UserCommercant $theUserCommercant) : bool
  103.     {
  104.         return $this->accessRead($theUserCommercant);
  105.     }
  106.     protected function accessAdd() : bool
  107.     {
  108.         return $this->accessWrite();
  109.     }
  110.     protected function accessEdit(UserCommercant $theUserCommercant) : bool
  111.     {
  112.         return $this->accessWrite($theUserCommercant);
  113.     }
  114.     protected function accessDelete(UserCommercant $theUserCommercant) : bool
  115.     {
  116.         return $this->accessWrite($theUserCommercant);
  117.     }
  118. }