User.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Entity;
  12. use FOS\UserBundle\Entity\User as BaseUser;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. /**
  16. * @ORM\Entity(repositoryClass="Packagist\WebBundle\Entity\UserRepository")
  17. * @ORM\Table(name="fos_user")
  18. */
  19. class User extends BaseUser
  20. {
  21. /**
  22. * @ORM\Id
  23. * @ORM\Column(type="integer")
  24. * @ORM\generatedValue(strategy="AUTO")
  25. */
  26. protected $id;
  27. /**
  28. * @ORM\ManyToMany(targetEntity="Package", mappedBy="maintainers")
  29. */
  30. private $packages;
  31. /**
  32. * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Author", mappedBy="owner")
  33. */
  34. private $authors;
  35. /**
  36. * @ORM\Column(type="datetime")
  37. */
  38. private $createdAt;
  39. /**
  40. * @ORM\Column(type="string")
  41. * @var string
  42. */
  43. private $apiToken;
  44. public function __construct()
  45. {
  46. $this->packages = new ArrayCollection();
  47. $this->authors = new ArrayCollection();
  48. $this->createdAt = new \DateTime();
  49. $this->apiToken = $this->generateApiToken();
  50. parent::__construct();
  51. }
  52. public function toArray()
  53. {
  54. return array(
  55. 'name' => $this->getUsername(),
  56. 'email' => $this->getEmail(),
  57. );
  58. }
  59. /**
  60. * Add packages
  61. *
  62. * @param Packagist\WebBundle\Entity\Package $packages
  63. */
  64. public function addPackages(Package $packages)
  65. {
  66. $this->packages[] = $packages;
  67. }
  68. /**
  69. * Get packages
  70. *
  71. * @return Doctrine\Common\Collections\Collection $packages
  72. */
  73. public function getPackages()
  74. {
  75. return $this->packages;
  76. }
  77. /**
  78. * Add authors
  79. *
  80. * @param Packagist\WebBundle\Entity\Author $authors
  81. */
  82. public function addAuthors(\Packagist\WebBundle\Entity\Author $authors)
  83. {
  84. $this->authors[] = $authors;
  85. }
  86. /**
  87. * Get authors
  88. *
  89. * @return Doctrine\Common\Collections\Collection
  90. */
  91. public function getAuthors()
  92. {
  93. return $this->authors;
  94. }
  95. /**
  96. * Set createdAt
  97. *
  98. * @param datetime $createdAt
  99. */
  100. public function setCreatedAt($createdAt)
  101. {
  102. $this->createdAt = $createdAt;
  103. }
  104. /**
  105. * Get createdAt
  106. *
  107. * @return datetime
  108. */
  109. public function getCreatedAt()
  110. {
  111. return $this->createdAt;
  112. }
  113. /**
  114. * Set apiToken
  115. *
  116. * @param string $apiToken
  117. */
  118. public function setApiToken($apiToken)
  119. {
  120. $this->apiToken = $apiToken;
  121. }
  122. /**
  123. * Get apiToken
  124. *
  125. * @return string
  126. */
  127. public function getApiToken()
  128. {
  129. return $this->apiToken;
  130. }
  131. /**
  132. * Regenerate the apiToken
  133. */
  134. public function regenerateApiToken()
  135. {
  136. $this->apiToken = $this->generateApiToken();
  137. }
  138. /**
  139. * Generate an apiToken
  140. *
  141. * @return string
  142. */
  143. protected function generateApiToken()
  144. {
  145. return base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  146. }
  147. }