src/Security/Voter/UserVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. class UserVoter extends Voter
  8. {
  9.     public function __construct(Private Security $security){}
  10.     protected function supports($attribute$subject): bool
  11.     {
  12.         return in_array($attribute, ['USER_VERIFIED''USER_APPROUVED''PROF_INDEPENDANT''PROF_NOT_SUSPENDED']);
  13.     }
  14.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  15.     {
  16.         $user $token->getUser();
  17.         // if the user is anonymous, do not grant access
  18.         if (!$user instanceof UserInterface) {
  19.             return false;
  20.         }
  21.         // dd($user);
  22.         // check isVerified()
  23.         if ($attribute=='USER_VERIFIED' and $user->isVerified()) {
  24.             return true;
  25.         }
  26.         // check isApprouved()
  27.         if ($attribute=='USER_APPROUVED' and $user->isApprouved()) {
  28.             return true;
  29.         }
  30.         // check isProfIndependant()
  31.         if ($attribute=='PROF_INDEPENDANT' and $user->isProf() and $user->isIndependant()) {
  32.             return true;
  33.         }
  34.         // check isProfNotSuspended()
  35.         if ($attribute=='PROF_NOT_SUSPENDED' and $user->isProf() and !$user->isSuspend()) {
  36.             return true;
  37.         }
  38.         return false;
  39.     }
  40. }