|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Application\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use App\Domain\User\Service\Authorization\UserPermissionVerifier; |
|
6
|
|
|
use App\Infrastructure\Utility\JsImportCacheBuster; |
|
7
|
|
|
use App\Infrastructure\Utility\Settings; |
|
8
|
|
|
use Cake\Database\Exception\DatabaseException; |
|
9
|
|
|
use Odan\Session\SessionInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
14
|
|
|
use Slim\App; |
|
15
|
|
|
use Slim\Interfaces\RouteParserInterface; |
|
16
|
|
|
use Slim\Routing\RouteContext; |
|
17
|
|
|
use Slim\Views\PhpRenderer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Adds attributes to the PhpRenderer and updates js imports with version number. |
|
21
|
|
|
* Documentation: https://github.com/samuelgfeller/slim-example-project/wiki/Template-rendering. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class PhpViewMiddleware implements MiddlewareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var array<string, mixed> */ |
|
26
|
|
|
private array $publicSettings; |
|
27
|
|
|
/** @var array<string, mixed> */ |
|
28
|
|
|
private array $deploymentSettings; |
|
29
|
|
|
|
|
30
|
179 |
|
public function __construct( |
|
31
|
|
|
private readonly App $app, |
|
32
|
|
|
private readonly PhpRenderer $phpRenderer, |
|
33
|
|
|
private readonly SessionInterface $session, |
|
34
|
|
|
private readonly JsImportCacheBuster $jsImportCacheBuster, |
|
35
|
|
|
Settings $settings, |
|
36
|
|
|
private readonly UserPermissionVerifier $userPermissionVerifier, |
|
37
|
|
|
private readonly RouteParserInterface $routeParser |
|
38
|
|
|
) { |
|
39
|
179 |
|
$this->publicSettings = $settings->get('public'); |
|
40
|
179 |
|
$this->deploymentSettings = $settings->get('deployment'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
179 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
44
|
|
|
{ |
|
45
|
179 |
|
$loggedInUserId = $this->session->get('user_id'); |
|
46
|
|
|
// The following has to work even with no connection to mysql to display the error page (layout needs those attr) |
|
47
|
179 |
|
$this->phpRenderer->setAttributes([ |
|
48
|
179 |
|
'version' => $this->deploymentSettings['version'], |
|
49
|
179 |
|
'uri' => $request->getUri(), |
|
50
|
179 |
|
'basePath' => $this->app->getBasePath(), |
|
51
|
179 |
|
'route' => $this->routeParser, |
|
52
|
179 |
|
'currRouteName' => RouteContext::fromRequest($request)->getRoute()?->getName(), |
|
53
|
179 |
|
'flash' => $this->session->getFlash(), |
|
54
|
|
|
// Used for public values used by view like company email address |
|
55
|
179 |
|
'config' => $this->publicSettings, |
|
56
|
179 |
|
'authenticatedUser' => $loggedInUserId, |
|
57
|
179 |
|
]); |
|
58
|
|
|
|
|
59
|
|
|
// Check and set user list authorization for "users" nav point |
|
60
|
179 |
|
if ($loggedInUserId) { |
|
61
|
|
|
// Check if the authenticated user is allowed to see user list |
|
62
|
|
|
// Add the user list authorization as an attribute to the PhpRenderer |
|
63
|
125 |
|
$this->phpRenderer->addAttribute( |
|
64
|
125 |
|
'userListAuthorization', |
|
65
|
125 |
|
$this->checkUserListAuthorization($loggedInUserId) |
|
66
|
125 |
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Add version number to js imports |
|
70
|
179 |
|
if ($this->deploymentSettings['update_js_imports_version'] === true) { |
|
71
|
|
|
$this->jsImportCacheBuster->addVersionToJsImports(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
179 |
|
return $handler->handle($request); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check if the user is allowed to see the user list. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $loggedInUserId |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
125 |
|
private function checkUserListAuthorization(int $loggedInUserId): bool |
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
|
|
// If the authenticated user is allowed to read another user (id + 1), the user list can be displayed |
|
87
|
125 |
|
return $this->userPermissionVerifier->isGrantedToRead($loggedInUserId + 1, false); |
|
88
|
|
|
} catch (DatabaseException $databaseException) { |
|
89
|
|
|
// Mysql connection not working. Caught here to prevent error page from crashing |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|