src/Auth/Authenticator.php line 123

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: samuelp
  5.  * Date: 10/20/17
  6.  * Time: 10:33 PM
  7.  */
  8. namespace App\Auth;
  9. use App\Entity\OrganizationUser;
  10. use App\Entity\User;
  11. use App\Entity\UserStation;
  12. use App\Form\LoginForm;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  21. use Symfony\Component\Routing\RouterInterface;
  22. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  23. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  24. use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
  25. use Symfony\Component\Security\Core\Security;
  26. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  27. use Symfony\Component\Security\Core\User\UserInterface;
  28. use Symfony\Component\Security\Core\User\UserProviderInterface;
  29. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  30. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  31. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  32. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  33. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  34. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  35. use UnexpectedValueException;
  36. class Authenticator  extends AbstractLoginFormAuthenticator implements PasswordAuthenticatedUserInterface{
  37.     private EntityManagerInterface $entityManager;
  38.     private UserPasswordHasherInterface $passwordHasher;
  39.     private UrlGeneratorInterface $urlGenerator;
  40.     private FormFactoryInterface $formFactory;
  41.     private User $user;
  42.     public function __construct(UrlGeneratorInterface $urlGenerator,
  43.                                 EntityManagerInterface $entityManager,
  44.                                 CsrfTokenManagerInterface $csrfTokenManager,
  45.                                 UserPasswordHasherInterface $passwordHasher,
  46.                                 Security $security)
  47.     {
  48.         $this->urlGenerator $urlGenerator;
  49.         $this->entityManager $entityManager;
  50.         $this->csrfTokenManager $csrfTokenManager;
  51.         $this->passwordHasher $passwordHasher;
  52.         $this->security $security;
  53.     }
  54.     protected function getLoginUrl(Request $request): string
  55.     {
  56.         // TODO: Implement getLoginUrl() method.
  57.         return $this->urlGenerator->generate('security_signin');
  58.     }
  59.     public function authenticate(Request $request) {
  60.         // TODO: Implement getCredentials() method.
  61.         $username $request->request->get('username''');
  62. //        dump($username);
  63. //        die;
  64.         $request->getSession()->set(Security::LAST_USERNAME$username);
  65.         $user $this->entityManager->getRepository(User::class)->findOneBy([
  66.             'username' => $username
  67.         ]);
  68.         if(!$user){
  69.             throw new CustomUserMessageAccountStatusException("invalid Username or Password ");
  70.         }
  71.         return new Passport(
  72.             new UserBadge($user),
  73.             new PasswordCredentials($request->request->get('password''')),
  74.             [
  75.                 new CsrfTokenBadge('authenticate'$request->get('_csrf_token')),
  76.             ]
  77.         );
  78.     }
  79.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  80.     {
  81.         $user $token->getUser();
  82.         /** @var  OrganizationUser $saccoUser */
  83.         $saccoUser $this->entityManager->getRepositoryOrganizationUser::class)->findOneBy([
  84.             'user' => $user
  85.         ]);
  86.         $request->getSession()->set('ORGANIZATION'$saccoUser->getOrganization()->getId());
  87.         if(!$saccoUser) {
  88.             return new RedirectResponse($this->urlGenerator->generate('no_mans_land'));
  89.         }
  90.         /** @var  UserStation $userStation */
  91.         $userStation $this->entityManager->getRepository(UserStation::class)->findOneBy([
  92.             'user' => $user,
  93.             'isActive' => true
  94.         ]);
  95.         if($userStation) {
  96.             $request->getSession()->set('STATION'$userStation->getStation()->getId());
  97.             return new RedirectResponse($this->urlGenerator->generate('parcels_homepage'));
  98.         }
  99.         return new RedirectResponse($this->urlGenerator->generate('homepage'));
  100.     }
  101.     public function getPassword(): ?string
  102.     {
  103.         return '';
  104.     }
  105. }