src/Security/LoginAuthenticator.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Doctrine\DBAL\TenantConnection;
  4. use App\Entity\Main\Tenant;
  5. use App\Repository\Main\TenantRepository;
  6. use App\Repository\Tenant\UserRepository;
  7. use App\Services\SwitchTenantManager;
  8. use Doctrine\DBAL\Driver\PDO\Exception;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  15. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  19. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  20. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  21. use Doctrine\Persistence\ManagerRegistry;
  22. use App\Entity\Tenant\User;
  23. class LoginAuthenticator extends AbstractLoginFormAuthenticator implements PasswordAuthenticatedInterface
  24. {
  25.     use TargetPathTrait;
  26.     public const LOGIN_ROUTE 'login';
  27.     private UrlGeneratorInterface $urlGenerator;
  28.     private UserPasswordEncoderInterface $passwordEncoder;
  29.     private UserRepository $userRepository;
  30.     private TenantRepository $tenantRepository;
  31.     private $doctrine;
  32.     public function __construct(
  33.         UrlGeneratorInterface $urlGenerator,
  34.         UserRepository $userRepository,
  35.         ManagerRegistry $doctrine,
  36.         TenantRepository $tenantRepository,
  37.         SwitchTenantManager $switchTenantManager
  38.     )
  39.     {
  40.         $switchTenantManager->reconnect();
  41.         $this->urlGenerator $urlGenerator;
  42.         $this->userRepository $userRepository;
  43.         $this->doctrine $doctrine;
  44.         $this->tenantRepository $tenantRepository;
  45.     }
  46.     public function supports(Request $request): bool
  47.     {
  48.         return self::LOGIN_ROUTE === $request->attributes->get('_route')
  49.             && $request->isMethod('POST');
  50.     }
  51.     /**
  52.      * Create a passport for the current request. Here a passport containing the user, the
  53.      * presented password and the CSRF token value.
  54.      * @param Request $request
  55.      * @return PassportInterface
  56.      */
  57.     public function authenticate(Request $request): PassportInterface
  58.     {
  59.         return new Passport(
  60.             new UserBadge($request->request->get('_username'), function ($userIdentifier) {
  61.                 return $this->userRepository->findOneByEmailOrUsername($userIdentifier);
  62.             }),
  63.             new PasswordCredentials($request->request->get('_password'))
  64.         );
  65.     }
  66.     /**
  67.      * Used to upgrade (rehash) the user's password automatically over time.
  68.      */
  69.     public function getPassword($credentials): ?string
  70.     {
  71.         return $credentials['password'];
  72.     }
  73.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?RedirectResponse
  74.     {
  75. //        dd($request->getSession());
  76.         //dd($this->getTargetPath($request->getSession(), $firewallName));
  77. //        if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  78. //            return new RedirectResponse($targetPath);
  79. //        }
  80.         return new RedirectResponse($this->urlGenerator->generate('admin'));
  81.     }
  82.     protected function getLoginUrl(Request $request): string
  83.     {
  84.         $tenant_id $request->request->get('_identifier');
  85.         return $this->urlGenerator->generate(self::LOGIN_ROUTE, ['tenant_id' => $tenant_id]);
  86.     }
  87. }