|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of web-stack |
|
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\WebStack\Domain\Security\Http\Authenticator; |
|
13
|
|
|
|
|
14
|
|
|
use Slick\WebStack\Domain\Security\Http\Authenticator\Passport\Badge\UserBadge; |
|
15
|
|
|
use Slick\WebStack\Domain\Security\Http\Authenticator\Passport\BadgeInterface; |
|
16
|
|
|
use Slick\WebStack\Domain\Security\SecurityException; |
|
17
|
|
|
use Slick\WebStack\Domain\Security\UserInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PassportMethodsTrait |
|
21
|
|
|
* |
|
22
|
|
|
* @package Slick\WebStack\Domain\Security\Http\Authenticator |
|
23
|
|
|
*/ |
|
24
|
|
|
trait PassportMethodsTrait |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
protected ?UserInterface $user = null; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array<BadgeInterface> */ |
|
30
|
|
|
protected array $badges = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The user that will be or was subject of authentication |
|
34
|
|
|
* |
|
35
|
|
|
* @return UserInterface |
|
36
|
|
|
* @throws SecurityException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function user(): UserInterface |
|
39
|
|
|
{ |
|
40
|
|
|
if (null === $this->user) { |
|
41
|
|
|
/** @phpstan-var UserBadge<UserInterface> $badge */ |
|
42
|
|
|
$badge = $this->badge(UserBadge::class); |
|
43
|
|
|
$this->user = $badge->user(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->user; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Adds a new security badge. |
|
51
|
|
|
* |
|
52
|
|
|
* A passport can hold only one instance of the same security badge. |
|
53
|
|
|
* This method replaces the current badge if it is already set on this |
|
54
|
|
|
* passport. |
|
55
|
|
|
* |
|
56
|
|
|
* @param BadgeInterface $badge |
|
57
|
|
|
* @param string|null $badgeName A FQCN to which the badge should be mapped to. |
|
58
|
|
|
* This allows replacing a built-in badge by a custom one using |
|
59
|
|
|
* e.g. addBadge(new MyCustomUserBadge(), UserBadge::class) |
|
60
|
|
|
* |
|
61
|
|
|
* @return static |
|
62
|
|
|
*/ |
|
63
|
|
|
public function addBadge(BadgeInterface $badge, ?string $badgeName = null): static |
|
64
|
|
|
{ |
|
65
|
|
|
$badgeName ??= $badge::class; |
|
66
|
|
|
$this->badges[$badgeName] = $badge; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if a badge exists. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $badgeName The name of the badge to check. |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool Returns true if the badge exists, false otherwise. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function hasBadge(string $badgeName): bool |
|
78
|
|
|
{ |
|
79
|
|
|
return array_key_exists($badgeName, $this->badges); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the badge with the given name. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $badgeName The name of the badge. |
|
86
|
|
|
* @return BadgeInterface|null The BadgeInterface instance if found; otherwise null. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function badge(string $badgeName): ?BadgeInterface |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->badges[$badgeName] ?? null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array<class-string<BadgeInterface>, BadgeInterface> |
|
|
|
|
|
|
95
|
|
|
*/ |
|
96
|
|
|
public function badges(): array |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->badges; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|