| Total Complexity | 65 |
| Total Lines | 813 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 86 | class User extends AttachmentContainingDBElement implements UserInterface, HasPermissionsInterface, TwoFactorInterface, BackupCodeInterface, TrustedDeviceInterface, U2FTwoFactorInterface, PreferredProviderInterface |
||
| 87 | { |
||
| 88 | //use MasterAttachmentTrait; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The User id of the anonymous user. |
||
| 92 | */ |
||
| 93 | public const ID_ANONYMOUS = 1; |
||
| 94 | |||
| 95 | public const AVAILABLE_THEMES = ['bootstrap', 'cerulean', 'cosmo', 'cyborg', 'darkly', 'flatly', 'journal', |
||
| 96 | 'litera', 'lumen', 'lux', 'materia', 'minty', 'pulse', 'sandstone', 'simplex', 'sketchy', 'slate', 'solar', |
||
| 97 | 'spacelab', 'united', 'yeti', ]; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var bool Determines if the user is disabled (user can not log in) |
||
| 101 | * @ORM\Column(type="boolean") |
||
| 102 | */ |
||
| 103 | protected $disabled = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string|null The theme |
||
| 107 | * @ORM\Column(type="string", name="config_theme", nullable=true) |
||
| 108 | * @Assert\Choice(choices=User::AVAILABLE_THEMES) |
||
| 109 | */ |
||
| 110 | protected $theme = ''; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string|null The hash of a token the user must provide when he wants to reset his password. |
||
| 114 | * @ORM\Column(type="string", nullable=true) |
||
| 115 | */ |
||
| 116 | protected $pw_reset_token; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @ORM\Column(type="text", name="config_instock_comment_a") |
||
| 120 | */ |
||
| 121 | protected $instock_comment_a = ''; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @ORM\Column(type="text", name="config_instock_comment_w") |
||
| 125 | */ |
||
| 126 | protected $instock_comment_w = ''; |
||
| 127 | |||
| 128 | /** @var int The version of the trusted device cookie. Used to invalidate all trusted device cookies at once. |
||
| 129 | * @ORM\Column(type="integer") |
||
| 130 | */ |
||
| 131 | protected $trustedDeviceCookieVersion = 0; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var string[]|null A list of backup codes that can be used, if the user has no access to its Google Authenticator device |
||
| 135 | * @ORM\Column(type="json") |
||
| 136 | */ |
||
| 137 | protected $backupCodes = []; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @ORM\Id() |
||
| 141 | * @ORM\GeneratedValue() |
||
| 142 | * @ORM\Column(type="integer") |
||
| 143 | */ |
||
| 144 | protected $id; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var Group|null the group this user belongs to |
||
| 148 | * @ORM\ManyToOne(targetEntity="Group", inversedBy="users", fetch="EAGER") |
||
| 149 | * @ORM\JoinColumn(name="group_id", referencedColumnName="id") |
||
| 150 | * @Selectable() |
||
| 151 | */ |
||
| 152 | protected $group; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var string|null The secret used for google authenticator |
||
| 156 | * @ORM\Column(name="google_authenticator_secret", type="string", nullable=true) |
||
| 157 | */ |
||
| 158 | protected $googleAuthenticatorSecret; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var string|null The timezone the user prefers |
||
| 162 | * @ORM\Column(type="string", name="config_timezone", nullable=true) |
||
| 163 | * @Assert\Timezone() |
||
| 164 | */ |
||
| 165 | protected $timezone = ''; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var string|null The language/locale the user prefers |
||
| 169 | * @ORM\Column(type="string", name="config_language", nullable=true) |
||
| 170 | * @Assert\Language() |
||
| 171 | */ |
||
| 172 | protected $language = ''; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var string|null The email address of the user |
||
| 176 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 177 | * @Assert\Email() |
||
| 178 | */ |
||
| 179 | protected $email = ''; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var string|null The department the user is working |
||
| 183 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 184 | */ |
||
| 185 | protected $department = ''; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var string|null The last name of the User |
||
| 189 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 190 | */ |
||
| 191 | protected $last_name = ''; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var string|null The first name of the User |
||
| 195 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 196 | */ |
||
| 197 | protected $first_name = ''; |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @var bool True if the user needs to change password after log in |
||
| 201 | * @ORM\Column(type="boolean") |
||
| 202 | */ |
||
| 203 | protected $need_pw_change = true; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * //@ORM\Column(type="json"). |
||
| 207 | */ |
||
| 208 | //protected $roles = []; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @var string|null The hashed password |
||
| 212 | * @ORM\Column(type="string", nullable=true) |
||
| 213 | */ |
||
| 214 | protected $password; |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @ORM\Column(type="string", length=180, unique=true) |
||
| 218 | * @Assert\NotBlank |
||
| 219 | * @Assert\Regex("/^[\w\.\+\-\$]+$/", message="user.invalid_username") |
||
| 220 | */ |
||
| 221 | protected $name = ''; |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @var array |
||
| 225 | * @ORM\Column(type="json") |
||
| 226 | */ |
||
| 227 | protected $settings = []; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @var Collection<int, UserAttachment> |
||
| 231 | * @ORM\OneToMany(targetEntity="App\Entity\Attachments\UserAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) |
||
| 232 | * @ORM\OrderBy({"name" = "ASC"}) |
||
| 233 | */ |
||
| 234 | protected $attachments; |
||
| 235 | |||
| 236 | /** @var DateTime|null The time when the backup codes were generated |
||
| 237 | * @ORM\Column(type="datetime", nullable=true) |
||
| 238 | */ |
||
| 239 | protected $backupCodesGenerationDate; |
||
| 240 | |||
| 241 | /** @var Collection<int, TwoFactorKeyInterface> |
||
| 242 | * @ORM\OneToMany(targetEntity="App\Entity\UserSystem\U2FKey", mappedBy="user", cascade={"REMOVE"}, orphanRemoval=true) |
||
| 243 | */ |
||
| 244 | protected $u2fKeys; |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @var Currency|null The currency the user wants to see prices in. |
||
| 248 | * Dont use fetch=EAGER here, this will cause problems with setting the currency setting. |
||
| 249 | * TODO: This is most likely a bug in doctrine/symfony related to the UniqueEntity constraint (it makes a db call). |
||
| 250 | * TODO: Find a way to use fetch EAGER (this improves performance a bit) |
||
| 251 | * @ORM\ManyToOne(targetEntity="App\Entity\PriceInformations\Currency") |
||
| 252 | * @ORM\JoinColumn(name="currency_id", referencedColumnName="id") |
||
| 253 | * @Selectable() |
||
| 254 | */ |
||
| 255 | protected $currency; |
||
| 256 | |||
| 257 | /** @var PermissionsEmbed |
||
| 258 | * @ORM\Embedded(class="PermissionsEmbed", columnPrefix="perms_") |
||
| 259 | * @ValidPermission() |
||
| 260 | */ |
||
| 261 | protected $permissions; |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @var DateTime The time until the password reset token is valid. |
||
| 265 | * @ORM\Column(type="datetime", nullable=true) |
||
| 266 | */ |
||
| 267 | protected $pw_reset_expires; |
||
| 268 | |||
| 269 | public function __construct() |
||
| 270 | { |
||
| 271 | parent::__construct(); |
||
| 272 | $this->permissions = new PermissionsEmbed(); |
||
| 273 | $this->u2fKeys = new ArrayCollection(); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns a string representation of this user (the full name). |
||
| 278 | * E.g. 'Jane Doe (j.doe) [DISABLED]. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function __toString() |
||
| 283 | { |
||
| 284 | $tmp = $this->isDisabled() ? ' [DISABLED]' : ''; |
||
| 285 | |||
| 286 | return $this->getFullName(true).$tmp; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Checks if the current user, is the user which represents the not logged in (anonymous) users. |
||
| 291 | * |
||
| 292 | * @return bool true if this user is the anonymous user |
||
| 293 | */ |
||
| 294 | public function isAnonymousUser(): bool |
||
| 295 | { |
||
| 296 | return $this->id === static::ID_ANONYMOUS && 'anonymous' === $this->name; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * A visual identifier that represents this user. |
||
| 301 | * |
||
| 302 | * @see UserInterface |
||
| 303 | */ |
||
| 304 | public function getUsername(): string |
||
| 305 | { |
||
| 306 | return (string) $this->name; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @see UserInterface |
||
| 311 | */ |
||
| 312 | public function getRoles(): array |
||
| 313 | { |
||
| 314 | $roles = []; |
||
| 315 | //$roles = $this->roles; |
||
| 316 | // guarantee every user at least has ROLE_USER |
||
| 317 | $roles[] = 'ROLE_USER'; |
||
| 318 | |||
| 319 | return array_unique($roles); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function setRoles(array $roles): self |
||
|
|
|||
| 323 | { |
||
| 324 | //$this->roles = $roles; |
||
| 325 | |||
| 326 | return $this; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @see UserInterface |
||
| 331 | * Gets the password hash for this entity. |
||
| 332 | */ |
||
| 333 | public function getPassword(): string |
||
| 334 | { |
||
| 335 | return (string) $this->password; |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Sets the password hash for this user. |
||
| 340 | * |
||
| 341 | * @return User |
||
| 342 | */ |
||
| 343 | public function setPassword(string $password): self |
||
| 344 | { |
||
| 345 | $this->password = $password; |
||
| 346 | |||
| 347 | return $this; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @see UserInterface |
||
| 352 | */ |
||
| 353 | public function getSalt(): ?string |
||
| 354 | { |
||
| 355 | return null; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @see UserInterface |
||
| 360 | */ |
||
| 361 | public function eraseCredentials(): void |
||
| 362 | { |
||
| 363 | // If you store any temporary, sensitive data on the user, clear it here |
||
| 364 | // $this->plainPassword = null; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Gets the currency the user prefers when showing him prices. |
||
| 369 | * |
||
| 370 | * @return Currency|null The currency the user prefers, or null if the global currency should be used. |
||
| 371 | */ |
||
| 372 | public function getCurrency(): ?Currency |
||
| 373 | { |
||
| 374 | return $this->currency; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Sets the currency the users prefers to see prices in. |
||
| 379 | * |
||
| 380 | * @return User |
||
| 381 | */ |
||
| 382 | public function setCurrency(?Currency $currency): self |
||
| 383 | { |
||
| 384 | $this->currency = $currency; |
||
| 385 | |||
| 386 | return $this; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Checks if this user is disabled (user cannot login any more). |
||
| 391 | * |
||
| 392 | * @return bool True, if the user is disabled. |
||
| 393 | */ |
||
| 394 | public function isDisabled(): bool |
||
| 395 | { |
||
| 396 | return $this->disabled; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Sets the status if a user is disabled. |
||
| 401 | * |
||
| 402 | * @param bool $disabled True if the user should be disabled. |
||
| 403 | * |
||
| 404 | * @return User |
||
| 405 | */ |
||
| 406 | public function setDisabled(bool $disabled): self |
||
| 407 | { |
||
| 408 | $this->disabled = $disabled; |
||
| 409 | |||
| 410 | return $this; |
||
| 411 | } |
||
| 412 | |||
| 413 | |||
| 414 | public function getPermissions(): PermissionsEmbed |
||
| 415 | { |
||
| 416 | return $this->permissions; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Check if the user needs a password change. |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function isNeedPwChange(): bool |
||
| 425 | { |
||
| 426 | return $this->need_pw_change; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set the status, if the user needs a password change. |
||
| 431 | * |
||
| 432 | * @return User |
||
| 433 | */ |
||
| 434 | public function setNeedPwChange(bool $need_pw_change): self |
||
| 435 | { |
||
| 436 | $this->need_pw_change = $need_pw_change; |
||
| 437 | |||
| 438 | return $this; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Returns the encrypted password reset token. |
||
| 443 | * |
||
| 444 | * @return string|null |
||
| 445 | */ |
||
| 446 | public function getPwResetToken(): ?string |
||
| 447 | { |
||
| 448 | return $this->pw_reset_token; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Sets the encrypted password reset token. |
||
| 453 | * |
||
| 454 | * @return User |
||
| 455 | */ |
||
| 456 | public function setPwResetToken(?string $pw_reset_token): self |
||
| 457 | { |
||
| 458 | $this->pw_reset_token = $pw_reset_token; |
||
| 459 | |||
| 460 | return $this; |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Gets the datetime when the password reset token expires. |
||
| 465 | * |
||
| 466 | * @return DateTime |
||
| 467 | */ |
||
| 468 | public function getPwResetExpires(): DateTime |
||
| 469 | { |
||
| 470 | return $this->pw_reset_expires; |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets the datetime when the password reset token expires. |
||
| 475 | * |
||
| 476 | * @return User |
||
| 477 | */ |
||
| 478 | public function setPwResetExpires(DateTime $pw_reset_expires): self |
||
| 479 | { |
||
| 480 | $this->pw_reset_expires = $pw_reset_expires; |
||
| 481 | |||
| 482 | return $this; |
||
| 483 | } |
||
| 484 | |||
| 485 | /************************************************ |
||
| 486 | * Getters |
||
| 487 | ************************************************/ |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns the full name in the format FIRSTNAME LASTNAME [(USERNAME)]. |
||
| 491 | * Example: Max Muster (m.muster). |
||
| 492 | * |
||
| 493 | * @param bool $including_username include the username in the full name |
||
| 494 | * |
||
| 495 | * @return string a string with the full name of this user |
||
| 496 | */ |
||
| 497 | public function getFullName(bool $including_username = false): string |
||
| 498 | { |
||
| 499 | $tmp = $this->getFirstName(); |
||
| 500 | //Dont add a space, if the name has only one part (it would look strange) |
||
| 501 | if (! empty($this->getFirstName()) && ! empty($this->getLastName())) { |
||
| 502 | $tmp .= ' '; |
||
| 503 | } |
||
| 504 | $tmp .= $this->getLastName(); |
||
| 505 | |||
| 506 | if ($including_username) { |
||
| 507 | $tmp .= sprintf(' (@%s)', $this->getName()); |
||
| 508 | } |
||
| 509 | |||
| 510 | return $tmp; |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Change the username of this user. |
||
| 515 | * |
||
| 516 | * @param string $new_name The new username. |
||
| 517 | * |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | public function setName(string $new_name): AbstractNamedDBElement |
||
| 521 | { |
||
| 522 | // Anonymous user is not allowed to change its username |
||
| 523 | if (! $this->isAnonymousUser()) { |
||
| 524 | $this->name = $new_name; |
||
| 525 | } |
||
| 526 | |||
| 527 | return $this; |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Get the first name of the user. |
||
| 532 | * |
||
| 533 | * @return string|null |
||
| 534 | */ |
||
| 535 | public function getFirstName(): ?string |
||
| 536 | { |
||
| 537 | return $this->first_name; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Change the first name of the user. |
||
| 542 | * |
||
| 543 | * @param string $first_name The new first name |
||
| 544 | * |
||
| 545 | * @return $this |
||
| 546 | */ |
||
| 547 | public function setFirstName(?string $first_name): self |
||
| 548 | { |
||
| 549 | $this->first_name = $first_name; |
||
| 550 | |||
| 551 | return $this; |
||
| 552 | } |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Get the last name of the user. |
||
| 556 | * |
||
| 557 | * @return string|null |
||
| 558 | */ |
||
| 559 | public function getLastName(): ?string |
||
| 560 | { |
||
| 561 | return $this->last_name; |
||
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Change the last name of the user. |
||
| 566 | * |
||
| 567 | * @param string $last_name The new last name |
||
| 568 | * |
||
| 569 | * @return $this |
||
| 570 | */ |
||
| 571 | public function setLastName(?string $last_name): self |
||
| 572 | { |
||
| 573 | $this->last_name = $last_name; |
||
| 574 | |||
| 575 | return $this; |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Gets the department of this user. |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public function getDepartment(): ?string |
||
| 584 | { |
||
| 585 | return $this->department; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Change the department of the user. |
||
| 590 | * |
||
| 591 | * @param string $department The new department |
||
| 592 | * |
||
| 593 | * @return User |
||
| 594 | */ |
||
| 595 | public function setDepartment(?string $department): self |
||
| 596 | { |
||
| 597 | $this->department = $department; |
||
| 598 | |||
| 599 | return $this; |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get the email of the user. |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function getEmail(): ?string |
||
| 608 | { |
||
| 609 | return $this->email; |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Change the email of the user. |
||
| 614 | * |
||
| 615 | * @param string $email The new email adress |
||
| 616 | * |
||
| 617 | * @return $this |
||
| 618 | */ |
||
| 619 | public function setEmail(?string $email): self |
||
| 620 | { |
||
| 621 | $this->email = $email; |
||
| 622 | |||
| 623 | return $this; |
||
| 624 | } |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Gets the language the user prefers (as 2 letter ISO code). |
||
| 628 | * |
||
| 629 | * @return string|null The 2 letter ISO code of the preferred language (e.g. 'en' or 'de'). |
||
| 630 | * If null is returned, the user has not specified a language and the server wide language should be used. |
||
| 631 | */ |
||
| 632 | public function getLanguage(): ?string |
||
| 633 | { |
||
| 634 | return $this->language; |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Change the language the user prefers. |
||
| 639 | * |
||
| 640 | * @param string|null $language The new language as 2 letter ISO code (e.g. 'en' or 'de'). |
||
| 641 | * Set to null, to use the system wide language. |
||
| 642 | * |
||
| 643 | * @return User |
||
| 644 | */ |
||
| 645 | public function setLanguage(?string $language): self |
||
| 646 | { |
||
| 647 | $this->language = $language; |
||
| 648 | |||
| 649 | return $this; |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Gets the timezone of the user. |
||
| 654 | * |
||
| 655 | * @return string|null The timezone of the user (e.g. 'Europe/Berlin') or null if the user has not specified |
||
| 656 | * a timezone (then the global one should be used) |
||
| 657 | */ |
||
| 658 | public function getTimezone(): ?string |
||
| 659 | { |
||
| 660 | return $this->timezone; |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Change the timezone of this user. |
||
| 665 | * |
||
| 666 | * @return $this |
||
| 667 | */ |
||
| 668 | public function setTimezone(?string $timezone): self |
||
| 669 | { |
||
| 670 | $this->timezone = $timezone; |
||
| 671 | |||
| 672 | return $this; |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Gets the theme the users wants to see. See self::AVAILABLE_THEMES for valid values. |
||
| 677 | * |
||
| 678 | * @return string|null The name of the theme the user wants to see, or null if the system wide should be used. |
||
| 679 | */ |
||
| 680 | public function getTheme(): ?string |
||
| 681 | { |
||
| 682 | return $this->theme; |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Change the theme the user wants to see. |
||
| 687 | * |
||
| 688 | * @param string|null $theme The name of the theme (See See self::AVAILABLE_THEMES for valid values). Set to null |
||
| 689 | * if the system wide theme should be used. |
||
| 690 | * |
||
| 691 | * @return $this |
||
| 692 | */ |
||
| 693 | public function setTheme(?string $theme): self |
||
| 694 | { |
||
| 695 | $this->theme = $theme; |
||
| 696 | |||
| 697 | return $this; |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Gets the group to which this user belongs to. |
||
| 702 | * |
||
| 703 | * @return Group|null The group of this user. Null if this user does not have a group. |
||
| 704 | */ |
||
| 705 | public function getGroup(): ?Group |
||
| 706 | { |
||
| 707 | return $this->group; |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Sets the group of this user. |
||
| 712 | * |
||
| 713 | * @param Group|null $group The new group of this user. Set to null if this user should not have a group. |
||
| 714 | * |
||
| 715 | * @return $this |
||
| 716 | */ |
||
| 717 | public function setGroup(?Group $group): self |
||
| 718 | { |
||
| 719 | $this->group = $group; |
||
| 720 | |||
| 721 | return $this; |
||
| 722 | } |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Return true if the user should do two-factor authentication. |
||
| 726 | * |
||
| 727 | * @return bool |
||
| 728 | */ |
||
| 729 | public function isGoogleAuthenticatorEnabled(): bool |
||
| 730 | { |
||
| 731 | return $this->googleAuthenticatorSecret ? true : false; |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Return the user name that should be shown in Google Authenticator. |
||
| 736 | * |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | public function getGoogleAuthenticatorUsername(): string |
||
| 740 | { |
||
| 741 | return $this->getUsername(); |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Return the Google Authenticator secret |
||
| 746 | * When an empty string is returned, the Google authentication is disabled. |
||
| 747 | * |
||
| 748 | * @return string|null |
||
| 749 | */ |
||
| 750 | public function getGoogleAuthenticatorSecret(): ?string |
||
| 751 | { |
||
| 752 | return $this->googleAuthenticatorSecret; |
||
| 753 | } |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Sets the secret used for Google Authenticator. Set to null to disable Google Authenticator. |
||
| 757 | * |
||
| 758 | * @return $this |
||
| 759 | */ |
||
| 760 | public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): self |
||
| 761 | { |
||
| 762 | $this->googleAuthenticatorSecret = $googleAuthenticatorSecret; |
||
| 763 | |||
| 764 | return $this; |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Check if the given code is a valid backup code. |
||
| 769 | * |
||
| 770 | * @param string $code The code that should be checked. |
||
| 771 | * |
||
| 772 | * @return bool True if the backup code is valid. |
||
| 773 | */ |
||
| 774 | public function isBackupCode(string $code): bool |
||
| 775 | { |
||
| 776 | return in_array($code, $this->backupCodes, true); |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Invalidate a backup code. |
||
| 781 | * |
||
| 782 | * @param string $code The code that should be invalidated |
||
| 783 | */ |
||
| 784 | public function invalidateBackupCode(string $code): void |
||
| 785 | { |
||
| 786 | $key = array_search($code, $this->backupCodes, true); |
||
| 787 | if (false !== $key) { |
||
| 788 | unset($this->backupCodes[$key]); |
||
| 789 | } |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Returns the list of all valid backup codes. |
||
| 794 | * |
||
| 795 | * @return string[] An array with all backup codes |
||
| 796 | */ |
||
| 797 | public function getBackupCodes(): array |
||
| 798 | { |
||
| 799 | return $this->backupCodes ?? []; |
||
| 800 | } |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Set the backup codes for this user. Existing backup codes are overridden. |
||
| 804 | * |
||
| 805 | * @param string[] $codes An array containing the backup codes |
||
| 806 | * |
||
| 807 | * @return $this |
||
| 808 | * |
||
| 809 | * @throws Exception If an error with the datetime occurs |
||
| 810 | */ |
||
| 811 | public function setBackupCodes(array $codes): self |
||
| 812 | { |
||
| 813 | $this->backupCodes = $codes; |
||
| 814 | if (empty($codes)) { |
||
| 815 | $this->backupCodesGenerationDate = null; |
||
| 816 | } else { |
||
| 817 | $this->backupCodesGenerationDate = new DateTime(); |
||
| 818 | } |
||
| 819 | |||
| 820 | return $this; |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Return the date when the backup codes were generated. |
||
| 825 | * |
||
| 826 | * @return DateTime|null |
||
| 827 | */ |
||
| 828 | public function getBackupCodesGenerationDate(): ?DateTime |
||
| 829 | { |
||
| 830 | return $this->backupCodesGenerationDate; |
||
| 831 | } |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Return version for the trusted device token. Increase version to invalidate all trusted token of the user. |
||
| 835 | * |
||
| 836 | * @return int The version of trusted device token |
||
| 837 | */ |
||
| 838 | public function getTrustedTokenVersion(): int |
||
| 839 | { |
||
| 840 | return $this->trustedDeviceCookieVersion; |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Invalidate all trusted device tokens at once, by incrementing the token version. |
||
| 845 | * You have to flush the changes to database afterwards. |
||
| 846 | */ |
||
| 847 | public function invalidateTrustedDeviceTokens(): void |
||
| 848 | { |
||
| 849 | ++$this->trustedDeviceCookieVersion; |
||
| 850 | } |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Check if U2F is enabled. |
||
| 854 | * |
||
| 855 | * @return bool |
||
| 856 | */ |
||
| 857 | public function isU2FAuthEnabled(): bool |
||
| 858 | { |
||
| 859 | return count($this->u2fKeys) > 0; |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Get all U2F Keys that are associated with this user. |
||
| 864 | * |
||
| 865 | * @return Collection |
||
| 866 | * |
||
| 867 | * @psalm-return Collection<int, TwoFactorKeyInterface> |
||
| 868 | */ |
||
| 869 | public function getU2FKeys(): Collection |
||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Add a U2F key to this user. |
||
| 876 | */ |
||
| 877 | public function addU2FKey(TwoFactorKeyInterface $key): void |
||
| 878 | { |
||
| 879 | $this->u2fKeys->add($key); |
||
| 880 | } |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Remove a U2F key from this user. |
||
| 884 | */ |
||
| 885 | public function removeU2FKey(TwoFactorKeyInterface $key): void |
||
| 888 | } |
||
| 889 | |||
| 890 | public function getPreferredTwoFactorProvider(): ?string |
||
| 899 | } |
||
| 900 | } |
||
| 901 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.