src/Security/SecurityAuthenticator.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Users;
  4. use App\Entity\PDossier;
  5. use App\Entity\Pemploye;
  6. use App\Entity\LContract;
  7. use App\Repository\UsersRepository;
  8. use App\Entity\UserOperationDossier;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use App\Repository\UserOperationDossierRepository;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  20. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  22. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  23. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  24. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  27. class SecurityAuthenticator extends AbstractLoginFormAuthenticator
  28. {
  29.     use TargetPathTrait;
  30.     public const LOGIN_ROUTE 'app_login';
  31.     private $passwordEncoder;
  32.     private $urlGenerator;
  33.     private $userRepository;
  34.     private $doctrine;
  35.     public function __construct(UrlGeneratorInterface $urlGenerator,UserPasswordHasherInterface $passwordEncoderUsersRepository $userRepositoryManagerRegistry $doctrine)
  36.     {
  37.         $this->urlGenerator $urlGenerator;
  38.         $this->passwordEncoder $passwordEncoder;
  39.         $this->userRepository $userRepository;
  40.         $this->doctrine $doctrine;
  41.     }
  42.     public function authenticate(Request $request): Passport
  43.     {
  44.         $username $request->request->get('username''');
  45.         $password $request->request->get('password''');
  46.         $request->getSession()->set(Security::LAST_USERNAME$username);
  47.         return new Passport(
  48.             new UserBadge($username, function($userIdentifier) {
  49.                 // optionally pass a callback to load the User manually
  50.                 $user $this->userRepository->findOneBy(['username' => $userIdentifier]);
  51.                 if (!$user) {
  52.                     $user $this->doctrine->getManager()->getRepository(Pemploye::class)->findOneBy(['cin' => $userIdentifier'active' => true]);
  53.                     if(!$user) {
  54.                         throw new CustomUserMessageAuthenticationException("Username introuvable!");
  55.                     }
  56.                 }
  57.                 // dd($user);
  58.                 return $user;
  59.             }),
  60.             new CustomCredentials(function($credentials$user) {
  61.                 if(!$this->passwordEncoder->isPasswordValid($user$credentials)) {
  62.                     throw new CustomUserMessageAuthenticationException("Votre mot de passe est incorrect!");
  63.                 }
  64.                 if($user instanceof Pemploye) {
  65.                     $contract $this->doctrine->getManager()->getRepository(LContract::class)->findOneBy(['employe' => $user'active' => true]);
  66.                     if(!$contract) {
  67.                         throw new CustomUserMessageAuthenticationException("Vous n'avez aucune contrat!");
  68.                     }
  69.                     return true;
  70.                 }
  71.                 if(!$user->isEnable()) {
  72.                     throw new CustomUserMessageAuthenticationException("Votre compte est desactiver veuillez contacte l'adminsitrateur");
  73.                 }
  74.                 
  75.                 if(in_array('ROLE_USER'$user->getRoles())){
  76.                     $dossiers $this->doctrine->getManager()->getRepository(UserOperationDossier::class)->finddossier($user->getId());
  77.                     if (count($dossiers) == 0) {
  78.                         throw new CustomUserMessageAuthenticationException("Vous n'avez aucune affectation !");
  79.                     }   
  80.                 }
  81.                 // $this->users = $user;
  82.                 return true;
  83.             }, $password),
  84.             [
  85.                 new RememberMeBadge(),
  86.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  87.             ]
  88.         );
  89.     }
  90.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  91.     {
  92.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  93.             return new RedirectResponse($targetPath);
  94.         }
  95.         // For example
  96.         return new RedirectResponse($this->urlGenerator->generate('app_site'));
  97.         throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  98.     }
  99.     protected function getLoginUrl(Request $request): string
  100.     {
  101.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  102.     }
  103. }