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\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 * @ORM\Column(type="integer", nullable=true)
108 * @ORM\Column(type="boolean")
110 private $twoFactorAuthentication = false;
113 * @ORM\Column(type="json_array", nullable=true)
118 * @var ArrayCollection
120 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
125 * @see getFirstClient() below
127 * @Groups({"user_api_with_client"})
128 * @Accessor(getter="getFirstClient")
130 protected $default_client;
132 public function __construct()
134 parent
::__construct();
135 $this->entries
= new ArrayCollection();
136 $this->roles
= ['ROLE_USER'];
143 public function timestamps()
145 if (is_null($this->createdAt
)) {
146 $this->createdAt
= new \
DateTime();
149 $this->updatedAt
= new \
DateTime();
155 * @param string $name
159 public function setName($name)
171 public function getName()
179 public function getCreatedAt()
181 return $this->createdAt
;
187 public function getUpdatedAt()
189 return $this->updatedAt
;
193 * @param Entry $entry
197 public function addEntry(Entry
$entry)
199 $this->entries
[] = $entry;
205 * @return ArrayCollection<Entry>
207 public function getEntries()
209 return $this->entries
;
212 public function isEqualTo(UserInterface
$user)
214 return $this->username
=== $user->getUsername();
220 * @param Config $config
224 public function setConfig(Config
$config = null)
226 $this->config
= $config;
236 public function getConfig()
238 return $this->config
;
244 public function isTwoFactorAuthentication()
246 return $this->twoFactorAuthentication
;
250 * @param bool $twoFactorAuthentication
252 public function setTwoFactorAuthentication($twoFactorAuthentication)
254 $this->twoFactorAuthentication
= $twoFactorAuthentication;
257 public function isEmailAuthEnabled()
259 return $this->twoFactorAuthentication
;
262 public function getEmailAuthCode()
264 return $this->authCode
;
267 public function setEmailAuthCode($authCode)
269 $this->authCode
= $authCode;
272 public function addTrustedComputer($token, \DateTime
$validUntil)
274 $this->trusted
[$token] = $validUntil->format('r');
277 public function isTrustedComputer($token)
279 if (isset($this->trusted
[$token])) {
280 $now = new \
DateTime();
281 $validUntil = new \
DateTime($this->trusted
[$token]);
283 return $now < $validUntil;
290 * @param Client $client
294 public function addClient(Client
$client)
296 $this->clients
[] = $client;
302 * @return ArrayCollection<Entry>
304 public function getClients()
306 return $this->clients
;
310 * 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).
314 public function getFirstClient()
316 if (!empty($this->clients
)) {
317 return $this->clients
->first();
322 * @return ArrayCollection<NotificationInterface>
324 public function getNotifications()
326 return $this->notifications
;
330 * @param ArrayCollection<NotificationInterface> $notifications
332 public function setNotifications($notifications)
334 $this->notifications
= $notifications;