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