]>
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\Component\Security\Core\User\UserInterface; | |
8 | use Symfony\Component\Security\Core\User\AdvancedUserInterface; | |
9 | use Symfony\Component\Validator\Constraints as Assert; | |
10 | use JMS\Serializer\Annotation\ExclusionPolicy; | |
11 | use JMS\Serializer\Annotation\Expose; | |
12 | ||
13 | /** | |
14 | * User | |
15 | * | |
16 | * @ORM\Table(name="user") | |
17 | * @ORM\Entity | |
18 | * @ORM\HasLifecycleCallbacks() | |
19 | * @ExclusionPolicy("all") | |
20 | */ | |
21 | class User implements AdvancedUserInterface, \Serializable | |
22 | { | |
23 | /** | |
24 | * @var integer | |
25 | * | |
26 | * @Expose | |
27 | * @ORM\Column(name="id", type="integer") | |
28 | * @ORM\Id | |
29 | * @ORM\GeneratedValue(strategy="AUTO") | |
30 | */ | |
31 | private $id; | |
32 | ||
33 | /** | |
34 | * @var string | |
35 | * | |
36 | * @ORM\Column(name="username", type="text") | |
37 | * @Assert\NotBlank() | |
38 | * @Assert\Length( | |
39 | * min = "3", | |
40 | * max = "255" | |
41 | * ) | |
42 | */ | |
43 | private $username; | |
44 | ||
45 | /** | |
46 | * @var string | |
47 | * | |
48 | * @ORM\Column(type="string", length=32) | |
49 | */ | |
50 | private $salt; | |
51 | ||
52 | /** | |
53 | * @var string | |
54 | * | |
55 | * @ORM\Column(name="password", type="text") | |
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 | * | |
69 | * @ORM\Column(name="email", type="text", nullable=false) | |
70 | * @Assert\Email() | |
71 | * @Assert\NotBlank() | |
72 | */ | |
73 | private $email; | |
74 | ||
75 | /** | |
76 | * @ORM\Column(name="is_active", type="boolean", nullable=false) | |
77 | */ | |
78 | private $isActive = true; | |
79 | ||
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 | ||
94 | /** | |
95 | * @ORM\OneToMany(targetEntity="Entry", mappedBy="user", cascade={"remove"}) | |
96 | */ | |
97 | private $entries; | |
98 | ||
99 | /** | |
100 | * @ORM\OneToOne(targetEntity="Config", mappedBy="user") | |
101 | */ | |
102 | private $config; | |
103 | ||
104 | /** | |
105 | * @ORM\OneToMany(targetEntity="Tag", mappedBy="user", cascade={"remove"}) | |
106 | */ | |
107 | private $tags; | |
108 | ||
109 | public function __construct() | |
110 | { | |
111 | $this->isActive = true; | |
112 | $this->salt = md5(uniqid(null, true)); | |
113 | $this->entries = new ArrayCollection(); | |
114 | $this->tags = new ArrayCollection(); | |
115 | } | |
116 | ||
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 | ||
130 | /** | |
131 | * Get id | |
132 | * | |
133 | * @return integer | |
134 | */ | |
135 | public function getId() | |
136 | { | |
137 | return $this->id; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Set username | |
142 | * | |
143 | * @param string $username | |
144 | * @return User | |
145 | */ | |
146 | public function setUsername($username) | |
147 | { | |
148 | $this->username = $username; | |
149 | ||
150 | return $this; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Get username | |
155 | * | |
156 | * @return string | |
157 | */ | |
158 | public function getUsername() | |
159 | { | |
160 | return $this->username; | |
161 | } | |
162 | ||
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 | ||
179 | /** | |
180 | * Set password | |
181 | * | |
182 | * @param string $password | |
183 | * @return User | |
184 | */ | |
185 | public function setPassword($password) | |
186 | { | |
187 | if (!$password && 0 === strlen($password)) { | |
188 | return; | |
189 | } | |
190 | ||
191 | $this->password = sha1($password.$this->getUsername().$this->getSalt()); | |
192 | ||
193 | return $this; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Get password | |
198 | * | |
199 | * @return string | |
200 | */ | |
201 | public function getPassword() | |
202 | { | |
203 | return $this->password; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Set name | |
208 | * | |
209 | * @param string $name | |
210 | * @return User | |
211 | */ | |
212 | public function setName($name) | |
213 | { | |
214 | $this->name = $name; | |
215 | ||
216 | return $this; | |
217 | } | |
218 | ||
219 | /** | |
220 | * Get name | |
221 | * | |
222 | * @return string | |
223 | */ | |
224 | public function getName() | |
225 | { | |
226 | return $this->name; | |
227 | } | |
228 | ||
229 | /** | |
230 | * Set email | |
231 | * | |
232 | * @param string $email | |
233 | * @return User | |
234 | */ | |
235 | public function setEmail($email) | |
236 | { | |
237 | $this->email = $email; | |
238 | ||
239 | return $this; | |
240 | } | |
241 | ||
242 | /** | |
243 | * Get email | |
244 | * | |
245 | * @return string | |
246 | */ | |
247 | public function getEmail() | |
248 | { | |
249 | return $this->email; | |
250 | } | |
251 | ||
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 | ||
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 | ||
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 | } | |
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 | { | |
329 | list( | |
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 | } | |
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 | } | |
380 | } |