]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Entity/User.php
first draft of hypermedia implementation
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / User.php
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 public function __construct()
105 {
106 $this->salt = md5(uniqid(null, true));
107 $this->entries = new ArrayCollection();
108 }
109
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
123 /**
124 * Get id
125 *
126 * @return integer
127 */
128 public function getId()
129 {
130 return $this->id;
131 }
132
133 /**
134 * Set username
135 *
136 * @param string $username
137 * @return User
138 */
139 public function setUsername($username)
140 {
141 $this->username = $username;
142
143 return $this;
144 }
145
146 /**
147 * Get username
148 *
149 * @return string
150 */
151 public function getUsername()
152 {
153 return $this->username;
154 }
155
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
172 /**
173 * Set password
174 *
175 * @param string $password
176 * @return User
177 */
178 public function setPassword($password)
179 {
180 if (!$password && 0 === strlen($password)) {
181 return;
182 }
183
184 $this->password = sha1($password.$this->getUsername().$this->getSalt());
185
186 return $this;
187 }
188
189 /**
190 * Get password
191 *
192 * @return string
193 */
194 public function getPassword()
195 {
196 return $this->password;
197 }
198
199 /**
200 * Set name
201 *
202 * @param string $name
203 * @return User
204 */
205 public function setName($name)
206 {
207 $this->name = $name;
208
209 return $this;
210 }
211
212 /**
213 * Get name
214 *
215 * @return string
216 */
217 public function getName()
218 {
219 return $this->name;
220 }
221
222 /**
223 * Set email
224 *
225 * @param string $email
226 * @return User
227 */
228 public function setEmail($email)
229 {
230 $this->email = $email;
231
232 return $this;
233 }
234
235 /**
236 * Get email
237 *
238 * @return string
239 */
240 public function getEmail()
241 {
242 return $this->email;
243 }
244
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
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
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 {
303 list(
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 }
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 }
354 }