Lines
100.00%
12 / 12
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| onKernelController | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | ||
| requiresAdminAuthentication | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| 30 | readonly class ControllerContainerListener | |
| 31 | { | |
| 32 | private const string ADMIN_NAMESPACE_PREFIX = 'phpMyFAQ\\Controller\\Administration\\'; | |
| 33 | ||
| 34 | public function __construct( | |
| 35 | private ContainerInterface $container, | |
| 36 | ) { | |
| 37 | } | |
| 38 | ||
| 39 | public function onKernelController(ControllerEvent $event): void | |
| 40 | { | |
| 41 | $controller = $event->getController(); | |
| 42 | ||
| 43 | if (is_array($controller)) { | |
| 44 | $controller = $controller[0]; | |
| 45 | } | |
| 46 | ||
| 47 | if (!$controller instanceof AbstractController) { | |
| 48 | return; | |
| 49 | } | |
| 50 | ||
| 51 | $controller->setContainer($this->container); | |
| 52 | ||
| 53 | if ($this->requiresAdminAuthentication($controller)) { | |
| 54 | $controller->userIsAuthenticated(); | |
| 55 | } | |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Defense-in-depth: every controller in the Administration namespace must | |
| 60 | * have an authenticated user, except controllers that explicitly opt out by | |
| 61 | * implementing SkipsAuthenticationCheck (e.g. AuthenticationController, | |
| 62 | * which handles login/logout/token endpoints). | |
| 63 | */ | |
| 64 | private function requiresAdminAuthentication(AbstractController $controller): bool | |
| 65 | { | |
| 66 | if ($controller instanceof SkipsAuthenticationCheck) { | |
| 67 | return false; | |
| 68 | } | |
| 69 | ||
| 70 | return str_starts_with($controller::class, self::ADMIN_NAMESPACE_PREFIX); | |
| 71 | } | |
| 72 | } |