3 namespace Wallabag\UserBundle\Entity
;
5 use Doctrine\Common\Collections\ArrayCollection
;
6 use Doctrine\ORM\Mapping
as ORM
;
7 use JMS\Serializer\Annotation\Groups
;
8 use JMS\Serializer\Annotation\XmlRoot
;
9 use JMS\Serializer\Annotation\Accessor
;
10 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface
;
11 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface
;
12 use FOS\UserBundle\Model\User
as BaseUser
;
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\FederationBundle\Entity\Account
;
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 TwoFactorInterface
, TrustedComputerInterface
33 /** @Serializer\XmlAttribute */
37 * @ORM\Column(name="id", type="integer")
39 * @ORM\GeneratedValue(strategy="AUTO")
41 * @Groups({"user_api", "user_api_with_client"})
48 * @ORM\Column(name="name", type="text", nullable=true)
50 * @Groups({"user_api", "user_api_with_client"})
57 * @Groups({"user_api", "user_api_with_client"})
64 * @Groups({"user_api", "user_api_with_client"})
71 * @ORM\Column(name="created_at", type="datetime")
73 * @Groups({"user_api", "user_api_with_client"})
80 * @ORM\Column(name="updated_at", type="datetime")
82 * @Groups({"user_api", "user_api_with_client"})
87 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
92 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Notification", mappedBy="user", cascade={"remove"})
94 protected $notifications;
97 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
102 * @ORM\Column(type="integer", nullable=true)
109 * @ORM\Column(type="boolean")
111 private $twoFactorAuthentication = false;
114 * @ORM\Column(type="json_array", nullable=true)
119 * @var ArrayCollection
121 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
126 * @see getFirstClient() below
128 * @Groups({"user_api_with_client"})
129 * @Accessor(getter="getFirstClient")
131 protected $default_client;
134 * @ORM\OneToOne(targetEntity="Wallabag\FederationBundle\Entity\Account", mappedBy="user", cascade={"remove"})
141 public function __construct()
143 parent
::__construct();
144 $this->entries
= new ArrayCollection();
145 $this->roles
= ['ROLE_USER'];
152 public function timestamps()
154 if (is_null($this->createdAt
)) {
155 $this->createdAt
= new \
DateTime();
158 $this->updatedAt
= new \
DateTime();
164 * @param string $name
168 public function setName($name)
180 public function getName()
188 public function getCreatedAt()
190 return $this->createdAt
;
196 public function getUpdatedAt()
198 return $this->updatedAt
;
202 * @param Entry $entry
206 public function addEntry(Entry
$entry)
208 $this->entries
[] = $entry;
214 * @return ArrayCollection<Entry>
216 public function getEntries()
218 return $this->entries
;
221 public function isEqualTo(UserInterface
$user)
223 return $this->username
=== $user->getUsername();
229 * @param Config $config
233 public function setConfig(Config
$config = null)
235 $this->config
= $config;
245 public function getConfig()
247 return $this->config
;
253 public function isTwoFactorAuthentication()
255 return $this->twoFactorAuthentication
;
259 * @param bool $twoFactorAuthentication
261 public function setTwoFactorAuthentication($twoFactorAuthentication)
263 $this->twoFactorAuthentication
= $twoFactorAuthentication;
266 public function isEmailAuthEnabled()
268 return $this->twoFactorAuthentication
;
271 public function getEmailAuthCode()
273 return $this->authCode
;
276 public function setEmailAuthCode($authCode)
278 $this->authCode
= $authCode;
281 public function addTrustedComputer($token, \DateTime
$validUntil)
283 $this->trusted
[$token] = $validUntil->format('r');
286 public function isTrustedComputer($token)
288 if (isset($this->trusted
[$token])) {
289 $now = new \
DateTime();
290 $validUntil = new \
DateTime($this->trusted
[$token]);
292 return $now < $validUntil;
299 * @param Client $client
303 public function addClient(Client
$client)
305 $this->clients
[] = $client;
311 * @return ArrayCollection<Entry>
313 public function getClients()
315 return $this->clients
;
319 * 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).
323 public function getFirstClient()
325 if (!empty($this->clients
)) {
326 return $this->clients
->first();
331 * @return ArrayCollection<NotificationInterface>
333 public function getNotifications()
335 return $this->notifications
;
339 * @param ArrayCollection<NotificationInterface> $notifications
341 public function setNotifications($notifications)
343 $this->notifications
= $notifications;
349 public function getAccount()
351 return $this->account
;
355 * @param mixed $account
358 public function setAccount(Account
$account)
360 $this->account
= $account;