src/Entity/User/Invitation.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity\User;
  3. use App\Entity\Core\Institution;
  4. use DateTime;
  5. use DateTimeZone;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Exception;
  8. #[ORM\Table('invitation')]
  9. #[ORM\Entity]
  10. class Invitation
  11. {
  12. const EXPIRES_AFTER = '24 hours';
  13. #[ORM\Column(name: 'id', type: 'integer')]
  14. #[ORM\Id]
  15. #[ORM\GeneratedValue(strategy: 'AUTO')]
  16. private int $id;
  17. #[ORM\Column(name: 'token', type: 'string', length: 64)]
  18. private string $token;
  19. #[ORM\Column(name: 'email', type: 'string', length: 255)]
  20. private string $email;
  21. #[ORM\JoinColumn(name: 'issued_by', referencedColumnName: 'id', onDelete: 'SET NULL', nullable: true)]
  22. #[ORM\ManyToOne(targetEntity: User::class)]
  23. private ?User $issuedBy;
  24. #[ORM\Column(name: 'created', type: 'datetime')]
  25. private DateTime $created;
  26. #[ORM\Column(name: 'active', type: 'boolean')]
  27. private bool $active;
  28. #[ORM\Column(name: 'role', type: 'string', length: 32)]
  29. private string $role;
  30. #[ORM\JoinColumn(name: 'institution', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: false)]
  31. #[ORM\ManyToOne(targetEntity: Institution::class)]
  32. private Institution $institution;
  33. public function getId(): ?int
  34. {
  35. return $this->id ?? null; // To avoid property access error in EasyAdmin
  36. }
  37. public function setId(int $id): void
  38. {
  39. $this->id = $id;
  40. }
  41. public function getToken(): string
  42. {
  43. return $this->token;
  44. }
  45. public function setToken(string $token): void
  46. {
  47. $this->token = $token;
  48. }
  49. public function getEmail(): string
  50. {
  51. return $this->email;
  52. }
  53. public function setEmail(string $email): void
  54. {
  55. $this->email = $email;
  56. }
  57. public function getIssuedBy(): ?User
  58. {
  59. return $this->issuedBy;
  60. }
  61. public function setIssuedBy(?User $issuedBy): void
  62. {
  63. $this->issuedBy = $issuedBy;
  64. }
  65. public function getCreated(): DateTime
  66. {
  67. return $this->created;
  68. }
  69. public function setCreated(DateTime $created): void
  70. {
  71. $this->created = $created;
  72. }
  73. public function isActive(): bool
  74. {
  75. return $this->active;
  76. }
  77. public function setActive(bool $active): void
  78. {
  79. $this->active = $active;
  80. }
  81. public function getRole(): string
  82. {
  83. return $this->role;
  84. }
  85. public function setRole(string $role): void
  86. {
  87. $this->role = $role;
  88. }
  89. public function getInstitution(): Institution
  90. {
  91. return $this->institution;
  92. }
  93. public function setInstitution(Institution $institution): void
  94. {
  95. $this->institution = $institution;
  96. }
  97. /**
  98. * @throws Exception
  99. */
  100. public function isValid(): bool
  101. {
  102. if (!$this->isActive()) {
  103. return false;
  104. }
  105. $latestCreationTime = new DateTime('-' . self::EXPIRES_AFTER, new DateTimeZone('Europe/Copenhagen'));
  106. if ($this->getCreated() < $latestCreationTime) {
  107. return false;
  108. }
  109. if ($this->getIssuedBy() === null) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. /**
  115. * @throws Exception
  116. */
  117. public function generateToken(): void
  118. {
  119. $this->token = bin2hex(random_bytes(20));
  120. }
  121. }