1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
7
|
|
|
|
8
|
|
|
trait UserTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @ORM\Column(type="datetime", nullable=true) |
12
|
|
|
*/ |
13
|
|
|
protected $createdAt; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
* @ORM\Column(type="string", length=180, unique=true) |
18
|
|
|
* @Assert\Email( |
19
|
|
|
* message="user.email.invalid", |
20
|
|
|
* mode="strict" |
21
|
|
|
* ) |
22
|
|
|
*/ |
23
|
|
|
protected $email; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Loaded From BaseUser. |
27
|
|
|
* |
28
|
|
|
* @Assert\Length( |
29
|
|
|
* min=7, |
30
|
|
|
* max=100, |
31
|
|
|
* minMessage="user.password.short" |
32
|
|
|
* ) |
33
|
|
|
*/ |
34
|
|
|
protected $plainPassword; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @ORM\Column(type="json") |
38
|
|
|
*/ |
39
|
|
|
private $roles = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string The hashed password |
43
|
|
|
* @ORM\Column(type="string", nullable=true) |
44
|
|
|
*/ |
45
|
|
|
private $password; |
46
|
|
|
|
47
|
|
|
public function __toString() |
48
|
|
|
{ |
49
|
|
|
return $this->email; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getId(): ?int |
53
|
|
|
{ |
54
|
|
|
return $this->id; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getEmail(): ?string |
58
|
|
|
{ |
59
|
|
|
return $this->email; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setEmail(string $email): self |
63
|
|
|
{ |
64
|
|
|
$this->email = $email; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setPlainPassword($password) |
70
|
|
|
{ |
71
|
|
|
$this->plainPassword = $password; |
72
|
|
|
$this->password = ''; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getPlainPassword() |
78
|
|
|
{ |
79
|
|
|
return $this->plainPassword; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* A visual identifier that represents this user. |
84
|
|
|
* |
85
|
|
|
* @see UserInterface |
86
|
|
|
*/ |
87
|
|
|
public function getUsername(): string |
88
|
|
|
{ |
89
|
|
|
return (string) $this->email; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @see UserInterface |
94
|
|
|
*/ |
95
|
|
|
public function getRoles() |
96
|
|
|
{ |
97
|
|
|
$roles = $this->roles; |
98
|
|
|
// guarantee every user at least has ROLE_USER |
99
|
|
|
$roles[] = static::ROLE_DEFAULT; |
100
|
|
|
|
101
|
|
|
return array_unique($roles); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setRoles(array $roles): self |
105
|
|
|
{ |
106
|
|
|
$this->roles = $roles; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function hasRole($role) |
112
|
|
|
{ |
113
|
|
|
return \in_array(strtoupper($role), $this->getRoles(), true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
public function __construct() |
118
|
|
|
{ |
119
|
|
|
$this->roles = []; |
120
|
|
|
} |
121
|
|
|
public function addRole($role) |
122
|
|
|
{ |
123
|
|
|
$role = strtoupper($role); |
124
|
|
|
if ($role === static::ROLE_DEFAULT) { |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (!in_array($role, $this->roles, true)) { |
129
|
|
|
$this->roles[] = $role; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
public function removeRole($role) |
137
|
|
|
{ |
138
|
|
|
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { |
139
|
|
|
unset($this->roles[$key]); |
140
|
|
|
$this->roles = array_values($this->roles); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
}/**/ |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @see UserInterface |
148
|
|
|
*/ |
149
|
|
|
public function getPassword(): string |
150
|
|
|
{ |
151
|
|
|
return (string) $this->password; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setPassword(string $password): self |
155
|
|
|
{ |
156
|
|
|
$this->password = $password; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @see UserInterface |
163
|
|
|
*/ |
164
|
|
|
public function getSalt() |
165
|
|
|
{ |
166
|
|
|
// not needed when using the "bcrypt" algorithm in security.yaml |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @see UserInterface |
171
|
|
|
*/ |
172
|
|
|
public function eraseCredentials() |
173
|
|
|
{ |
174
|
|
|
$this->plainPassword = null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @ORM\PrePersist |
179
|
|
|
*/ |
180
|
|
|
public function updatedTimestamps(): self |
181
|
|
|
{ |
182
|
|
|
$this->setCreatedAt(new \DateTime('now')); |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function getCreatedAt(): ?\DateTimeInterface |
188
|
|
|
{ |
189
|
|
|
return $this->createdAt; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function setCreatedAt(\DateTimeInterface $createdAt): self |
193
|
|
|
{ |
194
|
|
|
$this->createdAt = $createdAt; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|