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