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
;
12 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface
;
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
;
23 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
24 * @ORM\Table(name="`user`")
25 * @ORM\HasLifecycleCallbacks()
27 * @UniqueEntity("email")
28 * @UniqueEntity("username")
30 class User
extends BaseUser
implements TwoFactorInterface
, TrustedComputerInterface
32 /** @Serializer\XmlAttribute */
36 * @ORM\Column(name="id", type="integer")
38 * @ORM\GeneratedValue(strategy="AUTO")
40 * @Groups({"user_api", "user_api_with_client"})
47 * @ORM\Column(name="name", type="text", nullable=true)
49 * @Groups({"user_api", "user_api_with_client"})
56 * @Groups({"user_api", "user_api_with_client"})
63 * @Groups({"user_api", "user_api_with_client"})
70 * @ORM\Column(name="created_at", type="datetime")
72 * @Groups({"user_api", "user_api_with_client"})
79 * @ORM\Column(name="updated_at", type="datetime")
81 * @Groups({"user_api", "user_api_with_client"})
86 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
91 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Notification", mappedBy="user", cascade={"remove"})
93 protected $notifications;
96 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
101 * @var ArrayCollection
103 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
105 protected $siteCredentials;
108 * @var ArrayCollection
110 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
115 * @see getFirstClient() below
117 * @Groups({"user_api_with_client"})
118 * @Accessor(getter="getFirstClient")
120 protected $default_client;
123 * @ORM\Column(type="integer", nullable=true)
130 * @ORM\Column(type="boolean")
132 private $twoFactorAuthentication = false;
135 * @ORM\Column(type="json_array", nullable=true)
139 public function __construct()
141 parent
::__construct();
142 $this->entries
= new ArrayCollection();
143 $this->roles
= ['ROLE_USER'];
150 public function timestamps()
152 if (null === $this->createdAt
) {
153 $this->createdAt
= new \
DateTime();
156 $this->updatedAt
= new \
DateTime();
162 * @param string $name
166 public function setName($name)
178 public function getName()
186 public function getCreatedAt()
188 return $this->createdAt
;
194 public function getUpdatedAt()
196 return $this->updatedAt
;
200 * @param Entry $entry
204 public function addEntry(Entry
$entry)
206 $this->entries
[] = $entry;
212 * @return ArrayCollection<Entry>
214 public function getEntries()
216 return $this->entries
;
219 public function isEqualTo(UserInterface
$user)
221 return $this->username
=== $user->getUsername();
227 * @param Config $config
231 public function setConfig(Config
$config = null)
233 $this->config
= $config;
243 public function getConfig()
245 return $this->config
;
251 public function isTwoFactorAuthentication()
253 return $this->twoFactorAuthentication
;
257 * @param bool $twoFactorAuthentication
259 public function setTwoFactorAuthentication($twoFactorAuthentication)
261 $this->twoFactorAuthentication
= $twoFactorAuthentication;
264 public function isEmailAuthEnabled()
266 return $this->twoFactorAuthentication
;
269 public function getEmailAuthCode()
271 return $this->authCode
;
274 public function setEmailAuthCode($authCode)
276 $this->authCode
= $authCode;
279 public function addTrustedComputer($token, \DateTime
$validUntil)
281 $this->trusted
[$token] = $validUntil->format('r');
284 public function isTrustedComputer($token)
286 if (isset($this->trusted
[$token])) {
287 $now = new \
DateTime();
288 $validUntil = new \
DateTime($this->trusted
[$token]);
290 return $now < $validUntil;
297 * @param Client $client
301 public function addClient(Client
$client)
303 $this->clients
[] = $client;
309 * @return ArrayCollection<Entry>
311 public function getClients()
313 return $this->clients
;
317 * 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).
321 public function getFirstClient()
323 if (!empty($this->clients
)) {
324 return $this->clients
->first();
329 * @return ArrayCollection<NotificationInterface>
331 public function getNotifications()
333 return $this->notifications
;
337 * @param ArrayCollection<NotificationInterface> $notifications
339 public function setNotifications($notifications)
341 $this->notifications
= $notifications;