]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
Add backup codes
[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
NL
190 /**
191 * @param Entry $entry
192 *
193 * @return User
194 */
195 public function addEntry(Entry $entry)
196 {
197 $this->entries[] = $entry;
198
199 return $this;
200 }
201
202 /**
203 * @return ArrayCollection<Entry>
204 */
205 public function getEntries()
206 {
207 return $this->entries;
208 }
209
c3235553
NL
210 public function isEqualTo(UserInterface $user)
211 {
212 return $this->username === $user->getUsername();
213 }
214
32da2a70 215 /**
4346a860
JB
216 * Set config.
217 *
1210dae1 218 * @param Config $config
32da2a70 219 *
32da2a70
J
220 * @return User
221 */
1210dae1 222 public function setConfig(Config $config = null)
32da2a70
J
223 {
224 $this->config = $config;
225
226 return $this;
227 }
228
229 /**
4346a860 230 * Get config.
32da2a70 231 *
1210dae1 232 * @return Config
32da2a70
J
233 */
234 public function getConfig()
235 {
236 return $this->config;
237 }
2db616b5
NL
238
239 /**
240 * @return bool
241 */
a6b242a1
JB
242 public function isEmailTwoFactor()
243 {
244 return $this->emailTwoFactor;
245 }
246
247 /**
248 * @param bool $emailTwoFactor
249 */
250 public function setEmailTwoFactor($emailTwoFactor)
2db616b5 251 {
a6b242a1 252 $this->emailTwoFactor = $emailTwoFactor;
2db616b5
NL
253 }
254
255 /**
a6b242a1 256 * Used in the user config form to be "like" the email option.
2db616b5 257 */
a6b242a1 258 public function isGoogleTwoFactor()
2db616b5 259 {
a6b242a1 260 return $this->isGoogleAuthenticatorEnabled();
2db616b5
NL
261 }
262
a6b242a1
JB
263 /**
264 * {@inheritdoc}
265 */
266 public function isEmailAuthEnabled(): bool
2db616b5 267 {
a6b242a1 268 return $this->emailTwoFactor;
2db616b5
NL
269 }
270
a6b242a1
JB
271 /**
272 * {@inheritdoc}
273 */
274 public function getEmailAuthCode(): string
2db616b5
NL
275 {
276 return $this->authCode;
277 }
278
a6b242a1
JB
279 /**
280 * {@inheritdoc}
281 */
282 public function setEmailAuthCode(string $authCode): void
2db616b5
NL
283 {
284 $this->authCode = $authCode;
285 }
286
a6b242a1
JB
287 /**
288 * {@inheritdoc}
289 */
290 public function getEmailAuthRecipient(): string
2db616b5 291 {
a6b242a1 292 return $this->email;
2db616b5
NL
293 }
294
a6b242a1
JB
295 /**
296 * {@inheritdoc}
297 */
298 public function isGoogleAuthenticatorEnabled(): bool
2db616b5 299 {
a6b242a1
JB
300 return $this->googleAuthenticatorSecret ? true : false;
301 }
2db616b5 302
a6b242a1
JB
303 /**
304 * {@inheritdoc}
305 */
306 public function getGoogleAuthenticatorUsername(): string
307 {
308 return $this->username;
309 }
2db616b5 310
a6b242a1
JB
311 /**
312 * {@inheritdoc}
313 */
314 public function getGoogleAuthenticatorSecret(): string
315 {
316 return $this->googleAuthenticatorSecret;
317 }
318
319 /**
320 * {@inheritdoc}
321 */
322 public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
323 {
324 $this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
2db616b5 325 }
23406ca3 326
dfd0a7bc
JB
327 public function setBackupCodes(array $codes = null)
328 {
329 $this->backupCodes = $codes;
330 }
331
332 public function getBackupCodes()
333 {
334 return $this->backupCodes;
335 }
336
337 /**
338 * {@inheritdoc}
339 */
340 public function isBackupCode(string $code): bool
341 {
342 return \in_array($code, $this->backupCodes, true);
343 }
344
345 /**
346 * {@inheritdoc}
347 */
348 public function invalidateBackupCode(string $code): void
349 {
350 $key = array_search($code, $this->backupCodes, true);
351
352 if (false !== $key) {
353 unset($this->backupCodes[$key]);
354 }
355 }
356
23406ca3
NL
357 /**
358 * @param Client $client
359 *
360 * @return User
361 */
362 public function addClient(Client $client)
363 {
364 $this->clients[] = $client;
365
366 return $this;
367 }
368
369 /**
370 * @return ArrayCollection<Entry>
371 */
372 public function getClients()
373 {
374 return $this->clients;
375 }
0c00e525
JB
376
377 /**
378 * 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).
379 *
380 * @return Client
381 */
382 public function getFirstClient()
383 {
eb570e49
JB
384 if (!empty($this->clients)) {
385 return $this->clients->first();
0c00e525 386 }
0c00e525 387 }
9d50517c 388}