Invitation.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2021 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login\Invitations;
  9. /**
  10. * Invite users to the site.
  11. *
  12. * Tools to send emails for invites and handle invite registrations.
  13. */
  14. class Invitation
  15. {
  16. /** @var string */
  17. public $token;
  18. /** @var string */
  19. public $email;
  20. /** @var string */
  21. public $created_by;
  22. /** @var int */
  23. public $created_timestamp = 0;
  24. /** @var int */
  25. public $expiration_timestamp = 0;
  26. /** @var array */
  27. public $account = ['access' => ['site' => ['login' => true]]];
  28. public function __construct(string $token, array $data)
  29. {
  30. $this->token = $token;
  31. $this->email = $data['email'];
  32. $this->created_by = $data['created_by'];
  33. $this->created_timestamp = $data['created_timestamp'] ?? time();
  34. $this->expiration_timestamp = $data['expiration_timestamp'] ?? time() + 86400; // 1 day
  35. if (isset($data['account'])) {
  36. $this->account = $data['account'];
  37. }
  38. }
  39. public function isExpired(): bool
  40. {
  41. return ($this->expiration_timestamp ?? 0) < time();
  42. }
  43. public function toArray(): array
  44. {
  45. return [
  46. 'email' => $this->email,
  47. 'created_by' => $this->created_by,
  48. 'created_timestamp' => $this->created_timestamp,
  49. 'expiration_timestamp' => $this->expiration_timestamp,
  50. 'account' => $this->account,
  51. ];
  52. }
  53. }