Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
48.60% |
52 / 107 |
|
55.93% |
33 / 59 |
CRAP | |
0.00% |
0 / 1 |
User | |
48.60% |
52 / 107 |
|
55.93% |
33 / 59 |
1164.76 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
hasAccessTo | |
30.00% |
6 / 20 |
|
0.00% |
0 / 1 |
129.13 | |||
getFirstName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setFirstName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLastName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setLastName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFullName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEmail | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setEmail | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getUsername | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPassword | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPassword | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPlainPassword | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPlainPassword | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
eraseCredentials | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getForgetPasswordToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setForgetPasswordToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getSalt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRoles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRoles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isEnabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setEnabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCollectivity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setCollectivity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLastLogin | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setLastLogin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getServices | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setServices | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCollectivitesReferees | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setCollectivitesReferees | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getApiAuthorized | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setApiAuthorized | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isInUserServices | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getFavoriteDocuments | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setFavoriteDocuments | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getDocuments | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setDocuments | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
addFavoriteDocument | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
removeFavoriteDocument | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
isDocumentView | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setDocumentView | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMoreInfos | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setMoreInfos | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getEmailNotificationPreference | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setEmailNotificationPreference | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNotifications | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setNotifications | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSsoKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setSsoKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isNotGeneratesNotifications | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setNotGeneratesNotifications | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
hasModuleTools | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasModuleConformiteTraitement | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasModuleConformiteOrganisation | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasServices | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasModule | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
8.83 | |||
getUserIdentifier | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This file is part of the MADIS - RGPD Management application. |
5 | * |
6 | * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify |
9 | * it under the terms of the GNU Affero General Public License as published by |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * (at your option) any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU Affero General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Affero General Public License |
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
20 | */ |
21 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Domain\User\Model; |
25 | |
26 | use App\Application\Interfaces\CollectivityRelated; |
27 | use App\Application\Traits\Model\HistoryTrait; |
28 | use App\Application\Traits\Model\SoftDeletableTrait; |
29 | use App\Domain\Documentation\Model\Document; |
30 | use App\Domain\Reporting\Model\LoggableSubject; |
31 | use App\Domain\User\Dictionary\UserRoleDictionary; |
32 | use Doctrine\Common\Collections\ArrayCollection; |
33 | use Doctrine\Common\Collections\Collection; |
34 | use Ramsey\Uuid\Uuid; |
35 | use Ramsey\Uuid\UuidInterface; |
36 | use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; |
37 | use Symfony\Component\Security\Core\User\UserInterface; |
38 | |
39 | class User implements LoggableSubject, UserInterface, CollectivityRelated, PasswordAuthenticatedUserInterface |
40 | { |
41 | use SoftDeletableTrait; |
42 | use HistoryTrait; |
43 | |
44 | /** |
45 | * @var UuidInterface |
46 | */ |
47 | private $id; |
48 | |
49 | /** |
50 | * @var string|null |
51 | */ |
52 | private $firstName; |
53 | |
54 | /** |
55 | * @var string|null |
56 | */ |
57 | private $lastName; |
58 | |
59 | /** |
60 | * @var string|null |
61 | */ |
62 | private $email; |
63 | |
64 | /** |
65 | * @var string|null |
66 | */ |
67 | private $password; |
68 | |
69 | /** |
70 | * @var string|null |
71 | */ |
72 | private $plainPassword; |
73 | |
74 | /** |
75 | * @var string|null |
76 | */ |
77 | private $forgetPasswordToken; |
78 | |
79 | /** |
80 | * @var array |
81 | */ |
82 | private $roles; |
83 | |
84 | /** |
85 | * @var bool |
86 | */ |
87 | private $enabled; |
88 | |
89 | /** |
90 | * @var Collectivity |
91 | */ |
92 | private $collectivity; |
93 | |
94 | /** |
95 | * @var Collection|Service[] |
96 | */ |
97 | private $services; |
98 | |
99 | /** |
100 | * @var iterable |
101 | */ |
102 | private $collectivitesReferees; |
103 | |
104 | /** |
105 | * @var \DateTimeImmutable|null |
106 | */ |
107 | private $lastLogin; |
108 | |
109 | /** |
110 | * @var bool |
111 | */ |
112 | private $apiAuthorized; |
113 | |
114 | /** |
115 | * @var Collection|null |
116 | */ |
117 | private $favoriteDocuments; |
118 | |
119 | /** |
120 | * @var Collection|null |
121 | */ |
122 | private $documents; |
123 | |
124 | /** |
125 | * @var bool |
126 | */ |
127 | private $documentView; |
128 | |
129 | /** |
130 | * @var array|null |
131 | */ |
132 | private $moreInfos; |
133 | |
134 | private ?EmailNotificationPreference $emailNotificationPreference; |
135 | |
136 | /** |
137 | * @var Collection|array|null |
138 | */ |
139 | private $notifications; |
140 | |
141 | /** |
142 | * @var string|null |
143 | */ |
144 | private $ssoKey; |
145 | |
146 | private ?bool $notGeneratesNotifications; |
147 | |
148 | /** |
149 | * User constructor. |
150 | * |
151 | * @throws \Exception |
152 | */ |
153 | public function __construct() |
154 | { |
155 | $this->id = Uuid::uuid4(); |
156 | $this->roles = []; |
157 | $this->enabled = true; |
158 | $this->collectivitesReferees = []; |
159 | $this->moreInfos = []; |
160 | $this->emailNotificationPreference = new EmailNotificationPreference(); |
161 | $this->notGeneratesNotifications = false; |
162 | } |
163 | |
164 | public function getId(): UuidInterface |
165 | { |
166 | return $this->id; |
167 | } |
168 | |
169 | public function __toString(): string |
170 | { |
171 | if (\is_null($this->getFullName())) { |
172 | return ''; |
173 | } |
174 | |
175 | if (\mb_strlen($this->getFullName()) > 150) { |
176 | return \mb_substr($this->getFullName(), 0, 150) . '...'; |
177 | } |
178 | |
179 | return $this->getFullName(); |
180 | } |
181 | |
182 | /** |
183 | * Checks if this user has access to the object or not. |
184 | */ |
185 | public function hasAccessTo($object, $checkService = true): bool |
186 | { |
187 | $collectivity = null; |
188 | if ($object instanceof CollectivityRelated) { |
189 | $collectivity = $object->getCollectivity(); |
190 | } elseif ($object instanceof Collectivity) { |
191 | $collectivity = $object; |
192 | } elseif (method_exists(get_class($object), 'getCollectivity')) { |
193 | $collectivity = $object->getCollectivity(); |
194 | } |
195 | |
196 | // No collectivity, can be viewed by anyone |
197 | if (!$collectivity) { |
198 | return true; |
199 | } |
200 | |
201 | // Admins can view everything |
202 | if (\in_array(UserRoleDictionary::ROLE_ADMIN, $this->getRoles())) { |
203 | return true; |
204 | } |
205 | // Referents can view their collectivities |
206 | if (\in_array(UserRoleDictionary::ROLE_REFERENT, $this->getRoles()) && (in_array($collectivity, \iterable_to_array($this->getCollectivitesReferees())) || $this->getCollectivity() === $collectivity)) { |
207 | // Check service if necessary |
208 | if ($checkService && $collectivity->getIsServicesEnabled() && !$object->isInUserServices($this)) { |
209 | return false; |
210 | } |
211 | |
212 | return true; |
213 | } |
214 | // users can view their collectivity |
215 | if ((\in_array(UserRoleDictionary::ROLE_PREVIEW, $this->getRoles()) || \in_array(UserRoleDictionary::ROLE_USER, $this->getRoles())) && $this->getCollectivity() === $collectivity) { |
216 | // Check service if necessary |
217 | if ($checkService && $collectivity->getIsServicesEnabled() && !$object->isInUserServices($this)) { |
218 | return false; |
219 | } |
220 | |
221 | return true; |
222 | } |
223 | |
224 | return false; |
225 | } |
226 | |
227 | public function getFirstName(): ?string |
228 | { |
229 | return $this->firstName; |
230 | } |
231 | |
232 | public function setFirstName(?string $firstName): void |
233 | { |
234 | $this->firstName = $firstName; |
235 | } |
236 | |
237 | public function getLastName(): ?string |
238 | { |
239 | return $this->lastName; |
240 | } |
241 | |
242 | public function setLastName(?string $lastName): void |
243 | { |
244 | $this->lastName = $lastName; |
245 | } |
246 | |
247 | public function getFullName(): string |
248 | { |
249 | return "{$this->firstName} {$this->lastName}"; |
250 | } |
251 | |
252 | public function getEmail(): ?string |
253 | { |
254 | return $this->email; |
255 | } |
256 | |
257 | public function setEmail(?string $email): void |
258 | { |
259 | $this->email = $email; |
260 | } |
261 | |
262 | public function getUsername(): ?string |
263 | { |
264 | return $this->email; |
265 | } |
266 | |
267 | public function getPassword(): ?string |
268 | { |
269 | return $this->password; |
270 | } |
271 | |
272 | public function setPassword(?string $password): void |
273 | { |
274 | $this->password = $password; |
275 | } |
276 | |
277 | public function getPlainPassword(): ?string |
278 | { |
279 | return $this->plainPassword; |
280 | } |
281 | |
282 | public function setPlainPassword(?string $plainPassword): void |
283 | { |
284 | $this->plainPassword = $plainPassword; |
285 | } |
286 | |
287 | public function eraseCredentials(): void |
288 | { |
289 | $this->plainPassword = null; |
290 | } |
291 | |
292 | public function getForgetPasswordToken(): ?string |
293 | { |
294 | return $this->forgetPasswordToken; |
295 | } |
296 | |
297 | public function setForgetPasswordToken(?string $forgetPasswordToken): void |
298 | { |
299 | $this->forgetPasswordToken = $forgetPasswordToken; |
300 | } |
301 | |
302 | public function getSalt() |
303 | { |
304 | return null; |
305 | } |
306 | |
307 | public function getRoles(): array |
308 | { |
309 | return $this->roles; |
310 | } |
311 | |
312 | public function setRoles(array $roles): void |
313 | { |
314 | $this->roles = $roles; |
315 | } |
316 | |
317 | public function isEnabled(): bool |
318 | { |
319 | return $this->enabled; |
320 | } |
321 | |
322 | public function setEnabled(bool $enabled): void |
323 | { |
324 | $this->enabled = $enabled; |
325 | } |
326 | |
327 | public function getCollectivity(): ?Collectivity |
328 | { |
329 | return $this->collectivity; |
330 | } |
331 | |
332 | public function setCollectivity(Collectivity $collectivity): void |
333 | { |
334 | $this->collectivity = $collectivity; |
335 | } |
336 | |
337 | public function getLastLogin(): ?\DateTimeImmutable |
338 | { |
339 | return $this->lastLogin; |
340 | } |
341 | |
342 | public function setLastLogin(?\DateTimeImmutable $lastLogin): void |
343 | { |
344 | $this->lastLogin = $lastLogin; |
345 | } |
346 | |
347 | public function getServices(): ?Collection |
348 | { |
349 | return $this->services; |
350 | } |
351 | |
352 | public function setServices($services): void |
353 | { |
354 | $this->services = $services; |
355 | } |
356 | |
357 | public function getCollectivitesReferees(): iterable |
358 | { |
359 | return $this->collectivitesReferees; |
360 | } |
361 | |
362 | public function setCollectivitesReferees(iterable $collectivitesReferees): void |
363 | { |
364 | $this->collectivitesReferees = $collectivitesReferees; |
365 | } |
366 | |
367 | public function getApiAuthorized(): ?bool |
368 | { |
369 | return $this->apiAuthorized; |
370 | } |
371 | |
372 | public function setApiAuthorized(?bool $apiAuthorized): void |
373 | { |
374 | $this->apiAuthorized = $apiAuthorized; |
375 | } |
376 | |
377 | public function isInUserServices(User $user): bool |
378 | { |
379 | if (false == $user->getCollectivity()->getIsServicesEnabled()) { |
380 | return true; |
381 | } |
382 | |
383 | $result = false; |
384 | |
385 | if ($user->getServices() === $this->getServices()) { |
386 | $result = true; |
387 | } |
388 | |
389 | return $result; |
390 | } |
391 | |
392 | public function getFavoriteDocuments(): ?Collection |
393 | { |
394 | return $this->favoriteDocuments; |
395 | } |
396 | |
397 | public function setFavoriteDocuments(?Collection $favoriteDocuments): User |
398 | { |
399 | $this->favoriteDocuments = $favoriteDocuments; |
400 | |
401 | return $this; |
402 | } |
403 | |
404 | public function getDocuments(): ?Collection |
405 | { |
406 | return $this->documents; |
407 | } |
408 | |
409 | public function setDocuments(?Collection $documents): User |
410 | { |
411 | $this->documents = $documents; |
412 | |
413 | return $this; |
414 | } |
415 | |
416 | public function addFavoriteDocument(Document $doc): User |
417 | { |
418 | if (null === $this->favoriteDocuments) { |
419 | $this->favoriteDocuments = new ArrayCollection(); |
420 | } |
421 | if (!$this->favoriteDocuments->contains($doc)) { |
422 | $this->favoriteDocuments->add($doc); |
423 | } |
424 | |
425 | return $this; |
426 | } |
427 | |
428 | public function removeFavoriteDocument(Document $doc): User |
429 | { |
430 | if (null !== $this->favoriteDocuments && !$this->favoriteDocuments->contains($doc)) { |
431 | $this->favoriteDocuments->removeElement($doc); |
432 | } |
433 | |
434 | return $this; |
435 | } |
436 | |
437 | public function isDocumentView(): ?bool |
438 | { |
439 | return $this->documentView; |
440 | } |
441 | |
442 | public function setDocumentView(bool $documentView): void |
443 | { |
444 | $this->documentView = $documentView; |
445 | } |
446 | |
447 | public function getMoreInfos(): ?array |
448 | { |
449 | return $this->moreInfos; |
450 | } |
451 | |
452 | public function setMoreInfos(array $moreInfos): void |
453 | { |
454 | $this->moreInfos = $moreInfos; |
455 | } |
456 | |
457 | public function getEmailNotificationPreference(): ?EmailNotificationPreference |
458 | { |
459 | return $this->emailNotificationPreference; |
460 | } |
461 | |
462 | public function setEmailNotificationPreference(?EmailNotificationPreference $emailNotificationPreference): void |
463 | { |
464 | $this->emailNotificationPreference = $emailNotificationPreference; |
465 | } |
466 | |
467 | public function getNotifications(): ?Collection |
468 | { |
469 | return $this->notifications; |
470 | } |
471 | |
472 | public function setNotifications(?Collection $notifications): void |
473 | { |
474 | $this->notifications = $notifications; |
475 | } |
476 | |
477 | public function getSsoKey(): ?string |
478 | { |
479 | return $this->ssoKey; |
480 | } |
481 | |
482 | public function setSsoKey(?string $ssoKey): void |
483 | { |
484 | $this->ssoKey = $ssoKey; |
485 | } |
486 | |
487 | public function isNotGeneratesNotifications(): ?bool |
488 | { |
489 | return $this->notGeneratesNotifications; |
490 | } |
491 | |
492 | public function setNotGeneratesNotifications(?bool $notGeneratesNotifications): void |
493 | { |
494 | $this->notGeneratesNotifications = $notGeneratesNotifications; |
495 | } |
496 | |
497 | public function hasModuleTools(): bool |
498 | { |
499 | return $this->hasModule('isHasModuleTools'); |
500 | } |
501 | |
502 | public function hasModuleConformiteTraitement(): bool |
503 | { |
504 | return $this->hasModule('isHasModuleConformiteTraitement'); |
505 | } |
506 | |
507 | public function hasModuleConformiteOrganisation(): bool |
508 | { |
509 | return $this->hasModule('isHasModuleConformiteOrganisation'); |
510 | } |
511 | |
512 | public function hasServices(): bool |
513 | { |
514 | return $this->hasModule('getIsServicesEnabled'); |
515 | } |
516 | |
517 | private function hasModule($func) |
518 | { |
519 | if (in_array(UserRoleDictionary::ROLE_REFERENT, $this->getRoles())) { |
520 | foreach ($this->getCollectivitesReferees() as $collectivity) { |
521 | /** @var Collectivity $collectivity */ |
522 | if ($collectivity->{$func}()) { |
523 | return true; |
524 | } |
525 | } |
526 | } |
527 | if (in_array(UserRoleDictionary::ROLE_ADMIN, $this->getRoles())) { |
528 | return true; |
529 | } |
530 | |
531 | return $this->getCollectivity() && $this->getCollectivity()->{$func}(); |
532 | } |
533 | |
534 | public function getUserIdentifier(): string |
535 | { |
536 | return $this->email; |
537 | } |
538 | } |