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