|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace App\Entity; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
7
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
8
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
9
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
|
10
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
11
|
|
|
use JMS\Serializer\Annotation\Exclude; |
|
12
|
|
|
use JMS\Serializer\Annotation\Expose; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") |
|
16
|
|
|
* @ORM\Table(name="users") |
|
17
|
|
|
* @UniqueEntity(fields="email", message="Email already taken") |
|
18
|
|
|
* @UniqueEntity(fields="username", message="Username already taken") |
|
19
|
|
|
*/ |
|
20
|
|
|
class User implements UserInterface, \Serializable |
|
21
|
|
|
{ |
|
22
|
|
|
const ROLE_USER = 'ROLE_USER'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @ORM\Id() |
|
26
|
|
|
* @ORM\GeneratedValue() |
|
27
|
|
|
* @ORM\Column(type="integer") |
|
28
|
|
|
*/ |
|
29
|
|
|
private $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
|
33
|
|
|
* @Assert\NotBlank() |
|
34
|
|
|
* @Assert\Email() |
|
35
|
|
|
*/ |
|
36
|
|
|
public $email; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @ORM\Column(type="string", length=255, unique=true) |
|
40
|
|
|
* @Assert\NotBlank() |
|
41
|
|
|
*/ |
|
42
|
|
|
public $username; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @ORM\Column(type="string", length=255, nullable=true) |
|
46
|
|
|
*/ |
|
47
|
|
|
public $roles; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @Exclude |
|
51
|
|
|
* @Assert\NotBlank() |
|
52
|
|
|
* @Assert\Length(max=4096) |
|
53
|
|
|
*/ |
|
54
|
|
|
public $plainPassword; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @Exclude |
|
58
|
|
|
* @ORM\Column(type="string", length=64) |
|
59
|
|
|
*/ |
|
60
|
|
|
private $password; |
|
61
|
|
|
|
|
62
|
10 |
|
public function __construct() |
|
63
|
|
|
{ |
|
64
|
10 |
|
$this->addRole(self::ROLE_USER); |
|
65
|
10 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function getId(): ?int |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->id; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
10 |
|
public function addRole(string $role): self |
|
73
|
|
|
{ |
|
74
|
10 |
|
if (array_search($role, $this->getRoles()) === false) { |
|
75
|
3 |
|
return $this->setRoles( |
|
76
|
3 |
|
array_merge($this->getRoles(), [$role]) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
10 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function removeRole(string $role): self |
|
84
|
|
|
{ |
|
85
|
1 |
|
$roles = $this->getRoles(); |
|
86
|
1 |
|
$foundedRoleKey = array_search($role, $roles); |
|
87
|
|
|
|
|
88
|
1 |
|
if ($foundedRoleKey !== false) { |
|
89
|
1 |
|
unset($roles[$foundedRoleKey]); |
|
90
|
1 |
|
return $this->setRoles($roles); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
private function setRoles(array $roles): self |
|
97
|
|
|
{ |
|
98
|
3 |
|
if (!count($roles)) { |
|
99
|
1 |
|
$this->roles = null; |
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
$this->roles = json_encode($roles); |
|
104
|
|
|
|
|
105
|
3 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
10 |
|
public function getRoles(): array |
|
109
|
|
|
{ |
|
110
|
10 |
|
if (!$this->roles) { |
|
111
|
10 |
|
return $this->getDefaultRoles(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
3 |
|
$roles = (array)json_decode($this->roles); |
|
115
|
|
|
|
|
116
|
3 |
|
if (!count($roles)) { |
|
117
|
|
|
return $this->getDefaultRoles(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
return array_values($roles); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
10 |
|
private function getDefaultRoles() |
|
124
|
|
|
{ |
|
125
|
10 |
|
return [self::ROLE_USER]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
2 |
|
public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self |
|
129
|
|
|
{ |
|
130
|
2 |
|
$this->plainPassword = $password; |
|
131
|
2 |
|
$this->password = $passwordEncoder->encodePassword($this, $this->plainPassword); |
|
132
|
|
|
|
|
133
|
2 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool |
|
137
|
|
|
{ |
|
138
|
1 |
|
return $passwordEncoder->isPasswordValid($this, $password); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
public function getPassword(): ?string |
|
142
|
|
|
{ |
|
143
|
1 |
|
return $this->password; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
3 |
|
public function getSalt(): ?string |
|
147
|
|
|
{ |
|
148
|
3 |
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
3 |
|
public function getUsername(): ?string |
|
152
|
|
|
{ |
|
153
|
3 |
|
return $this->username; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
public function eraseCredentials(): self |
|
157
|
|
|
{ |
|
158
|
1 |
|
$this->plainPassword = null; |
|
159
|
|
|
|
|
160
|
1 |
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// todo remove apiKey from 2 methods below |
|
164
|
1 |
|
public function serialize(): string |
|
165
|
|
|
{ |
|
166
|
1 |
|
return serialize([ |
|
167
|
1 |
|
$this->id, |
|
168
|
1 |
|
$this->email, |
|
169
|
1 |
|
$this->username, |
|
170
|
1 |
|
$this->roles, |
|
171
|
|
|
]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
public function unserialize($serialized): self |
|
175
|
|
|
{ |
|
176
|
|
|
list ( |
|
177
|
1 |
|
$this->id, |
|
178
|
1 |
|
$this->email, |
|
179
|
1 |
|
$this->username, |
|
180
|
1 |
|
$this->roles, |
|
181
|
1 |
|
) = unserialize($serialized); |
|
182
|
|
|
|
|
183
|
1 |
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
} |