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
;
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 * @ORM\Column(type="integer", nullable=true)
103 * @ORM\Column(type="boolean")
105 private $twoFactorAuthentication = false;
108 * @ORM\Column(type="json_array", nullable=true)
113 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
118 * @see getFirstClient() below
120 * @Groups({"user_api_with_client"})
121 * @Accessor(getter="getFirstClient")
123 protected $default_client;
125 public function __construct()
127 parent
::__construct();
128 $this->entries
= new ArrayCollection();
129 $this->roles
= ['ROLE_USER'];
136 public function timestamps()
138 if (is_null($this->createdAt
)) {
139 $this->createdAt
= new \
DateTime();
142 $this->updatedAt
= new \
DateTime();
148 * @param string $name
152 public function setName($name)
164 public function getName()
172 public function getCreatedAt()
174 return $this->createdAt
;
180 public function getUpdatedAt()
182 return $this->updatedAt
;
186 * @param Entry $entry
190 public function addEntry(Entry
$entry)
192 $this->entries
[] = $entry;
198 * @return ArrayCollection<Entry>
200 public function getEntries()
202 return $this->entries
;
205 public function isEqualTo(UserInterface
$user)
207 return $this->username
=== $user->getUsername();
213 * @param Config $config
217 public function setConfig(Config
$config = null)
219 $this->config
= $config;
229 public function getConfig()
231 return $this->config
;
237 public function isTwoFactorAuthentication()
239 return $this->twoFactorAuthentication
;
243 * @param bool $twoFactorAuthentication
245 public function setTwoFactorAuthentication($twoFactorAuthentication)
247 $this->twoFactorAuthentication
= $twoFactorAuthentication;
250 public function isEmailAuthEnabled()
252 return $this->twoFactorAuthentication
;
255 public function getEmailAuthCode()
257 return $this->authCode
;
260 public function setEmailAuthCode($authCode)
262 $this->authCode
= $authCode;
265 public function addTrustedComputer($token, \DateTime
$validUntil)
267 $this->trusted
[$token] = $validUntil->format('r');
270 public function isTrustedComputer($token)
272 if (isset($this->trusted
[$token])) {
273 $now = new \
DateTime();
274 $validUntil = new \
DateTime($this->trusted
[$token]);
276 return $now < $validUntil;
283 * @param Client $client
287 public function addClient(Client
$client)
289 $this->clients
[] = $client;
295 * @return ArrayCollection<Entry>
297 public function getClients()
299 return $this->clients
;
303 * 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).
307 public function getFirstClient()
309 if (empty($this->clients
)) {
310 return $this->clients
;
313 return $this->clients
->first();