vendor/sulu/sulu/src/Sulu/Bundle/SecurityBundle/Entity/UserTwoFactor.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\SecurityBundle\Entity;
  11. use JMS\Serializer\Annotation\ExclusionPolicy;
  12. use JMS\Serializer\Annotation\Expose;
  13. use JMS\Serializer\Annotation\Groups;
  14. use JMS\Serializer\Annotation\SerializedName;
  15. use JMS\Serializer\Annotation\VirtualProperty;
  16. use Sulu\Bundle\SecurityBundle\Entity\TwoFactor\TwoFactorInterface;
  17. use Sulu\Component\Security\Authentication\UserInterface;
  18. #[ExclusionPolicy('all')]
  19. class UserTwoFactor
  20. {
  21. private int $id;
  22. private UserInterface $user;
  23. #[Expose]
  24. #[Groups(['profile'])]
  25. private ?string $method = null;
  26. private ?string $options = null;
  27. public function __construct(TwoFactorInterface $user)
  28. {
  29. /** @var UserInterface $user */
  30. $this->user = $user;
  31. }
  32. public function getMethod(): ?string
  33. {
  34. return $this->method;
  35. }
  36. #[VirtualProperty]
  37. #[SerializedName('hasBackupCodes')]
  38. #[Groups(['profile'])]
  39. public function hasBackupCodes(): bool
  40. {
  41. return [] !== ($this->getOptions()['backupCodes'] ?? []);
  42. }
  43. /**
  44. * @return static
  45. */
  46. public function setMethod(?string $twoFactorType)
  47. {
  48. $this->method = $twoFactorType;
  49. return $this;
  50. }
  51. /**
  52. * @return array{
  53. * backupCodes?: string[],
  54. * authCode?: string,
  55. * googleAuthenticatorSecret?: string,
  56. * pendingGoogleAuthenticatorSecret?: string,
  57. * googleAuthenticatorUsername?: string,
  58. * totpSecret?: string,
  59. * pendingTotpSecret?: string,
  60. * trustedVersion?: int,
  61. * }
  62. */
  63. public function getOptions(): ?array
  64. {
  65. if (null === $this->options) {
  66. return null;
  67. }
  68. /**
  69. * @var array{
  70. * backupCodes?: string[],
  71. * authCode?: string,
  72. * googleAuthenticatorSecret?: string,
  73. * pendingGoogleAuthenticatorSecret?: string,
  74. * googleAuthenticatorUsername?: string,
  75. * totpSecret?: string,
  76. * pendingTotpSecret?: string,
  77. * trustedVersion?: int,
  78. * }
  79. */
  80. return \json_decode($this->options, true, flags: \JSON_THROW_ON_ERROR);
  81. }
  82. /**
  83. * @param mixed[] $options
  84. *
  85. * @return static
  86. */
  87. public function setOptions(?array $options)
  88. {
  89. $this->options = $options ? \json_encode($options, \JSON_THROW_ON_ERROR) : null;
  90. return $this;
  91. }
  92. }