src/Security/LoginFormAuthenticator.php line 23

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Routing\RouterInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  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\Util\TargetPathTrait;
  20. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  21. {
  22.     use TargetPathTrait;
  23.     public const LOGIN_ROUTE 'app_login';
  24.     public const SUCCESS_ROUTE 'dashboard';
  25.     public function __construct(private EntityManagerInterface $manager, private RouterInterface $router, private UrlGeneratorInterface $urlGenerator, private UserRepository $userRepository)
  26.     {
  27.     }
  28.     public function authenticate(Request $request): Passport
  29.     {
  30.         $username $request->request->get('username''');
  31.         $request->getSession()->set(Security::LAST_USERNAME$username);
  32.         // Retrieve the authenticated user
  33.         $user $this->userRepository->findByEmailOrUsername($request->request->get('username'''));
  34.         if ($user) {
  35.             // Check if the user has the denied roles
  36.             if (in_array('ROLE_LECTURER'$user->getRoles()) || in_array('ROLE_STUDENT'$user->getRoles()) || in_array('ROLE_AGENT'$user->getRoles())) {
  37.                 throw new AccessDeniedException('Permission denied!');
  38.             }
  39.         }
  40.         return new Passport(
  41.             new UserBadge($username, function ($username) {
  42.                 return $this->userRepository->findByEmailOrUsername($username);
  43.             }),
  44.             new PasswordCredentials($request->request->get('password''')),
  45.             [
  46.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  47.             ]
  48.         );
  49.     }
  50.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  51.     {
  52. //        if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  53. //            return new RedirectResponse($targetPath);
  54. //        }
  55.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  56.             return new RedirectResponse($targetPath);
  57.         }
  58.         return new RedirectResponse(self::SUCCESS_ROUTE);
  59.         // For example:
  60.         // return new RedirectResponse($this->urlGenerator->generate('some_route'));
  61. //        throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  62.     }
  63.     protected function getLoginUrl(Request $request): string
  64.     {
  65.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  66.     }
  67.     /*
  68.      * @param $user
  69.      * @param TokenInterface $token
  70.      * @param Request $request
  71.      */
  72. //    private function getRoutesForUser(User $user, TokenInterface $token, Request $request): void
  73. //    {
  74. //        $this->setUserLastLogin($user);
  75. //
  76. //        $roles = $token->getUser()->getRoles();
  77. //
  78. //        $routes = ['app_login'];
  79. //
  80. //        foreach ($roles as $role) {
  81. //            $result = $this->lienSecuriseRepository->findRoutesForRoleAndUser($role);
  82. //
  83. //            foreach ($result as $value) {
  84. //                $routes[] = $value['routeName'];
  85. //            }
  86. //        }
  87. //
  88. //        $request->getSession()->set('routes', $routes);
  89. //    }
  90. //
  91. //    private function setUserLastLogin(User $user)
  92. //    {
  93. //        // set last login
  94. //        $user->setLastLogin(new \DateTime());
  95. //        $this->manager->flush();
  96. //
  97. //    }
  98. }