]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
Merge pull request #3153 from wallabag/content-proxy-refactor
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
CommitLineData
9d50517c
NL
1<?php
2
1210dae1 3namespace Wallabag\UserBundle\Entity;
9d50517c 4
5f09650e 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
2db616b5
NL
7use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
8use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
619cc453 9use FOS\UserBundle\Model\User as BaseUser;
0f006880
NL
10use JMS\Serializer\Annotation\ExclusionPolicy;
11use JMS\Serializer\Annotation\Expose;
619cc453
JB
12use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
13use Symfony\Component\Security\Core\User\UserInterface;
23406ca3 14use Wallabag\ApiBundle\Entity\Client;
1210dae1
NL
15use Wallabag\CoreBundle\Entity\Config;
16use Wallabag\CoreBundle\Entity\Entry;
9d50517c
NL
17
18/**
4346a860 19 * User.
9d50517c 20 *
1210dae1 21 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
bd0f3d32 22 * @ORM\Table(name="`user`")
2f69eb4a 23 * @ORM\HasLifecycleCallbacks()
0f006880 24 * @ExclusionPolicy("all")
c844dc0c
J
25 *
26 * @UniqueEntity("email")
27 * @UniqueEntity("username")
9d50517c 28 */
2db616b5 29class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface
9d50517c
NL
30{
31 /**
4346a860 32 * @var int
9d50517c 33 *
0f006880 34 * @Expose
2f69eb4a 35 * @ORM\Column(name="id", type="integer")
9d50517c 36 * @ORM\Id
2f69eb4a 37 * @ORM\GeneratedValue(strategy="AUTO")
9d50517c 38 */
a1691859 39 protected $id;
9d50517c
NL
40
41 /**
42 * @var string
43 *
44 * @ORM\Column(name="name", type="text", nullable=true)
45 */
a1691859 46 protected $name;
6894d48e 47
2f69eb4a
NL
48 /**
49 * @var date
50 *
51 * @ORM\Column(name="created_at", type="datetime")
52 */
a1691859 53 protected $createdAt;
2f69eb4a
NL
54
55 /**
56 * @var date
57 *
58 * @ORM\Column(name="updated_at", type="datetime")
59 */
a1691859 60 protected $updatedAt;
2f69eb4a 61
5f09650e 62 /**
1210dae1 63 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
5f09650e 64 */
a1691859 65 protected $entries;
5f09650e 66
32da2a70 67 /**
152fcccd 68 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
32da2a70 69 */
a1691859 70 protected $config;
32da2a70 71
2db616b5
NL
72 /**
73 * @ORM\Column(type="integer", nullable=true)
74 */
75 private $authCode;
76
77 /**
78 * @var bool Enabled yes/no
79 * @ORM\Column(type="boolean")
80 */
81 private $twoFactorAuthentication = false;
82
83 /**
84 * @ORM\Column(type="json_array", nullable=true)
85 */
86 private $trusted;
87
23406ca3
NL
88 /**
89 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
90 */
91 protected $clients;
92
c3235553
NL
93 public function __construct()
94 {
a1691859 95 parent::__construct();
98f0929f 96 $this->entries = new ArrayCollection();
4094ea47 97 $this->roles = ['ROLE_USER'];
c3235553 98 }
9d50517c 99
2f69eb4a
NL
100 /**
101 * @ORM\PrePersist
102 * @ORM\PreUpdate
103 */
104 public function timestamps()
105 {
106 if (is_null($this->createdAt)) {
107 $this->createdAt = new \DateTime();
108 }
109
110 $this->updatedAt = new \DateTime();
111 }
112
9d50517c 113 /**
4346a860
JB
114 * Set name.
115 *
116 * @param string $name
9d50517c 117 *
2f69eb4a 118 * @return User
9d50517c
NL
119 */
120 public function setName($name)
121 {
122 $this->name = $name;
123
124 return $this;
125 }
126
127 /**
4346a860 128 * Get name.
9d50517c 129 *
7df80cb3 130 * @return string
9d50517c
NL
131 */
132 public function getName()
133 {
134 return $this->name;
135 }
136
2f69eb4a
NL
137 /**
138 * @return string
139 */
140 public function getCreatedAt()
141 {
142 return $this->createdAt;
143 }
144
145 /**
146 * @return string
147 */
148 public function getUpdatedAt()
149 {
150 return $this->updatedAt;
151 }
152
5f09650e
NL
153 /**
154 * @param Entry $entry
155 *
156 * @return User
157 */
158 public function addEntry(Entry $entry)
159 {
160 $this->entries[] = $entry;
161
162 return $this;
163 }
164
165 /**
166 * @return ArrayCollection<Entry>
167 */
168 public function getEntries()
169 {
170 return $this->entries;
171 }
172
c3235553
NL
173 public function isEqualTo(UserInterface $user)
174 {
175 return $this->username === $user->getUsername();
176 }
177
32da2a70 178 /**
4346a860
JB
179 * Set config.
180 *
1210dae1 181 * @param Config $config
32da2a70 182 *
32da2a70
J
183 * @return User
184 */
1210dae1 185 public function setConfig(Config $config = null)
32da2a70
J
186 {
187 $this->config = $config;
188
189 return $this;
190 }
191
192 /**
4346a860 193 * Get config.
32da2a70 194 *
1210dae1 195 * @return Config
32da2a70
J
196 */
197 public function getConfig()
198 {
199 return $this->config;
200 }
2db616b5
NL
201
202 /**
203 * @return bool
204 */
205 public function isTwoFactorAuthentication()
206 {
207 return $this->twoFactorAuthentication;
208 }
209
210 /**
211 * @param bool $twoFactorAuthentication
212 */
213 public function setTwoFactorAuthentication($twoFactorAuthentication)
214 {
215 $this->twoFactorAuthentication = $twoFactorAuthentication;
216 }
217
218 public function isEmailAuthEnabled()
219 {
220 return $this->twoFactorAuthentication;
221 }
222
223 public function getEmailAuthCode()
224 {
225 return $this->authCode;
226 }
227
228 public function setEmailAuthCode($authCode)
229 {
230 $this->authCode = $authCode;
231 }
232
233 public function addTrustedComputer($token, \DateTime $validUntil)
234 {
235 $this->trusted[$token] = $validUntil->format('r');
236 }
237
238 public function isTrustedComputer($token)
239 {
240 if (isset($this->trusted[$token])) {
241 $now = new \DateTime();
242 $validUntil = new \DateTime($this->trusted[$token]);
243
244 return $now < $validUntil;
245 }
246
247 return false;
248 }
23406ca3
NL
249
250 /**
251 * @param Client $client
252 *
253 * @return User
254 */
255 public function addClient(Client $client)
256 {
257 $this->clients[] = $client;
258
259 return $this;
260 }
261
262 /**
263 * @return ArrayCollection<Entry>
264 */
265 public function getClients()
266 {
267 return $this->clients;
268 }
9d50517c 269}