src/Entity/User.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Tag\HasAuthToken;
  4. use App\Entity\Tag\IsBlameable;
  5. use App\Entity\Traits\AuthTokenTrait;
  6. use App\Entity\Traits\UserFields;
  7. use App\Repository\UserRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Gedmo\Blameable\Traits\BlameableEntity;
  12. use Gedmo\Timestampable\Traits\TimestampableEntity;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. #[ORM\Table]
  16. #[ORM\Index(name: 'auth_token', columns: ['auth_token'])]
  17. #[ORM\Index(name: 'username', columns: ['username'])]
  18. #[ORM\Index(name: 'created_by', columns: ['created_by'])]
  19. #[ORM\Index(name: 'updated_by', columns: ['updated_by'])]
  20. #[ORM\Entity(repositoryClass: UserRepository::class)]
  21. #[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
  22. class User implements UserInterface, HasAuthToken, IsBlameable
  23. {
  24. use AuthTokenTrait;
  25. use UserFields;
  26. use BlameableEntity;
  27. use TimestampableEntity;
  28. #[ORM\Id]
  29. #[ORM\GeneratedValue]
  30. #[ORM\Column(type: 'integer')]
  31. private $id;
  32. #[ORM\Column(type: 'string', length: 180, unique: true)]
  33. private $username;
  34. #[ORM\Column(type: 'json')]
  35. private $roles = [];
  36. /**
  37. * @var string The hashed password
  38. */
  39. #[ORM\Column(type: 'string')]
  40. private $password;
  41. #[ORM\Column(type: 'string', length: 255)]
  42. private $email;
  43. #[ORM\Column(type: 'boolean')]
  44. private $isVerified = false;
  45. #[ORM\ManyToMany(targetEntity: Company::class, mappedBy: 'members')]
  46. private $companies;
  47. #[ORM\OneToMany(targetEntity: AnalysisServiceWeight::class, mappedBy: 'user', orphanRemoval: true)]
  48. private $analysisServiceWeights;
  49. public function __construct()
  50. {
  51. $this->companies = new ArrayCollection();
  52. $this->analysisServiceWeights = new ArrayCollection();
  53. }
  54. public function getId(): ?int
  55. {
  56. return $this->id;
  57. }
  58. /**
  59. * A visual identifier that represents this user.
  60. *
  61. * @see UserInterface
  62. */
  63. public function getUsername(): string
  64. {
  65. return (string) $this->username;
  66. }
  67. public function setUsername(string $username): self
  68. {
  69. $this->username = $username;
  70. return $this;
  71. }
  72. /**
  73. * @see UserInterface
  74. */
  75. public function getRoles(): array
  76. {
  77. $roles = $this->roles;
  78. // guarantee every user at least has ROLE_USER
  79. $roles[] = 'ROLE_USER';
  80. return array_unique($roles);
  81. }
  82. public function setRoles(array $roles): self
  83. {
  84. $this->roles = $roles;
  85. return $this;
  86. }
  87. /**
  88. * @see UserInterface
  89. */
  90. public function getPassword(): string
  91. {
  92. return (string) $this->password;
  93. }
  94. public function setPassword(string $password): self
  95. {
  96. $this->password = $password;
  97. return $this;
  98. }
  99. /**
  100. * @see UserInterface
  101. */
  102. public function getSalt()
  103. {
  104. // not needed when using the "bcrypt" algorithm in security.yaml
  105. }
  106. /**
  107. * @see UserInterface
  108. */
  109. public function eraseCredentials()
  110. {
  111. // If you store any temporary, sensitive data on the user, clear it here
  112. // $this->plainPassword = null;
  113. }
  114. public function getEmail(): ?string
  115. {
  116. return $this->email;
  117. }
  118. public function setEmail(string $email): self
  119. {
  120. $this->email = $email;
  121. return $this;
  122. }
  123. public function isVerified(): bool
  124. {
  125. return $this->isVerified;
  126. }
  127. public function setIsVerified(bool $isVerified): self
  128. {
  129. $this->isVerified = $isVerified;
  130. return $this;
  131. }
  132. /**
  133. * @return Collection|Company[]
  134. */
  135. public function getCompanies(): Collection
  136. {
  137. return $this->companies;
  138. }
  139. public function addCompany(Company $company): self
  140. {
  141. if (!$this->companies->contains($company)) {
  142. $this->companies[] = $company;
  143. $company->addMember($this);
  144. }
  145. return $this;
  146. }
  147. public function removeCompany(Company $company): self
  148. {
  149. if ($this->companies->contains($company)) {
  150. $this->companies->removeElement($company);
  151. $company->removeMember($this);
  152. }
  153. return $this;
  154. }
  155. public function getIsVerified(): ?bool
  156. {
  157. return $this->isVerified;
  158. }
  159. /**
  160. * @return Collection|AnalysisServiceWeight[]
  161. */
  162. public function getAnalysisServiceWeights(): Collection
  163. {
  164. return $this->analysisServiceWeights;
  165. }
  166. public function addAnalysisServiceWeight(AnalysisServiceWeight $analysisServiceWeight): self
  167. {
  168. if (!$this->analysisServiceWeights->contains($analysisServiceWeight)) {
  169. $this->analysisServiceWeights[] = $analysisServiceWeight;
  170. $analysisServiceWeight->setUser($this);
  171. }
  172. return $this;
  173. }
  174. public function removeAnalysisServiceWeight(AnalysisServiceWeight $analysisServiceWeight): self
  175. {
  176. if ($this->analysisServiceWeights->contains($analysisServiceWeight)) {
  177. $this->analysisServiceWeights->removeElement($analysisServiceWeight);
  178. // set the owning side to null (unless already changed)
  179. if ($analysisServiceWeight->getUser() === $this) {
  180. $analysisServiceWeight->setUser(null);
  181. }
  182. }
  183. return $this;
  184. }
  185. }