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