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
;
191 * @param Entry $entry
195 public function addEntry(Entry
$entry)
197 $this->entries
[] = $entry;
203 * @return ArrayCollection<Entry>
205 public function getEntries()
207 return $this->entries
;
210 public function isEqualTo(UserInterface
$user)
212 return $this->username
=== $user->getUsername();
218 * @param Config $config
222 public function setConfig(Config
$config = null)
224 $this->config
= $config;
234 public function getConfig()
236 return $this->config
;
242 public function isEmailTwoFactor()
244 return $this->emailTwoFactor
;
248 * @param bool $emailTwoFactor
250 public function setEmailTwoFactor($emailTwoFactor)
252 $this->emailTwoFactor
= $emailTwoFactor;
256 * Used in the user config form to be "like" the email option.
258 public function isGoogleTwoFactor()
260 return $this->isGoogleAuthenticatorEnabled();
266 public function isEmailAuthEnabled(): bool
268 return $this->emailTwoFactor
;
274 public function getEmailAuthCode(): string
276 return $this->authCode
;
282 public function setEmailAuthCode(string $authCode): void
284 $this->authCode
= $authCode;
290 public function getEmailAuthRecipient(): string
298 public function isGoogleAuthenticatorEnabled(): bool
300 return $this->googleAuthenticatorSecret
? true : false;
306 public function getGoogleAuthenticatorUsername(): string
308 return $this->username
;
314 public function getGoogleAuthenticatorSecret(): string
316 return $this->googleAuthenticatorSecret
;
322 public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
324 $this->googleAuthenticatorSecret
= $googleAuthenticatorSecret;
327 public function setBackupCodes(array $codes = null)
329 $this->backupCodes
= $codes;
332 public function getBackupCodes()
334 return $this->backupCodes
;
340 public function isBackupCode(string $code): bool
342 return false === $this->findBackupCode($code) ? false : true;
348 public function invalidateBackupCode(string $code): void
350 $key = $this->findBackupCode($code);
352 if (false !== $key) {
353 unset($this->backupCodes
[$key]);
358 * @param Client $client
362 public function addClient(Client
$client)
364 $this->clients
[] = $client;
370 * @return ArrayCollection<Entry>
372 public function getClients()
374 return $this->clients
;
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).
382 public function getFirstClient()
384 if (!empty($this->clients
)) {
385 return $this->clients
->first();
390 * Try to find a backup code from the list of backup codes of the current user.
392 * @param string $code Given code from the user
394 * @return string|false
396 private function findBackupCode(string $code)
398 foreach ($this->backupCodes
as $key => $backupCode) {
399 // backup code are hashed using `password_hash`
400 // see ConfigController->otpAppAction
401 if (password_verify($code, $backupCode)) {