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)
101 * @ORM\ManyToMany(targetEntity="Wallabag\GroupBundle\Entity\Group")
102 * @ORM\JoinTable(name="user_group",
103 * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
104 * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
110 * @var bool Enabled yes/no
111 * @ORM\Column(type="boolean")
113 private $twoFactorAuthentication = false;
116 * @ORM\Column(type="json_array", nullable=true)
121 * @var ArrayCollection
123 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
128 * @see getFirstClient() below
130 * @Groups({"user_api_with_client"})
131 * @Accessor(getter="getFirstClient")
133 protected $default_client;
135 public function __construct()
137 parent
::__construct();
138 $this->entries
= new ArrayCollection();
139 $this->roles
= ['ROLE_USER'];
146 public function timestamps()
148 if (is_null($this->createdAt
)) {
149 $this->createdAt
= new \
DateTime();
152 $this->updatedAt
= new \
DateTime();
158 * @param string $name
162 public function setName($name)
174 public function getName()
182 public function getCreatedAt()
184 return $this->createdAt
;
190 public function getUpdatedAt()
192 return $this->updatedAt
;
196 * @param Entry $entry
200 public function addEntry(Entry
$entry)
202 $this->entries
[] = $entry;
208 * @return ArrayCollection<Entry>
210 public function getEntries()
212 return $this->entries
;
215 public function isEqualTo(UserInterface
$user)
217 return $this->username
=== $user->getUsername();
223 * @param Config $config
227 public function setConfig(Config
$config = null)
229 $this->config
= $config;
239 public function getConfig()
241 return $this->config
;
247 public function isTwoFactorAuthentication()
249 return $this->twoFactorAuthentication
;
253 * @param bool $twoFactorAuthentication
255 public function setTwoFactorAuthentication($twoFactorAuthentication)
257 $this->twoFactorAuthentication
= $twoFactorAuthentication;
260 public function isEmailAuthEnabled()
262 return $this->twoFactorAuthentication
;
265 public function getEmailAuthCode()
267 return $this->authCode
;
270 public function setEmailAuthCode($authCode)
272 $this->authCode
= $authCode;
275 public function addTrustedComputer($token, \DateTime
$validUntil)
277 $this->trusted
[$token] = $validUntil->format('r');
280 public function isTrustedComputer($token)
282 if (isset($this->trusted
[$token])) {
283 $now = new \
DateTime();
284 $validUntil = new \
DateTime($this->trusted
[$token]);
286 return $now < $validUntil;
293 * @param Client $client
297 public function addClient(Client
$client)
299 $this->clients
[] = $client;
305 * @return ArrayCollection<Entry>
307 public function getClients()
309 return $this->clients
;
313 * 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).
317 public function getFirstClient()
319 if (!empty($this->clients
)) {
320 return $this->clients
->first();