]>
Commit | Line | Data |
---|---|---|
9d50517c NL |
1 | <?php |
2 | ||
ad4d1caa | 3 | namespace Wallabag\CoreBundle\Entity; |
9d50517c | 4 | |
5f09650e | 5 | use Doctrine\Common\Collections\ArrayCollection; |
9d50517c | 6 | use Doctrine\ORM\Mapping as ORM; |
c844dc0c | 7 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; |
c3235553 | 8 | use Symfony\Component\Security\Core\User\UserInterface; |
c3235553 | 9 | use Symfony\Component\Security\Core\User\AdvancedUserInterface; |
c0d9eba0 | 10 | use Symfony\Component\Validator\Constraints as Assert; |
0f006880 NL |
11 | use JMS\Serializer\Annotation\ExclusionPolicy; |
12 | use JMS\Serializer\Annotation\Expose; | |
9d50517c NL |
13 | |
14 | /** | |
4346a860 | 15 | * User. |
9d50517c | 16 | * |
0c83fd59 | 17 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository") |
164bd801 | 18 | * @ORM\Table |
2f69eb4a | 19 | * @ORM\HasLifecycleCallbacks() |
0f006880 | 20 | * @ExclusionPolicy("all") |
c844dc0c J |
21 | * |
22 | * @UniqueEntity("email") | |
23 | * @UniqueEntity("username") | |
9d50517c | 24 | */ |
2f69eb4a | 25 | class User implements AdvancedUserInterface, \Serializable |
9d50517c NL |
26 | { |
27 | /** | |
4346a860 | 28 | * @var int |
9d50517c | 29 | * |
0f006880 | 30 | * @Expose |
2f69eb4a | 31 | * @ORM\Column(name="id", type="integer") |
9d50517c | 32 | * @ORM\Id |
2f69eb4a | 33 | * @ORM\GeneratedValue(strategy="AUTO") |
9d50517c NL |
34 | */ |
35 | private $id; | |
36 | ||
37 | /** | |
38 | * @var string | |
39 | * | |
2f69eb4a | 40 | * @ORM\Column(name="username", type="text") |
c0d9eba0 J |
41 | * @Assert\NotBlank() |
42 | * @Assert\Length( | |
43 | * min = "3", | |
44 | * max = "255" | |
45 | * ) | |
9d50517c NL |
46 | */ |
47 | private $username; | |
48 | ||
c3235553 | 49 | /** |
2f69eb4a NL |
50 | * @var string |
51 | * | |
c3235553 NL |
52 | * @ORM\Column(type="string", length=32) |
53 | */ | |
54 | private $salt; | |
55 | ||
9d50517c NL |
56 | /** |
57 | * @var string | |
58 | * | |
2f69eb4a | 59 | * @ORM\Column(name="password", type="text") |
9d50517c NL |
60 | */ |
61 | private $password; | |
62 | ||
63 | /** | |
64 | * @var string | |
65 | * | |
66 | * @ORM\Column(name="name", type="text", nullable=true) | |
67 | */ | |
68 | private $name; | |
69 | ||
70 | /** | |
71 | * @var string | |
72 | * | |
c0d9eba0 J |
73 | * @ORM\Column(name="email", type="text", nullable=false) |
74 | * @Assert\Email() | |
75 | * @Assert\NotBlank() | |
9d50517c NL |
76 | */ |
77 | private $email; | |
78 | ||
c3235553 | 79 | /** |
c0d9eba0 | 80 | * @ORM\Column(name="is_active", type="boolean", nullable=false) |
c3235553 | 81 | */ |
c0d9eba0 | 82 | private $isActive = true; |
9d50517c | 83 | |
6894d48e J |
84 | /** |
85 | * @ORM\Column(name="confirmation_token", type="string", nullable=true) | |
86 | */ | |
87 | private $confirmationToken; | |
88 | ||
89 | /** | |
90 | * @ORM\Column(name="password_requested_at", type="datetime", nullable=true) | |
91 | */ | |
92 | private $passwordRequestedAt; | |
93 | ||
2f69eb4a NL |
94 | /** |
95 | * @var date | |
96 | * | |
97 | * @ORM\Column(name="created_at", type="datetime") | |
98 | */ | |
99 | private $createdAt; | |
100 | ||
101 | /** | |
102 | * @var date | |
103 | * | |
104 | * @ORM\Column(name="updated_at", type="datetime") | |
105 | */ | |
106 | private $updatedAt; | |
107 | ||
5f09650e NL |
108 | /** |
109 | * @ORM\OneToMany(targetEntity="Entry", mappedBy="user", cascade={"remove"}) | |
110 | */ | |
111 | private $entries; | |
112 | ||
32da2a70 J |
113 | /** |
114 | * @ORM\OneToOne(targetEntity="Config", mappedBy="user") | |
115 | */ | |
116 | private $config; | |
117 | ||
092ca707 NL |
118 | /** |
119 | * @ORM\OneToMany(targetEntity="Tag", mappedBy="user", cascade={"remove"}) | |
120 | */ | |
121 | private $tags; | |
122 | ||
c3235553 NL |
123 | public function __construct() |
124 | { | |
092ca707 NL |
125 | $this->isActive = true; |
126 | $this->salt = md5(uniqid(null, true)); | |
127 | $this->entries = new ArrayCollection(); | |
128 | $this->tags = new ArrayCollection(); | |
c3235553 | 129 | } |
9d50517c | 130 | |
2f69eb4a NL |
131 | /** |
132 | * @ORM\PrePersist | |
133 | * @ORM\PreUpdate | |
134 | */ | |
135 | public function timestamps() | |
136 | { | |
137 | if (is_null($this->createdAt)) { | |
138 | $this->createdAt = new \DateTime(); | |
139 | } | |
140 | ||
141 | $this->updatedAt = new \DateTime(); | |
142 | } | |
143 | ||
9d50517c | 144 | /** |
4346a860 | 145 | * Get id. |
9d50517c | 146 | * |
4346a860 | 147 | * @return int |
9d50517c NL |
148 | */ |
149 | public function getId() | |
150 | { | |
151 | return $this->id; | |
152 | } | |
153 | ||
154 | /** | |
4346a860 JB |
155 | * Set username. |
156 | * | |
157 | * @param string $username | |
9d50517c | 158 | * |
2f69eb4a | 159 | * @return User |
9d50517c NL |
160 | */ |
161 | public function setUsername($username) | |
162 | { | |
163 | $this->username = $username; | |
164 | ||
165 | return $this; | |
166 | } | |
167 | ||
168 | /** | |
4346a860 | 169 | * Get username. |
9d50517c | 170 | * |
7df80cb3 | 171 | * @return string |
9d50517c NL |
172 | */ |
173 | public function getUsername() | |
174 | { | |
175 | return $this->username; | |
176 | } | |
177 | ||
c3235553 NL |
178 | /** |
179 | * @inheritDoc | |
180 | */ | |
181 | public function getSalt() | |
182 | { | |
183 | return $this->salt; | |
184 | } | |
185 | ||
186 | /** | |
187 | * @inheritDoc | |
188 | */ | |
189 | public function getRoles() | |
190 | { | |
191 | return array('ROLE_USER'); | |
192 | } | |
193 | ||
9d50517c | 194 | /** |
4346a860 JB |
195 | * Set password. |
196 | * | |
197 | * @param string $password | |
9d50517c | 198 | * |
2f69eb4a | 199 | * @return User |
9d50517c NL |
200 | */ |
201 | public function setPassword($password) | |
202 | { | |
d9169157 J |
203 | if (!$password && 0 === strlen($password)) { |
204 | return; | |
205 | } | |
206 | ||
207 | $this->password = sha1($password.$this->getUsername().$this->getSalt()); | |
9d50517c NL |
208 | |
209 | return $this; | |
210 | } | |
211 | ||
212 | /** | |
4346a860 | 213 | * Get password. |
9d50517c | 214 | * |
7df80cb3 | 215 | * @return string |
9d50517c NL |
216 | */ |
217 | public function getPassword() | |
218 | { | |
219 | return $this->password; | |
220 | } | |
221 | ||
222 | /** | |
4346a860 JB |
223 | * Set name. |
224 | * | |
225 | * @param string $name | |
9d50517c | 226 | * |
2f69eb4a | 227 | * @return User |
9d50517c NL |
228 | */ |
229 | public function setName($name) | |
230 | { | |
231 | $this->name = $name; | |
232 | ||
233 | return $this; | |
234 | } | |
235 | ||
236 | /** | |
4346a860 | 237 | * Get name. |
9d50517c | 238 | * |
7df80cb3 | 239 | * @return string |
9d50517c NL |
240 | */ |
241 | public function getName() | |
242 | { | |
243 | return $this->name; | |
244 | } | |
245 | ||
246 | /** | |
4346a860 JB |
247 | * Set email. |
248 | * | |
249 | * @param string $email | |
9d50517c | 250 | * |
2f69eb4a | 251 | * @return User |
9d50517c NL |
252 | */ |
253 | public function setEmail($email) | |
254 | { | |
255 | $this->email = $email; | |
256 | ||
257 | return $this; | |
258 | } | |
259 | ||
260 | /** | |
4346a860 | 261 | * Get email. |
9d50517c | 262 | * |
7df80cb3 | 263 | * @return string |
9d50517c NL |
264 | */ |
265 | public function getEmail() | |
266 | { | |
267 | return $this->email; | |
268 | } | |
c3235553 | 269 | |
2f69eb4a NL |
270 | /** |
271 | * @return string | |
272 | */ | |
273 | public function getCreatedAt() | |
274 | { | |
275 | return $this->createdAt; | |
276 | } | |
277 | ||
278 | /** | |
279 | * @return string | |
280 | */ | |
281 | public function getUpdatedAt() | |
282 | { | |
283 | return $this->updatedAt; | |
284 | } | |
285 | ||
5f09650e NL |
286 | /** |
287 | * @param Entry $entry | |
288 | * | |
289 | * @return User | |
290 | */ | |
291 | public function addEntry(Entry $entry) | |
292 | { | |
293 | $this->entries[] = $entry; | |
294 | ||
295 | return $this; | |
296 | } | |
297 | ||
298 | /** | |
299 | * @return ArrayCollection<Entry> | |
300 | */ | |
301 | public function getEntries() | |
302 | { | |
303 | return $this->entries; | |
304 | } | |
305 | ||
092ca707 NL |
306 | /** |
307 | * @param Entry $entry | |
308 | * | |
309 | * @return User | |
310 | */ | |
311 | public function addTag(Tag $tag) | |
312 | { | |
313 | $this->tags[] = $tag; | |
314 | ||
315 | return $this; | |
316 | } | |
317 | ||
318 | /** | |
319 | * @return ArrayCollection<Tag> | |
320 | */ | |
321 | public function getTags() | |
322 | { | |
323 | return $this->tags; | |
324 | } | |
c3235553 NL |
325 | /** |
326 | * @inheritDoc | |
327 | */ | |
328 | public function eraseCredentials() | |
329 | { | |
330 | } | |
331 | ||
332 | /** | |
333 | * @see \Serializable::serialize() | |
334 | */ | |
335 | public function serialize() | |
336 | { | |
337 | return serialize(array( | |
338 | $this->id, | |
339 | )); | |
340 | } | |
341 | ||
342 | /** | |
343 | * @see \Serializable::unserialize() | |
344 | */ | |
345 | public function unserialize($serialized) | |
346 | { | |
7df80cb3 | 347 | list( |
4346a860 | 348 | $this->id) = unserialize($serialized); |
c3235553 NL |
349 | } |
350 | ||
351 | public function isEqualTo(UserInterface $user) | |
352 | { | |
353 | return $this->username === $user->getUsername(); | |
354 | } | |
355 | ||
356 | public function isAccountNonExpired() | |
357 | { | |
358 | return true; | |
359 | } | |
360 | ||
361 | public function isAccountNonLocked() | |
362 | { | |
363 | return true; | |
364 | } | |
365 | ||
366 | public function isCredentialsNonExpired() | |
367 | { | |
368 | return true; | |
369 | } | |
370 | ||
371 | public function isEnabled() | |
372 | { | |
373 | return $this->isActive; | |
374 | } | |
32da2a70 | 375 | /** |
4346a860 JB |
376 | * Set config. |
377 | * | |
378 | * @param \Wallabag\CoreBundle\Entity\Config $config | |
32da2a70 | 379 | * |
32da2a70 J |
380 | * @return User |
381 | */ | |
382 | public function setConfig(\Wallabag\CoreBundle\Entity\Config $config = null) | |
383 | { | |
384 | $this->config = $config; | |
385 | ||
386 | return $this; | |
387 | } | |
388 | ||
389 | /** | |
4346a860 | 390 | * Get config. |
32da2a70 J |
391 | * |
392 | * @return \Wallabag\CoreBundle\Entity\Config | |
393 | */ | |
394 | public function getConfig() | |
395 | { | |
396 | return $this->config; | |
397 | } | |
6894d48e J |
398 | |
399 | /** | |
4346a860 JB |
400 | * Set confirmationToken. |
401 | * | |
402 | * @param string $confirmationToken | |
6894d48e | 403 | * |
6894d48e J |
404 | * @return User |
405 | */ | |
406 | public function setConfirmationToken($confirmationToken) | |
407 | { | |
408 | $this->confirmationToken = $confirmationToken; | |
409 | ||
410 | return $this; | |
411 | } | |
412 | ||
413 | /** | |
4346a860 | 414 | * Get confirmationToken. |
6894d48e J |
415 | * |
416 | * @return string | |
417 | */ | |
418 | public function getConfirmationToken() | |
419 | { | |
420 | return $this->confirmationToken; | |
421 | } | |
422 | ||
423 | /** | |
4346a860 JB |
424 | * Set passwordRequestedAt. |
425 | * | |
426 | * @param \DateTime $passwordRequestedAt | |
6894d48e | 427 | * |
6894d48e J |
428 | * @return User |
429 | */ | |
430 | public function setPasswordRequestedAt($passwordRequestedAt) | |
431 | { | |
432 | $this->passwordRequestedAt = $passwordRequestedAt; | |
433 | ||
434 | return $this; | |
435 | } | |
436 | ||
437 | /** | |
4346a860 | 438 | * Get passwordRequestedAt. |
6894d48e J |
439 | * |
440 | * @return \DateTime | |
441 | */ | |
442 | public function getPasswordRequestedAt() | |
443 | { | |
444 | return $this->passwordRequestedAt; | |
445 | } | |
9d50517c | 446 | } |