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