1 | <?php |
||
2 | |||
3 | namespace Smart\AuthenticationBundle\Entity\User; |
||
4 | |||
5 | use DateTime; |
||
6 | use Doctrine\ORM\Mapping as ORM; |
||
7 | use Symfony\Component\Validator\Constraints as Assert; |
||
8 | |||
9 | /** |
||
10 | * Basic methods to implements Symfony\Component\Security\Core\User\UserInterface |
||
11 | * |
||
12 | * @author Nicolas Bastien <[email protected]> |
||
13 | */ |
||
14 | trait UserTrait |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | * |
||
19 | * @ORM\Column(type="string", length=255, nullable=false, unique=true) |
||
20 | * |
||
21 | * @Assert\NotBlank |
||
22 | * @Assert\Email |
||
23 | */ |
||
24 | private $email; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | * |
||
29 | * @ORM\Column(name="password", type="string", length=100, nullable=false) |
||
30 | */ |
||
31 | private $password; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $plainPassword; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | * |
||
41 | * @ORM\Column(type="string", length=255, nullable=true) |
||
42 | */ |
||
43 | protected $firstName; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | * |
||
48 | * @ORM\Column(type="string", length=255, nullable=true) |
||
49 | */ |
||
50 | protected $lastName; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | * |
||
55 | * @ORM\Column(type="json_array") |
||
56 | */ |
||
57 | private $roles; |
||
58 | |||
59 | /** |
||
60 | * @var DateTime |
||
61 | * |
||
62 | * @ORM\Column(type="datetime", nullable=true) |
||
63 | */ |
||
64 | protected $lastLogin; |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function __toString() |
||
70 | { |
||
71 | if (strlen(trim($this->getFullName())) > 0) { |
||
72 | return (string) $this->getFullName(); |
||
73 | } |
||
74 | |||
75 | return (string) $this->getEmail(); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * On list we want to be able to sort by Lastname |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getListDisplay() |
||
83 | { |
||
84 | if (strlen(trim($this->getLastName())) > 0) { |
||
85 | return (string) $this->getListFullName(); |
||
86 | } |
||
87 | |||
88 | return (string) $this->getEmail(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return int |
||
93 | */ |
||
94 | public function getId() |
||
95 | { |
||
96 | return $this->id; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | public function getUsername() |
||
103 | { |
||
104 | return $this->email; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | public function getEmail() |
||
111 | { |
||
112 | return $this->email; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param string $email |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setEmail($email) |
||
121 | { |
||
122 | $this->email = strtolower($email); |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | public function getPassword() |
||
131 | { |
||
132 | return $this->password; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param string $password |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setPassword($password) |
||
141 | { |
||
142 | $this->password = $password; |
||
143 | |||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | public function getSalt() |
||
151 | { |
||
152 | return null; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getPlainPassword() |
||
159 | { |
||
160 | return $this->plainPassword; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $plainPassword |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setPlainPassword($plainPassword) |
||
169 | { |
||
170 | $this->plainPassword = $plainPassword; |
||
171 | |||
172 | return $this; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @inheritdoc |
||
177 | */ |
||
178 | public function eraseCredentials() |
||
179 | { |
||
180 | $this->plainPassword = null; |
||
181 | |||
182 | return; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | public function getRoles() |
||
189 | { |
||
190 | return $this->roles; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param array $roles |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setRoles(array $roles = []) |
||
199 | { |
||
200 | $this->roles = $roles; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getFirstName() |
||
209 | { |
||
210 | return $this->firstName; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param string $firstName |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setFirstName($firstName) |
||
219 | { |
||
220 | $this->firstName = $firstName; |
||
221 | |||
222 | return $this; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getLastName() |
||
229 | { |
||
230 | return $this->lastName; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param string $lastName |
||
235 | * |
||
236 | * @return $this |
||
237 | */ |
||
238 | public function setLastName($lastName) |
||
239 | { |
||
240 | $this->lastName = $lastName; |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getFullName() |
||
249 | { |
||
250 | return sprintf('%s %s', (string)$this->getFirstName(), (string)$this->getLastName()); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getListFullName() |
||
257 | { |
||
258 | return sprintf('%s %s', (string)$this->getLastName(), (string)$this->getFirstName()); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getFullNameAndEmail() |
||
265 | { |
||
266 | if (strlen(trim($this->getFullName())) > 0) { |
||
267 | return sprintf('%s - %s', $this->getFullName(), $this->getEmail()); |
||
268 | } |
||
269 | |||
270 | return $this->getEmail(); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @see \Serializable::serialize() |
||
275 | */ |
||
276 | public function serialize() |
||
277 | { |
||
278 | return serialize(array( |
||
279 | $this->id, |
||
280 | $this->email, |
||
281 | $this->password, |
||
282 | )); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @see \Serializable::unserialize() |
||
287 | */ |
||
288 | public function unserialize($serialized) |
||
289 | { |
||
290 | list ( |
||
291 | $this->id, |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
292 | $this->email, |
||
293 | $this->password, |
||
294 | // see section on salt below |
||
295 | // $this->salt |
||
296 | ) = unserialize($serialized); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @return null|DateTime |
||
301 | */ |
||
302 | public function getLastLogin() |
||
303 | { |
||
304 | return $this->lastLogin; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param null|DateTime $lastLogin |
||
309 | * |
||
310 | * @return $this |
||
311 | */ |
||
312 | public function setLastLogin($lastLogin = null) |
||
313 | { |
||
314 | $this->lastLogin = $lastLogin; |
||
315 | |||
316 | return $this; |
||
317 | } |
||
318 | } |
||
319 |