1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Auth\Authentication; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Component\User\Domain\User\User; |
18
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
19
|
|
|
use Acme\PhpExtension\ConstructableFromArrayInterface; |
20
|
|
|
use Acme\PhpExtension\ConstructableFromArrayTrait; |
21
|
|
|
use Symfony\Component\Security\Core\User\EquatableInterface; |
22
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
23
|
|
|
|
24
|
|
|
final class SecurityUser implements UserInterface, EquatableInterface, ConstructableFromArrayInterface |
25
|
|
|
{ |
26
|
|
|
use ConstructableFromArrayTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var UserId |
30
|
|
|
*/ |
31
|
|
|
private $userId; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $username; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $password; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string[] |
45
|
|
|
*/ |
46
|
|
|
private $roles; |
47
|
|
|
|
48
|
|
|
public function __construct(UserId $userId, string $username, string $password, array $roles = []) |
49
|
|
|
{ |
50
|
|
|
$this->userId = $userId; |
51
|
|
|
$this->username = $username; |
52
|
|
|
$this->password = $password; |
53
|
|
|
$this->roles = $roles; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function fromUser(User $user): self |
57
|
|
|
{ |
58
|
|
|
return new self($user->getId(), $user->getUsername(), $user->getPassword() ?? '', $user->getRoles()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getUserId(): UserId |
62
|
|
|
{ |
63
|
|
|
return $this->userId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getUsername(): string |
67
|
|
|
{ |
68
|
|
|
return $this->username; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getPassword(): string |
72
|
|
|
{ |
73
|
|
|
return $this->password; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string[] |
78
|
|
|
*/ |
79
|
|
|
public function getRoles(): array |
80
|
|
|
{ |
81
|
|
|
return $this->roles; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the salt that was originally used to encode the password. |
86
|
|
|
* |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function getSalt(): ?string |
90
|
|
|
{ |
91
|
|
|
// See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html |
92
|
|
|
// we're using bcrypt in security.yml to encode the password, so |
93
|
|
|
// the salt value is built-in and you don't have to generate one |
94
|
|
|
|
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Removes sensitive data from the user. |
100
|
|
|
* |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function eraseCredentials(): void |
104
|
|
|
{ |
105
|
|
|
// if you had a plainPassword property, you'd nullify it here |
106
|
|
|
// $this->plainPassword = null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function isEqualTo(UserInterface $user): bool |
110
|
|
|
{ |
111
|
|
|
if (!$user instanceof self) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->password !== $user->getPassword()) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($this->getSalt() !== $user->getSalt()) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($this->username !== $user->getUsername()) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|