|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of contao-community-alliance/dc-general. |
| 5 | + * |
| 6 | + * (c) 2013-2026 Contao Community Alliance. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * This project is provided in good faith and hope to be usable by anyone. |
| 12 | + * |
| 13 | + * @package contao-community-alliance/dc-general |
| 14 | + * @author Ingolf Steinhardt <info@e-spin.de> |
| 15 | + * @copyright 2013-2026 Contao Community Alliance. |
| 16 | + * @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later |
| 17 | + * @filesource |
| 18 | + */ |
| 19 | + |
| 20 | +namespace ContaoCommunityAlliance\DcGeneral\Exception; |
| 21 | + |
| 22 | +use Contao\System; |
| 23 | +use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
| 24 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * Base class for the "you may not perform this action here" exceptions. |
| 28 | + * |
| 29 | + * These describe a state, not a programming error: the editor tried an action the data |
| 30 | + * definition does not allow. Extending Symfony's AccessDeniedException instead of |
| 31 | + * DcGeneralRuntimeException lets the security firewall turn this into a proper 403 (or, for an |
| 32 | + * anonymous visitor, a redirect to the login form) instead of a 500 error screen - see |
| 33 | + * contao-community-alliance/dc-general#543. |
| 34 | + */ |
| 35 | +abstract class AbstractDefinitionAccessDeniedException extends AccessDeniedException |
| 36 | +{ |
| 37 | + /** |
| 38 | + * The definition name of the affected definition. |
| 39 | + * |
| 40 | + * @var string |
| 41 | + */ |
| 42 | + protected $name; |
| 43 | + |
| 44 | + /** |
| 45 | + * Create instance. |
| 46 | + * |
| 47 | + * @param string $definitionName The definition name of the affected definition. |
| 48 | + * @param \Throwable|null $previous The previous exception. |
| 49 | + */ |
| 50 | + public function __construct($definitionName, ?\Throwable $previous = null) |
| 51 | + { |
| 52 | + $this->name = $definitionName; |
| 53 | + |
| 54 | + parent::__construct($this->resolveMessage(), $previous); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * The translation key holding the human-readable message for this case. |
| 59 | + * |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + abstract protected function translationKey(): string; |
| 63 | + |
| 64 | + /** |
| 65 | + * The message used when no translator is available, e.g. in a unit test. |
| 66 | + * |
| 67 | + * @return string |
| 68 | + */ |
| 69 | + abstract protected function fallbackMessage(): string; |
| 70 | + |
| 71 | + /** |
| 72 | + * Resolve the translated message, falling back gracefully outside a bootstrapped Contao |
| 73 | + * framework (e.g. in a unit test constructing the exception directly). |
| 74 | + * |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + private function resolveMessage(): string |
| 78 | + { |
| 79 | + $container = System::getContainer(); |
| 80 | + // Contao's own docblock claims this is never null, but it genuinely is before the |
| 81 | + // framework has booted - e.g. in a plain unit test constructing this exception directly. |
| 82 | + /** @psalm-suppress DocblockTypeContradiction */ |
| 83 | + if (null === $container || !$container->has('translator')) { |
| 84 | + return $this->fallbackMessage(); |
| 85 | + } |
| 86 | + |
| 87 | + $translator = $container->get('translator'); |
| 88 | + assert($translator instanceof TranslatorInterface); |
| 89 | + |
| 90 | + $message = $translator->trans($this->translationKey(), [], 'dc-general'); |
| 91 | + |
| 92 | + return $message !== $this->translationKey() ? $message : $this->fallbackMessage(); |
| 93 | + } |
| 94 | +} |
0 commit comments