1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of template |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Template\Extension; |
13
|
|
|
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use Slick\Configuration\ConfigurationInterface; |
16
|
|
|
use Slick\Configuration\Driver\Environment; |
17
|
|
|
use Slick\WebStack\Domain\Security\AuthorizationCheckerInterface; |
18
|
|
|
use Slick\WebStack\Domain\Security\SecurityAuthenticatorInterface; |
19
|
|
|
use Slick\WebStack\Domain\Security\UserInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SlickApp |
23
|
|
|
* |
24
|
|
|
* @package Slick\Template\Extension |
25
|
|
|
*/ |
26
|
|
|
class SlickApp |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Creates a SlickApp |
30
|
|
|
* |
31
|
|
|
* @param AuthorizationCheckerInterface<UserInterface>|null $auth |
32
|
|
|
* @param SecurityAuthenticatorInterface|null $authenticator |
33
|
|
|
* @param ServerRequestInterface|null $request |
34
|
|
|
* @param ConfigurationInterface|null $settings |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
private readonly ?AuthorizationCheckerInterface $auth = null, |
38
|
|
|
private readonly ?SecurityAuthenticatorInterface $authenticator = null, |
39
|
|
|
private readonly ?ServerRequestInterface $request = null, |
40
|
|
|
private readonly ?ConfigurationInterface $settings = null, |
41
|
|
|
) { |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieves the authenticated user, if available. |
46
|
|
|
* |
47
|
|
|
* @return UserInterface|null The authenticated user, or null if not authenticated. |
48
|
|
|
*/ |
49
|
|
|
public function user(): ?UserInterface |
50
|
|
|
{ |
51
|
|
|
$enabled = $this->authenticator && $this->authenticator->enabled(); |
52
|
|
|
if ($enabled && $this->auth instanceof AuthorizationCheckerInterface) { |
53
|
|
|
return $this->auth->authenticatedUser(); |
54
|
|
|
} |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Retrieve the server request. |
60
|
|
|
* |
61
|
|
|
* @return ServerRequestInterface|null The server request object, or null if not set. |
62
|
|
|
*/ |
63
|
|
|
public function request(): ?ServerRequestInterface |
64
|
|
|
{ |
65
|
|
|
return $this->request; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function settings(): ConfigurationInterface |
69
|
|
|
{ |
70
|
|
|
if (!$this->settings) { |
71
|
|
|
return new Environment(); |
72
|
|
|
} |
73
|
|
|
return $this->settings; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|