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