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