LoginSuccessEvent.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Event;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Exception\LogicException;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  19. use Symfony\Contracts\EventDispatcher\Event;
  20. /**
  21. * This event is dispatched after authentication has successfully completed.
  22. *
  23. * At this stage, the authenticator created an authenticated token
  24. * and generated an authentication success response. Listeners to
  25. * this event can do actions related to successful authentication
  26. * (such as migrating the password).
  27. *
  28. * @author Wouter de Jong <wouter@wouterj.nl>
  29. */
  30. class LoginSuccessEvent extends Event
  31. {
  32. private $authenticator;
  33. private $passport;
  34. private $authenticatedToken;
  35. private $request;
  36. private $response;
  37. private $firewallName;
  38. public function __construct(AuthenticatorInterface $authenticator, PassportInterface $passport, TokenInterface $authenticatedToken, Request $request, ?Response $response, string $firewallName)
  39. {
  40. $this->authenticator = $authenticator;
  41. $this->passport = $passport;
  42. $this->authenticatedToken = $authenticatedToken;
  43. $this->request = $request;
  44. $this->response = $response;
  45. $this->firewallName = $firewallName;
  46. }
  47. public function getAuthenticator(): AuthenticatorInterface
  48. {
  49. return $this->authenticator;
  50. }
  51. public function getPassport(): PassportInterface
  52. {
  53. return $this->passport;
  54. }
  55. public function getUser(): UserInterface
  56. {
  57. if (!$this->passport instanceof UserPassportInterface) {
  58. throw new LogicException(sprintf('Cannot call "%s" as the authenticator ("%s") did not set a user.', __METHOD__, \get_class($this->authenticator)));
  59. }
  60. return $this->passport->getUser();
  61. }
  62. public function getAuthenticatedToken(): TokenInterface
  63. {
  64. return $this->authenticatedToken;
  65. }
  66. public function getRequest(): Request
  67. {
  68. return $this->request;
  69. }
  70. public function getFirewallName(): string
  71. {
  72. return $this->firewallName;
  73. }
  74. public function setResponse(?Response $response): void
  75. {
  76. $this->response = $response;
  77. }
  78. public function getResponse(): ?Response
  79. {
  80. return $this->response;
  81. }
  82. }