]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
Merge pull request #4151 from ldidry/fix-4060
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
CommitLineData
9d50517c
NL
1<?php
2
1210dae1 3namespace Wallabag\UserBundle\Entity;
9d50517c 4
5f09650e 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
f808b016
JB
7use FOS\UserBundle\Model\User as BaseUser;
8use JMS\Serializer\Annotation\Accessor;
22510459 9use JMS\Serializer\Annotation\Groups;
5709ecb3 10use JMS\Serializer\Annotation\XmlRoot;
dfd0a7bc 11use Scheb\TwoFactorBundle\Model\BackupCodeInterface;
a6b242a1
JB
12use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface as EmailTwoFactorInterface;
13use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface as GoogleTwoFactorInterface;
619cc453
JB
14use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
15use Symfony\Component\Security\Core\User\UserInterface;
23406ca3 16use Wallabag\ApiBundle\Entity\Client;
1210dae1
NL
17use Wallabag\CoreBundle\Entity\Config;
18use Wallabag\CoreBundle\Entity\Entry;
927c9e79 19use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
9d50517c
NL
20
21/**
4346a860 22 * User.
9d50517c 23 *
5709ecb3 24 * @XmlRoot("user")
1210dae1 25 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
bd0f3d32 26 * @ORM\Table(name="`user`")
2f69eb4a 27 * @ORM\HasLifecycleCallbacks()
c844dc0c
J
28 *
29 * @UniqueEntity("email")
30 * @UniqueEntity("username")
9d50517c 31 */
dfd0a7bc 32class User extends BaseUser implements EmailTwoFactorInterface, GoogleTwoFactorInterface, BackupCodeInterface
9d50517c 33{
927c9e79
JB
34 use EntityTimestampsTrait;
35
5709ecb3 36 /** @Serializer\XmlAttribute */
9d50517c 37 /**
4346a860 38 * @var int
9d50517c 39 *
2f69eb4a 40 * @ORM\Column(name="id", type="integer")
9d50517c 41 * @ORM\Id
2f69eb4a 42 * @ORM\GeneratedValue(strategy="AUTO")
5709ecb3 43 *
0c00e525 44 * @Groups({"user_api", "user_api_with_client"})
9d50517c 45 */
a1691859 46 protected $id;
9d50517c
NL
47
48 /**
49 * @var string
50 *
51 * @ORM\Column(name="name", type="text", nullable=true)
5709ecb3 52 *
0c00e525 53 * @Groups({"user_api", "user_api_with_client"})
9d50517c 54 */
a1691859 55 protected $name;
6894d48e 56
5709ecb3
JB
57 /**
58 * @var string
59 *
0c00e525 60 * @Groups({"user_api", "user_api_with_client"})
5709ecb3
JB
61 */
62 protected $username;
63
64 /**
65 * @var string
66 *
0c00e525 67 * @Groups({"user_api", "user_api_with_client"})
5709ecb3
JB
68 */
69 protected $email;
70
2f69eb4a 71 /**
c3f7a2ca 72 * @var \DateTime
2f69eb4a
NL
73 *
74 * @ORM\Column(name="created_at", type="datetime")
5709ecb3 75 *
0c00e525 76 * @Groups({"user_api", "user_api_with_client"})
2f69eb4a 77 */
a1691859 78 protected $createdAt;
2f69eb4a
NL
79
80 /**
c3f7a2ca 81 * @var \DateTime
2f69eb4a
NL
82 *
83 * @ORM\Column(name="updated_at", type="datetime")
5709ecb3 84 *
0c00e525 85 * @Groups({"user_api", "user_api_with_client"})
2f69eb4a 86 */
a1691859 87 protected $updatedAt;
2f69eb4a 88
5f09650e 89 /**
1210dae1 90 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
5f09650e 91 */
a1691859 92 protected $entries;
5f09650e 93
32da2a70 94 /**
152fcccd 95 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
32da2a70 96 */
a1691859 97 protected $config;
32da2a70 98
9114615a
JB
99 /**
100 * @var ArrayCollection
101 *
102 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
103 */
e50d7d31 104 protected $siteCredentials;
9114615a 105
2db616b5 106 /**
f808b016
JB
107 * @var ArrayCollection
108 *
109 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
2db616b5 110 */
f808b016 111 protected $clients;
2db616b5
NL
112
113 /**
f808b016 114 * @see getFirstClient() below
0c00e525 115 *
f808b016
JB
116 * @Groups({"user_api_with_client"})
117 * @Accessor(getter="getFirstClient")
2db616b5 118 */
f808b016 119 protected $default_client;
2db616b5
NL
120
121 /**
f808b016 122 * @ORM\Column(type="integer", nullable=true)
2db616b5 123 */
f808b016 124 private $authCode;
2db616b5 125
23406ca3 126 /**
a6b242a1 127 * @ORM\Column(name="googleAuthenticatorSecret", type="string", nullable=true)
23406ca3 128 */
a6b242a1 129 private $googleAuthenticatorSecret;
23406ca3 130
dfd0a7bc
JB
131 /**
132 * @ORM\Column(type="json_array", nullable=true)
133 */
134 private $backupCodes;
135
0c00e525 136 /**
a6b242a1
JB
137 * @var bool
138 *
139 * @ORM\Column(type="boolean")
0c00e525 140 */
a6b242a1 141 private $emailTwoFactor = false;
0c00e525 142
c3235553
NL
143 public function __construct()
144 {
a1691859 145 parent::__construct();
98f0929f 146 $this->entries = new ArrayCollection();
4094ea47 147 $this->roles = ['ROLE_USER'];
c3235553 148 }
2f69eb4a 149
9d50517c 150 /**
4346a860
JB
151 * Set name.
152 *
153 * @param string $name
9d50517c 154 *
2f69eb4a 155 * @return User
9d50517c
NL
156 */
157 public function setName($name)
158 {
159 $this->name = $name;
160
161 return $this;
162 }
163
164 /**
4346a860 165 * Get name.
9d50517c 166 *
7df80cb3 167 * @return string
9d50517c
NL
168 */
169 public function getName()
170 {
171 return $this->name;
172 }
173
2f69eb4a 174 /**
3a6af6c5 175 * @return \DateTime
2f69eb4a
NL
176 */
177 public function getCreatedAt()
178 {
179 return $this->createdAt;
180 }
181
182 /**
3a6af6c5 183 * @return \DateTime
2f69eb4a
NL
184 */
185 public function getUpdatedAt()
186 {
187 return $this->updatedAt;
188 }
189
5f09650e 190 /**
5f09650e
NL
191 * @return User
192 */
193 public function addEntry(Entry $entry)
194 {
195 $this->entries[] = $entry;
196
197 return $this;
198 }
199
200 /**
201 * @return ArrayCollection<Entry>
202 */
203 public function getEntries()
204 {
205 return $this->entries;
206 }
207
c3235553
NL
208 public function isEqualTo(UserInterface $user)
209 {
210 return $this->username === $user->getUsername();
211 }
212
32da2a70 213 /**
4346a860
JB
214 * Set config.
215 *
1210dae1 216 * @param Config $config
32da2a70 217 *
32da2a70
J
218 * @return User
219 */
1210dae1 220 public function setConfig(Config $config = null)
32da2a70
J
221 {
222 $this->config = $config;
223
224 return $this;
225 }
226
227 /**
4346a860 228 * Get config.
32da2a70 229 *
1210dae1 230 * @return Config
32da2a70
J
231 */
232 public function getConfig()
233 {
234 return $this->config;
235 }
2db616b5
NL
236
237 /**
238 * @return bool
239 */
a6b242a1
JB
240 public function isEmailTwoFactor()
241 {
242 return $this->emailTwoFactor;
243 }
244
245 /**
246 * @param bool $emailTwoFactor
247 */
248 public function setEmailTwoFactor($emailTwoFactor)
2db616b5 249 {
a6b242a1 250 $this->emailTwoFactor = $emailTwoFactor;
2db616b5
NL
251 }
252
253 /**
a6b242a1 254 * Used in the user config form to be "like" the email option.
2db616b5 255 */
a6b242a1 256 public function isGoogleTwoFactor()
2db616b5 257 {
a6b242a1 258 return $this->isGoogleAuthenticatorEnabled();
2db616b5
NL
259 }
260
a6b242a1
JB
261 /**
262 * {@inheritdoc}
263 */
264 public function isEmailAuthEnabled(): bool
2db616b5 265 {
a6b242a1 266 return $this->emailTwoFactor;
2db616b5
NL
267 }
268
a6b242a1
JB
269 /**
270 * {@inheritdoc}
271 */
272 public function getEmailAuthCode(): string
2db616b5
NL
273 {
274 return $this->authCode;
275 }
276
a6b242a1
JB
277 /**
278 * {@inheritdoc}
279 */
280 public function setEmailAuthCode(string $authCode): void
2db616b5
NL
281 {
282 $this->authCode = $authCode;
283 }
284
a6b242a1
JB
285 /**
286 * {@inheritdoc}
287 */
288 public function getEmailAuthRecipient(): string
2db616b5 289 {
a6b242a1 290 return $this->email;
2db616b5
NL
291 }
292
a6b242a1
JB
293 /**
294 * {@inheritdoc}
295 */
296 public function isGoogleAuthenticatorEnabled(): bool
2db616b5 297 {
a6b242a1
JB
298 return $this->googleAuthenticatorSecret ? true : false;
299 }
2db616b5 300
a6b242a1
JB
301 /**
302 * {@inheritdoc}
303 */
304 public function getGoogleAuthenticatorUsername(): string
305 {
306 return $this->username;
307 }
2db616b5 308
a6b242a1
JB
309 /**
310 * {@inheritdoc}
311 */
312 public function getGoogleAuthenticatorSecret(): string
313 {
314 return $this->googleAuthenticatorSecret;
315 }
316
317 /**
318 * {@inheritdoc}
319 */
320 public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
321 {
322 $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
2db616b5 323 }
23406ca3 324
dfd0a7bc
JB
325 public function setBackupCodes(array $codes = null)
326 {
327 $this->backupCodes = $codes;
328 }
329
330 public function getBackupCodes()
331 {
332 return $this->backupCodes;
333 }
334
335 /**
336 * {@inheritdoc}
337 */
338 public function isBackupCode(string $code): bool
339 {
4654a83b 340 return false === $this->findBackupCode($code) ? false : true;
dfd0a7bc
JB
341 }
342
343 /**
344 * {@inheritdoc}
345 */
346 public function invalidateBackupCode(string $code): void
347 {
4654a83b 348 $key = $this->findBackupCode($code);
dfd0a7bc
JB
349
350 if (false !== $key) {
351 unset($this->backupCodes[$key]);
352 }
353 }
354
23406ca3 355 /**
23406ca3
NL
356 * @return User
357 */
358 public function addClient(Client $client)
359 {
360 $this->clients[] = $client;
361
362 return $this;
363 }
364
365 /**
366 * @return ArrayCollection<Entry>
367 */
368 public function getClients()
369 {
370 return $this->clients;
371 }
0c00e525
JB
372
373 /**
374 * Only used by the API when creating a new user it'll also return the first client (which was also created at the same time).
375 *
376 * @return Client
377 */
378 public function getFirstClient()
379 {
eb570e49
JB
380 if (!empty($this->clients)) {
381 return $this->clients->first();
0c00e525 382 }
0c00e525 383 }
4654a83b
JB
384
385 /**
386 * Try to find a backup code from the list of backup codes of the current user.
387 *
388 * @param string $code Given code from the user
389 *
390 * @return string|false
391 */
392 private function findBackupCode(string $code)
393 {
394 foreach ($this->backupCodes as $key => $backupCode) {
395 // backup code are hashed using `password_hash`
396 // see ConfigController->otpAppAction
397 if (password_verify($code, $backupCode)) {
398 return $key;
399 }
400 }
401
402 return false;
403 }
9d50517c 404}