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\Email\TwoFactorInterface
as EmailTwoFactorInterface
;
12 use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface
as GoogleTwoFactorInterface
;
13 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity
;
14 use Symfony\Component\Security\Core\User\UserInterface
;
15 use Wallabag\ApiBundle\Entity\Client
;
16 use Wallabag\CoreBundle\Entity\Config
;
17 use Wallabag\CoreBundle\Entity\Entry
;
18 use Wallabag\CoreBundle\Helper\EntityTimestampsTrait
;
24 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
25 * @ORM\Table(name="`user`")
26 * @ORM\HasLifecycleCallbacks()
28 * @UniqueEntity("email")
29 * @UniqueEntity("username")
31 class User
extends BaseUser
implements EmailTwoFactorInterface
, GoogleTwoFactorInterface
33 use EntityTimestampsTrait
;
35 /** @Serializer\XmlAttribute */
39 * @ORM\Column(name="id", type="integer")
41 * @ORM\GeneratedValue(strategy="AUTO")
43 * @Groups({"user_api", "user_api_with_client"})
50 * @ORM\Column(name="name", type="text", nullable=true)
52 * @Groups({"user_api", "user_api_with_client"})
59 * @Groups({"user_api", "user_api_with_client"})
66 * @Groups({"user_api", "user_api_with_client"})
73 * @ORM\Column(name="created_at", type="datetime")
75 * @Groups({"user_api", "user_api_with_client"})
82 * @ORM\Column(name="updated_at", type="datetime")
84 * @Groups({"user_api", "user_api_with_client"})
89 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
94 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
99 * @var ArrayCollection
101 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
103 protected $siteCredentials;
106 * @var ArrayCollection
108 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
113 * @see getFirstClient() below
115 * @Groups({"user_api_with_client"})
116 * @Accessor(getter="getFirstClient")
118 protected $default_client;
121 * @ORM\Column(type="integer", nullable=true)
126 * @ORM\Column(name="googleAuthenticatorSecret", type="string", nullable=true)
128 private $googleAuthenticatorSecret;
133 * @ORM\Column(type="boolean")
135 private $emailTwoFactor = false;
137 public function __construct()
139 parent
::__construct();
140 $this->entries
= new ArrayCollection();
141 $this->roles
= ['ROLE_USER'];
147 * @param string $name
151 public function setName($name)
163 public function getName()
171 public function getCreatedAt()
173 return $this->createdAt
;
179 public function getUpdatedAt()
181 return $this->updatedAt
;
185 * @param Entry $entry
189 public function addEntry(Entry
$entry)
191 $this->entries
[] = $entry;
197 * @return ArrayCollection<Entry>
199 public function getEntries()
201 return $this->entries
;
204 public function isEqualTo(UserInterface
$user)
206 return $this->username
=== $user->getUsername();
212 * @param Config $config
216 public function setConfig(Config
$config = null)
218 $this->config
= $config;
228 public function getConfig()
230 return $this->config
;
236 public function isEmailTwoFactor()
238 return $this->emailTwoFactor
;
242 * @param bool $emailTwoFactor
244 public function setEmailTwoFactor($emailTwoFactor)
246 $this->emailTwoFactor
= $emailTwoFactor;
250 * Used in the user config form to be "like" the email option.
252 public function isGoogleTwoFactor()
254 return $this->isGoogleAuthenticatorEnabled();
260 public function isEmailAuthEnabled(): bool
262 return $this->emailTwoFactor
;
268 public function getEmailAuthCode(): string
270 return $this->authCode
;
276 public function setEmailAuthCode(string $authCode): void
278 $this->authCode
= $authCode;
284 public function getEmailAuthRecipient(): string
292 public function isGoogleAuthenticatorEnabled(): bool
294 return $this->googleAuthenticatorSecret
? true : false;
300 public function getGoogleAuthenticatorUsername(): string
302 return $this->username
;
308 public function getGoogleAuthenticatorSecret(): string
310 return $this->googleAuthenticatorSecret
;
316 public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
318 $this->googleAuthenticatorSecret
= $googleAuthenticatorSecret;
322 * @param Client $client
326 public function addClient(Client
$client)
328 $this->clients
[] = $client;
334 * @return ArrayCollection<Entry>
336 public function getClients()
338 return $this->clients
;
342 * 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).
346 public function getFirstClient()
348 if (!empty($this->clients
)) {
349 return $this->clients
->first();