]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Entity/User.php
Add ldap
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Entity / User.php
CommitLineData
9d50517c
NL
1<?php
2
3b68f6ca
IB
3// This permits to have the LdapUserInterface even when fr3d/ldap-bundle is not
4// in the packages
5namespace FR3D\LdapBundle\Model;
6
7interface LdapUserInterface
8{
9 public function setDn($dn);
10 public function getDn();
11}
12
1210dae1 13namespace Wallabag\UserBundle\Entity;
9d50517c 14
5f09650e 15use Doctrine\Common\Collections\ArrayCollection;
9d50517c 16use Doctrine\ORM\Mapping as ORM;
f808b016
JB
17use FOS\UserBundle\Model\User as BaseUser;
18use JMS\Serializer\Annotation\Accessor;
22510459 19use JMS\Serializer\Annotation\Groups;
5709ecb3 20use JMS\Serializer\Annotation\XmlRoot;
2db616b5
NL
21use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
22use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
619cc453
JB
23use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
24use Symfony\Component\Security\Core\User\UserInterface;
23406ca3 25use Wallabag\ApiBundle\Entity\Client;
1210dae1
NL
26use Wallabag\CoreBundle\Entity\Config;
27use Wallabag\CoreBundle\Entity\Entry;
927c9e79 28use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
3b68f6ca 29use FR3D\LdapBundle\Model\LdapUserInterface;
9d50517c
NL
30
31/**
4346a860 32 * User.
9d50517c 33 *
5709ecb3 34 * @XmlRoot("user")
1210dae1 35 * @ORM\Entity(repositoryClass="Wallabag\UserBundle\Repository\UserRepository")
bd0f3d32 36 * @ORM\Table(name="`user`")
2f69eb4a 37 * @ORM\HasLifecycleCallbacks()
c844dc0c
J
38 *
39 * @UniqueEntity("email")
40 * @UniqueEntity("username")
9d50517c 41 */
3b68f6ca 42class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterface, LdapUserInterface
9d50517c 43{
927c9e79
JB
44 use EntityTimestampsTrait;
45
5709ecb3 46 /** @Serializer\XmlAttribute */
9d50517c 47 /**
4346a860 48 * @var int
9d50517c 49 *
2f69eb4a 50 * @ORM\Column(name="id", type="integer")
9d50517c 51 * @ORM\Id
2f69eb4a 52 * @ORM\GeneratedValue(strategy="AUTO")
5709ecb3 53 *
0c00e525 54 * @Groups({"user_api", "user_api_with_client"})
9d50517c 55 */
a1691859 56 protected $id;
9d50517c
NL
57
58 /**
59 * @var string
60 *
61 * @ORM\Column(name="name", type="text", nullable=true)
5709ecb3 62 *
0c00e525 63 * @Groups({"user_api", "user_api_with_client"})
9d50517c 64 */
a1691859 65 protected $name;
6894d48e 66
5709ecb3
JB
67 /**
68 * @var string
69 *
0c00e525 70 * @Groups({"user_api", "user_api_with_client"})
5709ecb3
JB
71 */
72 protected $username;
73
74 /**
75 * @var string
76 *
0c00e525 77 * @Groups({"user_api", "user_api_with_client"})
5709ecb3
JB
78 */
79 protected $email;
80
3b68f6ca
IB
81 /**
82 * @var string
83 *
84 * @ORM\Column(name="dn", type="text", nullable=true)
85 */
86 protected $dn;
87
2f69eb4a 88 /**
c3f7a2ca 89 * @var \DateTime
2f69eb4a
NL
90 *
91 * @ORM\Column(name="created_at", type="datetime")
5709ecb3 92 *
0c00e525 93 * @Groups({"user_api", "user_api_with_client"})
2f69eb4a 94 */
a1691859 95 protected $createdAt;
2f69eb4a
NL
96
97 /**
c3f7a2ca 98 * @var \DateTime
2f69eb4a
NL
99 *
100 * @ORM\Column(name="updated_at", type="datetime")
5709ecb3 101 *
0c00e525 102 * @Groups({"user_api", "user_api_with_client"})
2f69eb4a 103 */
a1691859 104 protected $updatedAt;
2f69eb4a 105
5f09650e 106 /**
1210dae1 107 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\Entry", mappedBy="user", cascade={"remove"})
5f09650e 108 */
a1691859 109 protected $entries;
5f09650e 110
32da2a70 111 /**
152fcccd 112 * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"})
32da2a70 113 */
a1691859 114 protected $config;
32da2a70 115
9114615a
JB
116 /**
117 * @var ArrayCollection
118 *
119 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\SiteCredential", mappedBy="user", cascade={"remove"})
120 */
e50d7d31 121 protected $siteCredentials;
9114615a 122
2db616b5 123 /**
f808b016
JB
124 * @var ArrayCollection
125 *
126 * @ORM\OneToMany(targetEntity="Wallabag\ApiBundle\Entity\Client", mappedBy="user", cascade={"remove"})
2db616b5 127 */
f808b016 128 protected $clients;
2db616b5
NL
129
130 /**
f808b016 131 * @see getFirstClient() below
0c00e525 132 *
f808b016
JB
133 * @Groups({"user_api_with_client"})
134 * @Accessor(getter="getFirstClient")
2db616b5 135 */
f808b016 136 protected $default_client;
2db616b5
NL
137
138 /**
f808b016 139 * @ORM\Column(type="integer", nullable=true)
2db616b5 140 */
f808b016 141 private $authCode;
2db616b5 142
23406ca3 143 /**
f808b016 144 * @var bool
eb570e49 145 *
f808b016 146 * @ORM\Column(type="boolean")
23406ca3 147 */
f808b016 148 private $twoFactorAuthentication = false;
23406ca3 149
0c00e525 150 /**
f808b016 151 * @ORM\Column(type="json_array", nullable=true)
0c00e525 152 */
f808b016 153 private $trusted;
0c00e525 154
c3235553
NL
155 public function __construct()
156 {
a1691859 157 parent::__construct();
98f0929f 158 $this->entries = new ArrayCollection();
4094ea47 159 $this->roles = ['ROLE_USER'];
c3235553 160 }
2f69eb4a 161
9d50517c 162 /**
4346a860
JB
163 * Set name.
164 *
165 * @param string $name
9d50517c 166 *
2f69eb4a 167 * @return User
9d50517c
NL
168 */
169 public function setName($name)
170 {
171 $this->name = $name;
172
173 return $this;
174 }
175
176 /**
4346a860 177 * Get name.
9d50517c 178 *
7df80cb3 179 * @return string
9d50517c
NL
180 */
181 public function getName()
182 {
183 return $this->name;
184 }
185
2f69eb4a 186 /**
3a6af6c5 187 * @return \DateTime
2f69eb4a
NL
188 */
189 public function getCreatedAt()
190 {
191 return $this->createdAt;
192 }
193
194 /**
3a6af6c5 195 * @return \DateTime
2f69eb4a
NL
196 */
197 public function getUpdatedAt()
198 {
199 return $this->updatedAt;
200 }
201
5f09650e
NL
202 /**
203 * @param Entry $entry
204 *
205 * @return User
206 */
207 public function addEntry(Entry $entry)
208 {
209 $this->entries[] = $entry;
210
211 return $this;
212 }
213
214 /**
215 * @return ArrayCollection<Entry>
216 */
217 public function getEntries()
218 {
219 return $this->entries;
220 }
221
c3235553
NL
222 public function isEqualTo(UserInterface $user)
223 {
224 return $this->username === $user->getUsername();
225 }
226
32da2a70 227 /**
4346a860
JB
228 * Set config.
229 *
1210dae1 230 * @param Config $config
32da2a70 231 *
32da2a70
J
232 * @return User
233 */
1210dae1 234 public function setConfig(Config $config = null)
32da2a70
J
235 {
236 $this->config = $config;
237
238 return $this;
239 }
240
241 /**
4346a860 242 * Get config.
32da2a70 243 *
1210dae1 244 * @return Config
32da2a70
J
245 */
246 public function getConfig()
247 {
248 return $this->config;
249 }
2db616b5
NL
250
251 /**
252 * @return bool
253 */
254 public function isTwoFactorAuthentication()
255 {
256 return $this->twoFactorAuthentication;
257 }
258
259 /**
260 * @param bool $twoFactorAuthentication
261 */
262 public function setTwoFactorAuthentication($twoFactorAuthentication)
263 {
264 $this->twoFactorAuthentication = $twoFactorAuthentication;
265 }
266
267 public function isEmailAuthEnabled()
268 {
269 return $this->twoFactorAuthentication;
270 }
271
272 public function getEmailAuthCode()
273 {
274 return $this->authCode;
275 }
276
277 public function setEmailAuthCode($authCode)
278 {
279 $this->authCode = $authCode;
280 }
281
282 public function addTrustedComputer($token, \DateTime $validUntil)
283 {
284 $this->trusted[$token] = $validUntil->format('r');
285 }
286
287 public function isTrustedComputer($token)
288 {
289 if (isset($this->trusted[$token])) {
290 $now = new \DateTime();
291 $validUntil = new \DateTime($this->trusted[$token]);
292
293 return $now < $validUntil;
294 }
295
296 return false;
297 }
23406ca3
NL
298
299 /**
300 * @param Client $client
301 *
302 * @return User
303 */
304 public function addClient(Client $client)
305 {
306 $this->clients[] = $client;
307
308 return $this;
309 }
310
311 /**
312 * @return ArrayCollection<Entry>
313 */
314 public function getClients()
315 {
316 return $this->clients;
317 }
0c00e525
JB
318
319 /**
320 * 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).
321 *
322 * @return Client
323 */
324 public function getFirstClient()
325 {
eb570e49
JB
326 if (!empty($this->clients)) {
327 return $this->clients->first();
0c00e525 328 }
0c00e525 329 }
3b68f6ca
IB
330
331 /**
332 * Set dn.
333 *
334 * @param string $dn
335 *
336 * @return User
337 */
338 public function setDn($dn)
339 {
340 $this->dn = $dn;
341
342 return $this;
343 }
344
345 /**
346 * Get dn.
347 *
348 * @return string
349 */
350 public function getDn()
351 {
352 return $this->dn;
353 }
354
355 public function isLdapUser()
356 {
357 return $this->dn !== null;
358 }
9d50517c 359}