BaseUser.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\UserBundle\Document;
  11. use FOS\UserBundle\Document\User as AbstractedUser;
  12. use Sonata\UserBundle\Model\UserInterface;
  13. /**
  14. * Represents a Base User Document
  15. */
  16. class BaseUser extends AbstractedUser implements UserInterface
  17. {
  18. /**
  19. * @var \DateTime
  20. */
  21. protected $createdAt;
  22. /**
  23. * @var \DateTime
  24. */
  25. protected $updatedAt;
  26. /**
  27. * @var string
  28. */
  29. protected $twoStepVerificationCode;
  30. /**
  31. * Sets the creation date
  32. *
  33. * @param \DateTime|null $createdAt
  34. */
  35. public function setCreatedAt(\DateTime $createdAt = null)
  36. {
  37. $this->createdAt = $createdAt;
  38. }
  39. /**
  40. * Returns the creation date
  41. *
  42. * @return \DateTime|null
  43. */
  44. public function getCreatedAt()
  45. {
  46. return $this->createdAt;
  47. }
  48. /**
  49. * Sets the last update date
  50. *
  51. * @param \DateTime|null $updatedAt
  52. */
  53. public function setUpdatedAt(\DateTime $updatedAt = null)
  54. {
  55. $this->updatedAt = $updatedAt;
  56. }
  57. /**
  58. * Returns the last update date
  59. *
  60. * @return \DateTime|null
  61. */
  62. public function getUpdatedAt()
  63. {
  64. return $this->updatedAt;
  65. }
  66. /**
  67. * Hook on pre-persist operations
  68. */
  69. public function prePersist()
  70. {
  71. $this->createdAt = new \DateTime;
  72. $this->updatedAt = new \DateTime;
  73. }
  74. /**
  75. * Hook on pre-update operations
  76. */
  77. public function preUpdate()
  78. {
  79. $this->updatedAt = new \DateTime;
  80. }
  81. /**
  82. * Returns a string representation
  83. *
  84. * @return string
  85. */
  86. public function __toString()
  87. {
  88. return $this->getUsername() ?: '-';
  89. }
  90. /**
  91. * Sets the user groups
  92. *
  93. * @param array $groups
  94. */
  95. public function setGroups($groups)
  96. {
  97. foreach ($groups as $group){
  98. $this->addGroup($group);
  99. }
  100. }
  101. /**
  102. * Sets the two-step verification code
  103. *
  104. * @param string $twoStepVerificationCode
  105. */
  106. public function setTwoStepVerificationCode($twoStepVerificationCode)
  107. {
  108. $this->twoStepVerificationCode = $twoStepVerificationCode;
  109. }
  110. /**
  111. * Returns the two-step verification code
  112. *
  113. * @return string
  114. */
  115. public function getTwoStepVerificationCode()
  116. {
  117. return $this->twoStepVerificationCode;
  118. }
  119. }