]>
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\Table(name="user") | |
18 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\UserRepository") | |
19 | * @ORM\HasLifecycleCallbacks() | |
20 | * @ExclusionPolicy("all") | |
21 | * | |
22 | * @UniqueEntity("email") | |
23 | * @UniqueEntity("username") | |
24 | */ | |
25 | class User implements AdvancedUserInterface, \Serializable | |
26 | { | |
27 | /** | |
28 | * @var integer | |
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 integer | |
148 | */ | |
149 | public function getId() | |
150 | { | |
151 | return $this->id; | |
152 | } | |
153 | ||
154 | /** | |
155 | * Set username | |
156 | * | |
157 | * @param string $username | |
158 | * @return User | |
159 | */ | |
160 | public function setUsername($username) | |
161 | { | |
162 | $this->username = $username; | |
163 | ||
164 | return $this; | |
165 | } | |
166 | ||
167 | /** | |
168 | * Get username | |
169 | * | |
170 | * @return string | |
171 | */ | |
172 | public function getUsername() | |
173 | { | |
174 | return $this->username; | |
175 | } | |
176 | ||
177 | /** | |
178 | * @inheritDoc | |
179 | */ | |
180 | public function getSalt() | |
181 | { | |
182 | return $this->salt; | |
183 | } | |
184 | ||
185 | /** | |
186 | * @inheritDoc | |
187 | */ | |
188 | public function getRoles() | |
189 | { | |
190 | return array('ROLE_USER'); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Set password | |
195 | * | |
196 | * @param string $password | |
197 | * @return User | |
198 | */ | |
199 | public function setPassword($password) | |
200 | { | |
201 | if (!$password && 0 === strlen($password)) { | |
202 | return; | |
203 | } | |
204 | ||
205 | $this->password = sha1($password.$this->getUsername().$this->getSalt()); | |
206 | ||
207 | return $this; | |
208 | } | |
209 | ||
210 | /** | |
211 | * Get password | |
212 | * | |
213 | * @return string | |
214 | */ | |
215 | public function getPassword() | |
216 | { | |
217 | return $this->password; | |
218 | } | |
219 | ||
220 | /** | |
221 | * Set name | |
222 | * | |
223 | * @param string $name | |
224 | * @return User | |
225 | */ | |
226 | public function setName($name) | |
227 | { | |
228 | $this->name = $name; | |
229 | ||
230 | return $this; | |
231 | } | |
232 | ||
233 | /** | |
234 | * Get name | |
235 | * | |
236 | * @return string | |
237 | */ | |
238 | public function getName() | |
239 | { | |
240 | return $this->name; | |
241 | } | |
242 | ||
243 | /** | |
244 | * Set email | |
245 | * | |
246 | * @param string $email | |
247 | * @return User | |
248 | */ | |
249 | public function setEmail($email) | |
250 | { | |
251 | $this->email = $email; | |
252 | ||
253 | return $this; | |
254 | } | |
255 | ||
256 | /** | |
257 | * Get email | |
258 | * | |
259 | * @return string | |
260 | */ | |
261 | public function getEmail() | |
262 | { | |
263 | return $this->email; | |
264 | } | |
265 | ||
266 | /** | |
267 | * @return string | |
268 | */ | |
269 | public function getCreatedAt() | |
270 | { | |
271 | return $this->createdAt; | |
272 | } | |
273 | ||
274 | /** | |
275 | * @return string | |
276 | */ | |
277 | public function getUpdatedAt() | |
278 | { | |
279 | return $this->updatedAt; | |
280 | } | |
281 | ||
282 | /** | |
283 | * @param Entry $entry | |
284 | * | |
285 | * @return User | |
286 | */ | |
287 | public function addEntry(Entry $entry) | |
288 | { | |
289 | $this->entries[] = $entry; | |
290 | ||
291 | return $this; | |
292 | } | |
293 | ||
294 | /** | |
295 | * @return ArrayCollection<Entry> | |
296 | */ | |
297 | public function getEntries() | |
298 | { | |
299 | return $this->entries; | |
300 | } | |
301 | ||
302 | /** | |
303 | * @param Entry $entry | |
304 | * | |
305 | * @return User | |
306 | */ | |
307 | public function addTag(Tag $tag) | |
308 | { | |
309 | $this->tags[] = $tag; | |
310 | ||
311 | return $this; | |
312 | } | |
313 | ||
314 | /** | |
315 | * @return ArrayCollection<Tag> | |
316 | */ | |
317 | public function getTags() | |
318 | { | |
319 | return $this->tags; | |
320 | } | |
321 | /** | |
322 | * @inheritDoc | |
323 | */ | |
324 | public function eraseCredentials() | |
325 | { | |
326 | } | |
327 | ||
328 | /** | |
329 | * @see \Serializable::serialize() | |
330 | */ | |
331 | public function serialize() | |
332 | { | |
333 | return serialize(array( | |
334 | $this->id, | |
335 | )); | |
336 | } | |
337 | ||
338 | /** | |
339 | * @see \Serializable::unserialize() | |
340 | */ | |
341 | public function unserialize($serialized) | |
342 | { | |
343 | list( | |
344 | $this->id, | |
345 | ) = unserialize($serialized); | |
346 | } | |
347 | ||
348 | public function isEqualTo(UserInterface $user) | |
349 | { | |
350 | return $this->username === $user->getUsername(); | |
351 | } | |
352 | ||
353 | public function isAccountNonExpired() | |
354 | { | |
355 | return true; | |
356 | } | |
357 | ||
358 | public function isAccountNonLocked() | |
359 | { | |
360 | return true; | |
361 | } | |
362 | ||
363 | public function isCredentialsNonExpired() | |
364 | { | |
365 | return true; | |
366 | } | |
367 | ||
368 | public function isEnabled() | |
369 | { | |
370 | return $this->isActive; | |
371 | } | |
372 | /** | |
373 | * Set config | |
374 | * | |
375 | * @param \Wallabag\CoreBundle\Entity\Config $config | |
376 | * @return User | |
377 | */ | |
378 | public function setConfig(\Wallabag\CoreBundle\Entity\Config $config = null) | |
379 | { | |
380 | $this->config = $config; | |
381 | ||
382 | return $this; | |
383 | } | |
384 | ||
385 | /** | |
386 | * Get config | |
387 | * | |
388 | * @return \Wallabag\CoreBundle\Entity\Config | |
389 | */ | |
390 | public function getConfig() | |
391 | { | |
392 | return $this->config; | |
393 | } | |
394 | ||
395 | /** | |
396 | * Set confirmationToken | |
397 | * | |
398 | * @param string $confirmationToken | |
399 | * @return User | |
400 | */ | |
401 | public function setConfirmationToken($confirmationToken) | |
402 | { | |
403 | $this->confirmationToken = $confirmationToken; | |
404 | ||
405 | return $this; | |
406 | } | |
407 | ||
408 | /** | |
409 | * Get confirmationToken | |
410 | * | |
411 | * @return string | |
412 | */ | |
413 | public function getConfirmationToken() | |
414 | { | |
415 | return $this->confirmationToken; | |
416 | } | |
417 | ||
418 | /** | |
419 | * Set passwordRequestedAt | |
420 | * | |
421 | * @param \DateTime $passwordRequestedAt | |
422 | * @return User | |
423 | */ | |
424 | public function setPasswordRequestedAt($passwordRequestedAt) | |
425 | { | |
426 | $this->passwordRequestedAt = $passwordRequestedAt; | |
427 | ||
428 | return $this; | |
429 | } | |
430 | ||
431 | /** | |
432 | * Get passwordRequestedAt | |
433 | * | |
434 | * @return \DateTime | |
435 | */ | |
436 | public function getPasswordRequestedAt() | |
437 | { | |
438 | return $this->passwordRequestedAt; | |
439 | } | |
440 | } |