]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Entity; | |
4 | ||
5 | use Doctrine\Common\Collections\ArrayCollection; | |
6 | use Doctrine\ORM\Mapping as ORM; | |
7 | use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | |
8 | use Symfony\Component\Security\Core\User\UserInterface; | |
9 | use Symfony\Component\Security\Core\User\AdvancedUserInterface; | |
10 | use Symfony\Component\Validator\Constraints as Assert; | |
11 | use JMS\Serializer\Annotation\ExclusionPolicy; | |
12 | use JMS\Serializer\Annotation\Expose; | |
13 | ||
14 | /** | |
15 | * User. | |
16 | * | |
17 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository") | |
18 | * @ORM\Table | |
19 | * @ORM\HasLifecycleCallbacks() | |
20 | * @ExclusionPolicy("all") | |
21 | * | |
22 | * @UniqueEntity("email") | |
23 | * @UniqueEntity("username") | |
24 | */ | |
25 | class User implements AdvancedUserInterface, \Serializable | |
26 | { | |
27 | /** | |
28 | * @var int | |
29 | * | |
30 | * @Expose | |
31 | * @ORM\Column(name="id", type="integer") | |
32 | * @ORM\Id | |
33 | * @ORM\GeneratedValue(strategy="AUTO") | |
34 | */ | |
35 | private $id; | |
36 | ||
37 | /** | |
38 | * @var string | |
39 | * | |
40 | * @ORM\Column(name="username", type="text") | |
41 | * @Assert\NotBlank() | |
42 | * @Assert\Length( | |
43 | * min = "3", | |
44 | * max = "255" | |
45 | * ) | |
46 | */ | |
47 | private $username; | |
48 | ||
49 | /** | |
50 | * @var string | |
51 | * | |
52 | * @ORM\Column(type="string", length=32) | |
53 | */ | |
54 | private $salt; | |
55 | ||
56 | /** | |
57 | * @var string | |
58 | * | |
59 | * @ORM\Column(name="password", type="text") | |
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 | * | |
73 | * @ORM\Column(name="email", type="text", nullable=false) | |
74 | * @Assert\Email() | |
75 | * @Assert\NotBlank() | |
76 | */ | |
77 | private $email; | |
78 | ||
79 | /** | |
80 | * @ORM\Column(name="is_active", type="boolean", nullable=false) | |
81 | */ | |
82 | private $isActive = true; | |
83 | ||
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 | ||
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 | ||
108 | /** | |
109 | * @ORM\OneToMany(targetEntity="Entry", mappedBy="user", cascade={"remove"}) | |
110 | */ | |
111 | private $entries; | |
112 | ||
113 | /** | |
114 | * @ORM\OneToOne(targetEntity="Config", mappedBy="user") | |
115 | */ | |
116 | private $config; | |
117 | ||
118 | /** | |
119 | * @ORM\OneToMany(targetEntity="Tag", mappedBy="user", cascade={"remove"}) | |
120 | */ | |
121 | private $tags; | |
122 | ||
123 | public function __construct() | |
124 | { | |
125 | $this->isActive = true; | |
126 | $this->salt = md5(uniqid(null, true)); | |
127 | $this->entries = new ArrayCollection(); | |
128 | $this->tags = new ArrayCollection(); | |
129 | } | |
130 | ||
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 | ||
144 | /** | |
145 | * Get id. | |
146 | * | |
147 | * @return int | |
148 | */ | |
149 | public function getId() | |
150 | { | |
151 | return $this->id; | |
152 | } | |
153 | ||
154 | /** | |
155 | * Set username. | |
156 | * | |
157 | * @param string $username | |
158 | * | |
159 | * @return User | |
160 | */ | |
161 | public function setUsername($username) | |
162 | { | |
163 | $this->username = $username; | |
164 | ||
165 | return $this; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Get username. | |
170 | * | |
171 | * @return string | |
172 | */ | |
173 | public function getUsername() | |
174 | { | |
175 | return $this->username; | |
176 | } | |
177 | ||
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 | ||
194 | /** | |
195 | * Set password. | |
196 | * | |
197 | * @param string $password | |
198 | * | |
199 | * @return User | |
200 | */ | |
201 | public function setPassword($password) | |
202 | { | |
203 | if (!$password && 0 === strlen($password)) { | |
204 | return; | |
205 | } | |
206 | ||
207 | $this->password = sha1($password.$this->getUsername().$this->getSalt()); | |
208 | ||
209 | return $this; | |
210 | } | |
211 | ||
212 | /** | |
213 | * Get password. | |
214 | * | |
215 | * @return string | |
216 | */ | |
217 | public function getPassword() | |
218 | { | |
219 | return $this->password; | |
220 | } | |
221 | ||
222 | /** | |
223 | * Set name. | |
224 | * | |
225 | * @param string $name | |
226 | * | |
227 | * @return User | |
228 | */ | |
229 | public function setName($name) | |
230 | { | |
231 | $this->name = $name; | |
232 | ||
233 | return $this; | |
234 | } | |
235 | ||
236 | /** | |
237 | * Get name. | |
238 | * | |
239 | * @return string | |
240 | */ | |
241 | public function getName() | |
242 | { | |
243 | return $this->name; | |
244 | } | |
245 | ||
246 | /** | |
247 | * Set email. | |
248 | * | |
249 | * @param string $email | |
250 | * | |
251 | * @return User | |
252 | */ | |
253 | public function setEmail($email) | |
254 | { | |
255 | $this->email = $email; | |
256 | ||
257 | return $this; | |
258 | } | |
259 | ||
260 | /** | |
261 | * Get email. | |
262 | * | |
263 | * @return string | |
264 | */ | |
265 | public function getEmail() | |
266 | { | |
267 | return $this->email; | |
268 | } | |
269 | ||
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 | ||
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 | ||
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 | } | |
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 | { | |
347 | list( | |
348 | $this->id) = unserialize($serialized); | |
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 | } | |
375 | /** | |
376 | * Set config. | |
377 | * | |
378 | * @param \Wallabag\CoreBundle\Entity\Config $config | |
379 | * | |
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 | /** | |
390 | * Get config. | |
391 | * | |
392 | * @return \Wallabag\CoreBundle\Entity\Config | |
393 | */ | |
394 | public function getConfig() | |
395 | { | |
396 | return $this->config; | |
397 | } | |
398 | ||
399 | /** | |
400 | * Set confirmationToken. | |
401 | * | |
402 | * @param string $confirmationToken | |
403 | * | |
404 | * @return User | |
405 | */ | |
406 | public function setConfirmationToken($confirmationToken) | |
407 | { | |
408 | $this->confirmationToken = $confirmationToken; | |
409 | ||
410 | return $this; | |
411 | } | |
412 | ||
413 | /** | |
414 | * Get confirmationToken. | |
415 | * | |
416 | * @return string | |
417 | */ | |
418 | public function getConfirmationToken() | |
419 | { | |
420 | return $this->confirmationToken; | |
421 | } | |
422 | ||
423 | /** | |
424 | * Set passwordRequestedAt. | |
425 | * | |
426 | * @param \DateTime $passwordRequestedAt | |
427 | * | |
428 | * @return User | |
429 | */ | |
430 | public function setPasswordRequestedAt($passwordRequestedAt) | |
431 | { | |
432 | $this->passwordRequestedAt = $passwordRequestedAt; | |
433 | ||
434 | return $this; | |
435 | } | |
436 | ||
437 | /** | |
438 | * Get passwordRequestedAt. | |
439 | * | |
440 | * @return \DateTime | |
441 | */ | |
442 | public function getPasswordRequestedAt() | |
443 | { | |
444 | return $this->passwordRequestedAt; | |
445 | } | |
446 | } |