|
@@ -1,89 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace App\Security;
|
|
|
-
|
|
|
-use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
|
-use Symfony\Component\HttpFoundation\Request;
|
|
|
-use Symfony\Component\HttpFoundation\Response;
|
|
|
-use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
-use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
|
-use Symfony\Component\Security\Core\Security;
|
|
|
-use Symfony\Component\Security\Core\User\UserInterface;
|
|
|
-use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
|
|
|
-use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
|
|
|
-use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
|
|
|
-use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
|
|
-use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
|
|
|
-use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
|
|
-use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
|
|
-
|
|
|
-class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
|
|
|
-{
|
|
|
- use TargetPathTrait;
|
|
|
-
|
|
|
- public const LOGIN_ROUTE = 'app_login';
|
|
|
-
|
|
|
- private $urlGenerator;
|
|
|
-
|
|
|
- public function __construct(UrlGeneratorInterface $urlGenerator)
|
|
|
- {
|
|
|
- $this->urlGenerator = $urlGenerator;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Called on every request to decide if this authenticator should be
|
|
|
- * used for the request.
|
|
|
- *
|
|
|
- * @param Request $request request
|
|
|
- *
|
|
|
- * @return boolean Returning false will cause this authenticator to be skipped.
|
|
|
- */
|
|
|
- public function supports(Request $request): bool
|
|
|
- {
|
|
|
- return self::LOGIN_ROUTE === $request->attributes->get('_route')
|
|
|
- && $request->isMethod('POST');
|
|
|
-}
|
|
|
-
|
|
|
- public function authenticate(Request $request): Passport
|
|
|
- {
|
|
|
- $username = $request->request->get('username', '');
|
|
|
-
|
|
|
- $request->getSession()->set(Security::LAST_USERNAME, $username);
|
|
|
-
|
|
|
- return new Passport(
|
|
|
- new UserBadge($username),
|
|
|
- new PasswordCredentials($request->request->get('password', '')),
|
|
|
- [
|
|
|
- new CsrfTokenBadge('authenticate', $request->get('_csrf_token')),
|
|
|
- new RememberMeBadge(),
|
|
|
- ]
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- public function checkCredentials($credentials, UserInterface $user): bool
|
|
|
- {
|
|
|
- // Check the user's password or other credentials and return true or false
|
|
|
- // If there are no credentials to check, you can just return true
|
|
|
- return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
|
|
|
- }
|
|
|
-
|
|
|
- public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
|
|
|
- {
|
|
|
- if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
|
|
|
- return new RedirectResponse($targetPath);
|
|
|
- }
|
|
|
-
|
|
|
- // For example:
|
|
|
- return new RedirectResponse($this->urlGenerator->generate('videotheque_liste'));
|
|
|
- }
|
|
|
-
|
|
|
- protected function getLoginUrl(Request $request): string
|
|
|
- {
|
|
|
- return $this->urlGenerator->generate(self::LOGIN_ROUTE);
|
|
|
- }
|
|
|
-
|
|
|
- public function supportsRememberMe(): bool
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
-}
|