src/Voter/User/UserAdminVoter.php line 12

  1. <?php
  2. namespace App\Voter\User;
  3. use App\Entity\User\UserAdmin;
  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 UserAdminVoter extends Voter
  10. {
  11.     const INDEX 'user_user_admin_index';
  12.     const FICHE 'user_user_admin_fiche';
  13.     const ADD 'user_user_admin_add';
  14.     const EDIT 'user_user_admin_edit';
  15.     const DELETE 'user_user_admin_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 UserAdmin &&
  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 UserAdmin $theUserAdmin */
  51.         $theUserAdmin $subject;
  52.         switch ($attribute) {
  53.             case self::INDEX:
  54.                 return $this->accessIndex();
  55.             case self::FICHE:
  56.                 return $this->accessFiche($theUserAdmin);
  57.             case self::ADD:
  58.                 return $this->accessAdd();
  59.             case self::EDIT:
  60.                 return $this->accessEdit($theUserAdmin);
  61.             case self::DELETE:
  62.                 return $this->accessDelete($theUserAdmin);
  63.             default:
  64.                 return false;
  65.         }
  66.     }
  67.     protected function accessUser()
  68.     {
  69.         return $this->theSecurity->getUser()->getTheStatut()->getId() == UserStatutRepository::DATA['actif'];
  70.     }
  71.     protected function accessSuperAdmin()
  72.     {
  73.         return $this->theSecurity->isGranted('ROLE_SUPER_ADMIN');
  74.     }
  75.     protected function accessAdmin(?UserAdmin $theUserAdmin null)
  76.     {
  77.         return
  78.             $this->accessUser()
  79.             && $this->theSecurity->isGranted('ROLE_ADMIN')
  80.             && (
  81.                 $theUserAdmin == null
  82.                 || !$theUserAdmin->hasRole('ROLE_SUPER_ADMIN')
  83.             );
  84.     }
  85.     protected function accessWrite(?UserAdmin $theUserAdmin null)
  86.     {
  87.         return
  88.             $this->accessRead($theUserAdmin);
  89.     }
  90.     protected function accessRead(?UserAdmin $theUserAdmin null)
  91.     {
  92.         return
  93.             $this->accessSuperAdmin()
  94.             || $this->accessAdmin($theUserAdmin);
  95.     }
  96.     protected function accessIndex() : bool
  97.     {
  98.         return $this->accessRead();
  99.     }
  100.     protected function accessFiche(UserAdmin $theUserAdmin) : bool
  101.     {
  102.         return $this->accessRead($theUserAdmin);
  103.     }
  104.     protected function accessAdd() : bool
  105.     {
  106.         return $this->accessWrite();
  107.     }
  108.     protected function accessEdit(UserAdmin $theUserAdmin) : bool
  109.     {
  110.         return $this->accessWrite($theUserAdmin);
  111.     }
  112.     protected function accessDelete(UserAdmin $theUserAdmin) : bool
  113.     {
  114.         return $this->accessWrite($theUserAdmin);
  115.     }
  116. }