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\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
96 * @var ArrayCollection
98 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
103 * @see getFirstClient() below
105 * @Groups({"user_api_with_client"})
106 * @Accessor(getter="getFirstClient")
108 protected $default_client;
111 * @ORM\Column(type="integer", nullable=true)
118 * @ORM\Column(type="boolean")
120 private $twoFactorAuthentication = false;
123 * @ORM\Column(type="json_array", nullable=true)
127 public function __construct()
129 parent
::__construct();
130 $this->entries
= new ArrayCollection();
131 $this->roles
= ['ROLE_USER'];
138 public function timestamps()
140 if (null === $this->createdAt
) {
141 $this->createdAt
= new \
DateTime();
144 $this->updatedAt
= new \
DateTime();
150 * @param string $name
154 public function setName($name)
166 public function getName()
174 public function getCreatedAt()
176 return $this->createdAt
;
182 public function getUpdatedAt()
184 return $this->updatedAt
;
188 * @param Entry $entry
192 public function addEntry(Entry
$entry)
194 $this->entries
[] = $entry;
200 * @return ArrayCollection<Entry>
202 public function getEntries()
204 return $this->entries
;
207 public function isEqualTo(UserInterface
$user)
209 return $this->username
=== $user->getUsername();
215 * @param Config $config
219 public function setConfig(Config
$config = null)
221 $this->config
= $config;
231 public function getConfig()
233 return $this->config
;
239 public function isTwoFactorAuthentication()
241 return $this->twoFactorAuthentication
;
245 * @param bool $twoFactorAuthentication
247 public function setTwoFactorAuthentication($twoFactorAuthentication)
249 $this->twoFactorAuthentication
= $twoFactorAuthentication;
252 public function isEmailAuthEnabled()
254 return $this->twoFactorAuthentication
;
257 public function getEmailAuthCode()
259 return $this->authCode
;
262 public function setEmailAuthCode($authCode)
264 $this->authCode
= $authCode;
267 public function addTrustedComputer($token, \DateTime
$validUntil)
269 $this->trusted
[$token] = $validUntil->format('r');
272 public function isTrustedComputer($token)
274 if (isset($this->trusted
[$token])) {
275 $now = new \
DateTime();
276 $validUntil = new \
DateTime($this->trusted
[$token]);
278 return $now < $validUntil;
285 * @param Client $client
289 public function addClient(Client
$client)
291 $this->clients
[] = $client;
297 * @return ArrayCollection<Entry>
299 public function getClients()
301 return $this->clients
;
305 * 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).
309 public function getFirstClient()
311 if (!empty($this->clients
)) {
312 return $this->clients
->first();