3 namespace Wallabag\UserBundle\Entity
;
5 use Doctrine\Common\Collections\ArrayCollection
;
6 use Doctrine\ORM\Mapping
as ORM
;
7 use FOS\UserBundle\Model\User
as BaseUser
;
8 use JMS\Serializer\Annotation\Accessor
;
9 use JMS\Serializer\Annotation\Groups
;
10 use JMS\Serializer\Annotation\XmlRoot
;
11 use Scheb\TwoFactorBundle\Model\BackupCodeInterface
;
12 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface
as EmailTwoFactorInterface
;
13 use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface
as GoogleTwoFactorInterface
;
14 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
;
15 use Symfony\Component\Security\Core\User\UserInterface
;
16 use Wallabag\ApiBundle\Entity\Client
;
17 use Wallabag\CoreBundle\Entity\Config
;
18 use Wallabag\CoreBundle\Entity\Entry
;
19 use Wallabag\CoreBundle\Helper\EntityTimestampsTrait
;
25 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
26 * @ORM\Table(name="`user`")
27 * @ORM\HasLifecycleCallbacks()
29 * @UniqueEntity("email")
30 * @UniqueEntity("username")
32 class User
extends BaseUser
implements EmailTwoFactorInterface
, GoogleTwoFactorInterface
, BackupCodeInterface
34 use EntityTimestampsTrait
;
36 /** @Serializer\XmlAttribute */
40 * @ORM\Column(name="id", type="integer")
42 * @ORM\GeneratedValue(strategy="AUTO")
44 * @Groups({"user_api", "user_api_with_client"})
51 * @ORM\Column(name="name", type="text", nullable=true)
53 * @Groups({"user_api", "user_api_with_client"})
60 * @Groups({"user_api", "user_api_with_client"})
67 * @Groups({"user_api", "user_api_with_client"})
74 * @ORM\Column(name="created_at", type="datetime")
76 * @Groups({"user_api", "user_api_with_client"})
83 * @ORM\Column(name="updated_at", type="datetime")
85 * @Groups({"user_api", "user_api_with_client"})
90 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
95 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
100 * @var ArrayCollection
102 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
104 protected $siteCredentials;
107 * @var ArrayCollection
109 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
114 * @see getFirstClient() below
116 * @Groups({"user_api_with_client"})
117 * @Accessor(getter="getFirstClient")
119 protected $default_client;
122 * @ORM\Column(type="integer", nullable=true)
127 * @ORM\Column(name="googleAuthenticatorSecret", type="string", nullable=true)
129 private $googleAuthenticatorSecret;
132 * @ORM\Column(type="json_array", nullable=true)
134 private $backupCodes;
139 * @ORM\Column(type="boolean")
141 private $emailTwoFactor = false;
143 public function __construct()
145 parent
::__construct();
146 $this->entries
= new ArrayCollection();
147 $this->roles
= ['ROLE_USER'];
153 * @param string $name
157 public function setName($name)
169 public function getName()
177 public function getCreatedAt()
179 return $this->createdAt
;
185 public function getUpdatedAt()
187 return $this->updatedAt
;
193 public function addEntry(Entry
$entry)
195 $this->entries
[] = $entry;
201 * @return ArrayCollection<Entry>
203 public function getEntries()
205 return $this->entries
;
208 public function isEqualTo(UserInterface
$user)
210 return $this->username
=== $user->getUsername();
216 * @param Config $config
220 public function setConfig(Config
$config = null)
222 $this->config
= $config;
232 public function getConfig()
234 return $this->config
;
240 public function isEmailTwoFactor()
242 return $this->emailTwoFactor
;
246 * @param bool $emailTwoFactor
248 public function setEmailTwoFactor($emailTwoFactor)
250 $this->emailTwoFactor
= $emailTwoFactor;
254 * Used in the user config form to be "like" the email option.
256 public function isGoogleTwoFactor()
258 return $this->isGoogleAuthenticatorEnabled();
264 public function isEmailAuthEnabled(): bool
266 return $this->emailTwoFactor
;
272 public function getEmailAuthCode(): string
274 return $this->authCode
;
280 public function setEmailAuthCode(string $authCode): void
282 $this->authCode
= $authCode;
288 public function getEmailAuthRecipient(): string
296 public function isGoogleAuthenticatorEnabled(): bool
298 return $this->googleAuthenticatorSecret
? true : false;
304 public function getGoogleAuthenticatorUsername(): string
306 return $this->username
;
312 public function getGoogleAuthenticatorSecret(): string
314 return $this->googleAuthenticatorSecret
;
320 public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
322 $this->googleAuthenticatorSecret
= $googleAuthenticatorSecret;
325 public function setBackupCodes(array $codes = null)
327 $this->backupCodes
= $codes;
330 public function getBackupCodes()
332 return $this->backupCodes
;
338 public function isBackupCode(string $code): bool
340 return false === $this->findBackupCode($code) ? false : true;
346 public function invalidateBackupCode(string $code): void
348 $key = $this->findBackupCode($code);
350 if (false !== $key) {
351 unset($this->backupCodes
[$key]);
358 public function addClient(Client
$client)
360 $this->clients
[] = $client;
366 * @return ArrayCollection<Entry>
368 public function getClients()
370 return $this->clients
;
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).
378 public function getFirstClient()
380 if (!empty($this->clients
)) {
381 return $this->clients
->first();
386 * Try to find a backup code from the list of backup codes of the current user.
388 * @param string $code Given code from the user
390 * @return string|false
392 private function findBackupCode(string $code)
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)) {