]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Entity/User.php
first draft of hypermedia implementation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / User.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Entity;
9d50517c 4
5f09650e 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
c3235553 7use Symfony\Component\Security\Core\User\UserInterface;
c3235553 8use Symfony\Component\Security\Core\User\AdvancedUserInterface;
c0d9eba0 9use Symfony\Component\Validator\Constraints as Assert;
0f006880
NL
10use JMS\Serializer\Annotation\ExclusionPolicy;
11use 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 21class 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
c3235553
NL
104 public function __construct()
105 {
c0d9eba0
J
106 $this->salt = md5(uniqid(null, true));
107 $this->entries = new ArrayCollection();
c3235553 108 }
9d50517c 109
2f69eb4a
NL
110 /**
111 * @ORM\PrePersist
112 * @ORM\PreUpdate
113 */
114 public function timestamps()
115 {
116 if (is_null($this->createdAt)) {
117 $this->createdAt = new \DateTime();
118 }
119
120 $this->updatedAt = new \DateTime();
121 }
122
9d50517c
NL
123 /**
124 * Get id
125 *
7df80cb3 126 * @return integer
9d50517c
NL
127 */
128 public function getId()
129 {
130 return $this->id;
131 }
132
133 /**
134 * Set username
135 *
7df80cb3 136 * @param string $username
2f69eb4a 137 * @return User
9d50517c
NL
138 */
139 public function setUsername($username)
140 {
141 $this->username = $username;
142
143 return $this;
144 }
145
146 /**
147 * Get username
148 *
7df80cb3 149 * @return string
9d50517c
NL
150 */
151 public function getUsername()
152 {
153 return $this->username;
154 }
155
c3235553
NL
156 /**
157 * @inheritDoc
158 */
159 public function getSalt()
160 {
161 return $this->salt;
162 }
163
164 /**
165 * @inheritDoc
166 */
167 public function getRoles()
168 {
169 return array('ROLE_USER');
170 }
171
9d50517c
NL
172 /**
173 * Set password
174 *
7df80cb3 175 * @param string $password
2f69eb4a 176 * @return User
9d50517c
NL
177 */
178 public function setPassword($password)
179 {
d9169157
J
180 if (!$password && 0 === strlen($password)) {
181 return;
182 }
183
184 $this->password = sha1($password.$this->getUsername().$this->getSalt());
9d50517c
NL
185
186 return $this;
187 }
188
189 /**
190 * Get password
191 *
7df80cb3 192 * @return string
9d50517c
NL
193 */
194 public function getPassword()
195 {
196 return $this->password;
197 }
198
199 /**
200 * Set name
201 *
7df80cb3 202 * @param string $name
2f69eb4a 203 * @return User
9d50517c
NL
204 */
205 public function setName($name)
206 {
207 $this->name = $name;
208
209 return $this;
210 }
211
212 /**
213 * Get name
214 *
7df80cb3 215 * @return string
9d50517c
NL
216 */
217 public function getName()
218 {
219 return $this->name;
220 }
221
222 /**
223 * Set email
224 *
7df80cb3 225 * @param string $email
2f69eb4a 226 * @return User
9d50517c
NL
227 */
228 public function setEmail($email)
229 {
230 $this->email = $email;
231
232 return $this;
233 }
234
235 /**
236 * Get email
237 *
7df80cb3 238 * @return string
9d50517c
NL
239 */
240 public function getEmail()
241 {
242 return $this->email;
243 }
c3235553 244
2f69eb4a
NL
245 /**
246 * @return string
247 */
248 public function getCreatedAt()
249 {
250 return $this->createdAt;
251 }
252
253 /**
254 * @return string
255 */
256 public function getUpdatedAt()
257 {
258 return $this->updatedAt;
259 }
260
5f09650e
NL
261 /**
262 * @param Entry $entry
263 *
264 * @return User
265 */
266 public function addEntry(Entry $entry)
267 {
268 $this->entries[] = $entry;
269
270 return $this;
271 }
272
273 /**
274 * @return ArrayCollection<Entry>
275 */
276 public function getEntries()
277 {
278 return $this->entries;
279 }
280
c3235553
NL
281 /**
282 * @inheritDoc
283 */
284 public function eraseCredentials()
285 {
286 }
287
288 /**
289 * @see \Serializable::serialize()
290 */
291 public function serialize()
292 {
293 return serialize(array(
294 $this->id,
295 ));
296 }
297
298 /**
299 * @see \Serializable::unserialize()
300 */
301 public function unserialize($serialized)
302 {
7df80cb3 303 list(
c3235553
NL
304 $this->id,
305 ) = unserialize($serialized);
306 }
307
308 public function isEqualTo(UserInterface $user)
309 {
310 return $this->username === $user->getUsername();
311 }
312
313 public function isAccountNonExpired()
314 {
315 return true;
316 }
317
318 public function isAccountNonLocked()
319 {
320 return true;
321 }
322
323 public function isCredentialsNonExpired()
324 {
325 return true;
326 }
327
328 public function isEnabled()
329 {
330 return $this->isActive;
331 }
32da2a70
J
332 /**
333 * Set config
334 *
335 * @param \Wallabag\CoreBundle\Entity\Config $config
336 * @return User
337 */
338 public function setConfig(\Wallabag\CoreBundle\Entity\Config $config = null)
339 {
340 $this->config = $config;
341
342 return $this;
343 }
344
345 /**
346 * Get config
347 *
348 * @return \Wallabag\CoreBundle\Entity\Config
349 */
350 public function getConfig()
351 {
352 return $this->config;
353 }
9d50517c 354}