CustomUserMessageAccountStatusException.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Core\Exception;
  11. /**
  12. * An authentication exception caused by the user account status
  13. * where you can control the message shown to the user.
  14. *
  15. * Be sure that the message passed to this exception is something that
  16. * can be shown safely to your user. In other words, avoid catching
  17. * other exceptions and passing their message directly to this class.
  18. *
  19. * @author Vincent Langlet <vincentlanglet@github.com>
  20. */
  21. class CustomUserMessageAccountStatusException extends AccountStatusException
  22. {
  23. private $messageKey;
  24. private $messageData = [];
  25. public function __construct(string $message = '', array $messageData = [], int $code = 0, \Throwable $previous = null)
  26. {
  27. parent::__construct($message, $code, $previous);
  28. $this->setSafeMessage($message, $messageData);
  29. }
  30. /**
  31. * Sets a message that will be shown to the user.
  32. *
  33. * @param string $messageKey The message or message key
  34. * @param array $messageData Data to be passed into the translator
  35. */
  36. public function setSafeMessage(string $messageKey, array $messageData = [])
  37. {
  38. $this->messageKey = $messageKey;
  39. $this->messageData = $messageData;
  40. }
  41. public function getMessageKey()
  42. {
  43. return $this->messageKey;
  44. }
  45. public function getMessageData()
  46. {
  47. return $this->messageData;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function __serialize(): array
  53. {
  54. return [parent::__serialize(), $this->messageKey, $this->messageData];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function __unserialize(array $data): void
  60. {
  61. [$parentData, $this->messageKey, $this->messageData] = $data;
  62. parent::__unserialize($parentData);
  63. }
  64. }