Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| LegacyMiddleware | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
30 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| process | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
9 | |||
| loadPage | n/a |
0 / 0 |
n/a |
0 / 0 |
16 | |||||
| renderPage | n/a |
0 / 0 |
n/a |
0 / 0 |
4 | |||||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Middleware; |
| 6 | |
| 7 | use Engelsystem\Helpers\Authenticator; |
| 8 | use Engelsystem\Helpers\Translation\Translator; |
| 9 | use Engelsystem\Http\Request; |
| 10 | use Psr\Container\ContainerInterface; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | use Psr\Http\Message\ServerRequestInterface; |
| 13 | use Psr\Http\Server\MiddlewareInterface; |
| 14 | use Psr\Http\Server\RequestHandlerInterface; |
| 15 | |
| 16 | /** |
| 17 | * Middleware to support the old routing / pages from includes |
| 18 | */ |
| 19 | class LegacyMiddleware implements MiddlewareInterface |
| 20 | { |
| 21 | /** @var array<string> */ |
| 22 | protected array $free_pages = [ |
| 23 | 'angeltypes', |
| 24 | 'public_dashboard', |
| 25 | 'locations', |
| 26 | 'shift_entries', |
| 27 | 'shifts', |
| 28 | 'users', |
| 29 | ]; |
| 30 | |
| 31 | public function __construct(protected ContainerInterface $container, protected Authenticator $auth) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Handle the request the old way |
| 37 | * |
| 38 | * Should be used before a 404 is sent |
| 39 | */ |
| 40 | public function process( |
| 41 | ServerRequestInterface $request, |
| 42 | RequestHandlerInterface $handler |
| 43 | ): ResponseInterface { |
| 44 | /** @var Request $appRequest */ |
| 45 | $appRequest = $this->container->get('request'); |
| 46 | $page = $appRequest->query->get('p'); |
| 47 | // Support old URL/permission scheme |
| 48 | if (empty($page)) { |
| 49 | $page = $appRequest->path(); |
| 50 | $page = str_replace('-', '_', $page); |
| 51 | } |
| 52 | |
| 53 | $allowPage = false; |
| 54 | if ($page === 'admin_arrive') { |
| 55 | $allowPage = $this->auth->can('users.arrive.list'); |
| 56 | } |
| 57 | |
| 58 | $title = $content = ''; |
| 59 | if ( |
| 60 | preg_match('~^\w+$~i', $page) |
| 61 | && (in_array($page, $this->free_pages) || $this->auth->can($page) || $allowPage) |
| 62 | ) { |
| 63 | list($title, $content) = $this->loadPage($page); |
| 64 | } |
| 65 | |
| 66 | if (empty($title) and empty($content)) { |
| 67 | /** @var Translator $translator */ |
| 68 | $translator = $this->container->get('translator'); |
| 69 | |
| 70 | $page = 404; |
| 71 | $title = $translator->translate('page.404.title'); |
| 72 | $content = $translator->translate('page.404.text'); |
| 73 | } |
| 74 | |
| 75 | return $this->renderPage($page, $title, $content); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get the legacy page content and title |
| 80 | * |
| 81 | * @return array ['title', 'content'] |
| 82 | * @codeCoverageIgnore |
| 83 | */ |
| 84 | protected function loadPage(string $page): array |
| 85 | { |
| 86 | switch ($page) { |
| 87 | case 'public_dashboard': |
| 88 | return public_dashboard_controller(); |
| 89 | case 'angeltypes': |
| 90 | return angeltypes_controller(); |
| 91 | case 'shift_entries': |
| 92 | return shift_entries_controller(); |
| 93 | case 'shifts': |
| 94 | return shifts_controller(); |
| 95 | case 'users': |
| 96 | return users_controller(); |
| 97 | case 'user_angeltypes': |
| 98 | return user_angeltypes_controller(); |
| 99 | case 'locations': |
| 100 | return locations_controller(); |
| 101 | case 'user_myshifts': |
| 102 | $title = myshifts_title(); |
| 103 | $content = user_myshifts(); |
| 104 | return [$title, $content]; |
| 105 | case 'user_shifts': |
| 106 | $title = shifts_title(); |
| 107 | $content = user_shifts(); |
| 108 | return [$title, $content]; |
| 109 | case 'admin_user': |
| 110 | $title = admin_user_title(); |
| 111 | $content = admin_user(); |
| 112 | return [$title, $content]; |
| 113 | case 'admin_arrive': |
| 114 | $title = admin_arrive_title(); |
| 115 | $content = admin_arrive(); |
| 116 | return [$title, $content]; |
| 117 | case 'admin_active': |
| 118 | $title = admin_active_title(); |
| 119 | $content = admin_active(); |
| 120 | return [$title, $content]; |
| 121 | case 'admin_free': |
| 122 | $title = admin_free_title(); |
| 123 | $content = admin_free(); |
| 124 | return [$title, $content]; |
| 125 | case 'admin_groups': |
| 126 | $title = admin_groups_title(); |
| 127 | $content = admin_groups(); |
| 128 | return [$title, $content]; |
| 129 | case 'admin_shifts': |
| 130 | $title = admin_shifts_title(); |
| 131 | $content = admin_shifts(); |
| 132 | return [$title, $content]; |
| 133 | } |
| 134 | |
| 135 | throw_redirect(url('/login')); |
| 136 | |
| 137 | return []; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Render the template |
| 142 | * |
| 143 | * @codeCoverageIgnore |
| 144 | */ |
| 145 | protected function renderPage(string | int $page, string $title, string $content): ResponseInterface |
| 146 | { |
| 147 | if (!empty($page) && is_int($page)) { |
| 148 | return response($content, $page); |
| 149 | } |
| 150 | |
| 151 | if (str_contains($content, '<html')) { |
| 152 | return response($content); |
| 153 | } |
| 154 | |
| 155 | return response( |
| 156 | view( |
| 157 | 'layouts/app', |
| 158 | [ |
| 159 | 'title' => $title, |
| 160 | 'content' => msg() . $content, |
| 161 | ] |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | } |