src/Security/HabilitationVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use App\Entity\HabilitationProfile;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class HabilitationVoter extends Voter
  8. {
  9.     // Tous les codes habilitation connus
  10.     public const CODES = [
  11.         'VOIR_DASHBOARD',
  12.         'SAISIR_VENTE',
  13.         'SAISIR_FOURNITURE',
  14.         'SAISIR_DEPENSE',
  15.         'GERER_PRODUITS',
  16.         'GERER_CATEGORIES',
  17.         'GERER_FOURNISSEURS',
  18.         'GERER_CLIENTS',
  19.         'GERER_CAISSE',
  20.         'GERER_STOCK',
  21.         'VOIR_ALERTES_STOCK',
  22.         'VOIR_REPORTING',
  23.         'GERER_UTILISATEURS',
  24.         'GERER_PROFILS',
  25.         'GERER_HABILITATIONS',
  26.     ];
  27.     protected function supports(string $attribute$subject): bool
  28.     {
  29.         return in_array($attributeself::CODEStrue);
  30.     }
  31.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  32.     {
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             return false;
  36.         }
  37.         // ROLE_ADMIN bypasse toutes les vérifications
  38.         if (in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  39.             return true;
  40.         }
  41.         $profile $user->getProfile();
  42.         if (!$profile || !$profile->isActif()) {
  43.             return false;
  44.         }
  45.         foreach ($profile->getHabilitationProfiles() as $hp) {
  46.             if (
  47.                 $hp->isStatus()
  48.                 && $hp->getHabilitation() !== null
  49.                 && $hp->getHabilitation()->getCode() === $attribute
  50.                 && $hp->getHabilitation()->isStatut()
  51.             ) {
  52.                 return true;
  53.             }
  54.         }
  55.         return false;
  56.     }
  57. }