CustomUserMessageAuthenticationException.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 where you can control the message shown to the user.
  13. *
  14. * Be sure that the message passed to this exception is something that
  15. * can be shown safely to your user. In other words, avoid catching
  16. * other exceptions and passing their message directly to this class.
  17. *
  18. * @author Ryan Weaver <ryan@knpuniversity.com>
  19. */
  20. class CustomUserMessageAuthenticationException extends AuthenticationException
  21. {
  22. private $messageKey;
  23. private $messageData = array();
  24. public function __construct($message = '', array $messageData = array(), $code = 0, \Exception $previous = null)
  25. {
  26. parent::__construct($message, $code, $previous);
  27. $this->setSafeMessage($message, $messageData);
  28. }
  29. /**
  30. * Set a message that will be shown to the user.
  31. *
  32. * @param string $messageKey The message or message key
  33. * @param array $messageData Data to be passed into the translator
  34. */
  35. public function setSafeMessage($messageKey, array $messageData = array())
  36. {
  37. $this->messageKey = $messageKey;
  38. $this->messageData = $messageData;
  39. }
  40. public function getMessageKey()
  41. {
  42. return $this->messageKey;
  43. }
  44. public function getMessageData()
  45. {
  46. return $this->messageData;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function serialize()
  52. {
  53. return serialize(array(
  54. parent::serialize(),
  55. $this->messageKey,
  56. $this->messageData,
  57. ));
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function unserialize($str)
  63. {
  64. list($parentData, $this->messageKey, $this->messageData) = unserialize($str);
  65. parent::unserialize($parentData);
  66. }
  67. }