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