|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Exception; |
| 13 | + |
| 14 | +/** |
| 15 | + * An authentication exception where you can control the message shown to the user. |
| 16 | + * |
| 17 | + * Be sure that the message passed to this exception is something that |
| 18 | + * can be shown safely to your user. In other words, avoid catching |
| 19 | + * other exceptions and passing their message directly to this class. |
| 20 | + * |
| 21 | + * @author Ryan Weaver <ryan@knpuniversity.com> |
| 22 | + */ |
| 23 | +class CustomUserMessageAuthenticaitonException extends AuthenticationException |
| 24 | +{ |
| 25 | + private $messageKey; |
| 26 | + |
| 27 | + private $messageData = array(); |
| 28 | + |
| 29 | + public function __construct($message = '', array $messageData = array(), $code = 0, \Exception $previous = null) |
| 30 | + { |
| 31 | + parent::__construct($message, $code, $previous); |
| 32 | + |
| 33 | + $this->setSafeMessage($message, $messageData); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Set a message that will be shown to the user. |
| 38 | + * |
| 39 | + * @param string $messageKey The message or message key |
| 40 | + * @param array $messageData Data to be passed into the translator |
| 41 | + */ |
| 42 | + public function setSafeMessage($messageKey, array $messageData = array()) |
| 43 | + { |
| 44 | + $this->messageKey = $messageKey; |
| 45 | + $this->messageData = $messageData; |
| 46 | + } |
| 47 | + |
| 48 | + public function getMessageKey() |
| 49 | + { |
| 50 | + return $this->messageKey; |
| 51 | + } |
| 52 | + |
| 53 | + public function getMessageData() |
| 54 | + { |
| 55 | + return $this->messageData; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + public function serialize() |
| 62 | + { |
| 63 | + return serialize(array( |
| 64 | + parent::serialize(), |
| 65 | + $this->messageKey, |
| 66 | + $this->messageData, |
| 67 | + )); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + public function unserialize($str) |
| 74 | + { |
| 75 | + list($parentData, $this->messageKey, $this->messageData) = unserialize($str); |
| 76 | + |
| 77 | + parent::unserialize($parentData); |
| 78 | + } |
| 79 | +} |
0 commit comments