src/Entity/User.php line 131

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
  10. /**
  11. * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  12. * @ORM\Table(name="users")
  13. * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  14. */
  15. class User implements UserInterface
  16. {
  17. /**
  18. * @ORM\Id()
  19. * @ORM\GeneratedValue()
  20. * @ORM\Column(type="integer")
  21. */
  22. private $id;
  23. /**
  24. * @ORM\Column(type="string", length=180, unique=true)
  25. */
  26. private $email;
  27. /**
  28. * @ORM\Column(type="json")
  29. */
  30. private $roles = [];
  31. /**
  32. * @var string The hashed password
  33. * @ORM\Column(type="string")
  34. * @RollerworksPassword\PasswordStrength(minLength=8, minStrength=3)
  35. */
  36. private $password;
  37. /**
  38. * @ORM\ManyToMany(targetEntity="App\Entity\Newsletter", inversedBy="subscribers")
  39. */
  40. private $subscriptions;
  41. /**
  42. * @ORM\OneToMany(targetEntity="App\Entity\ApplicationRole", mappedBy="user")
  43. */
  44. private $applicationRoles;
  45. /**
  46. * @ORM\Column(type="boolean")
  47. */
  48. private $verified;
  49. /**
  50. * @ORM\Column(type="string", length=255, nullable=true)
  51. */
  52. private $verificationToken;
  53. /**
  54. * @ORM\Column(type="string", length=255, nullable=true)
  55. */
  56. private $title;
  57. /**
  58. * @ORM\Column(type="string", length=255)
  59. */
  60. private $forename;
  61. /**
  62. * @ORM\Column(type="string", length=255)
  63. */
  64. private $surname;
  65. /**
  66. * @ORM\OneToMany(targetEntity="App\Entity\Audit", mappedBy="user")
  67. */
  68. private $audits;
  69. /**
  70. * @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="user")
  71. */
  72. private $comments;
  73. /**
  74. * @ORM\OneToMany(targetEntity="App\Entity\Student", mappedBy="user")
  75. */
  76. private $studentships;
  77. /**
  78. * @ORM\OneToMany(targetEntity=ApplicationReportRole::class, mappedBy="createdBy")
  79. */
  80. private $applicationReportRoles;
  81. public const TITLES = [
  82. 'dr' => 'Dr',
  83. 'prof' => 'Professor',
  84. 'mr' => 'Mr',
  85. 'ms' => 'Ms',
  86. 'mrs' => 'Mrs',
  87. 'miss' => 'Miss',
  88. ];
  89. public function __construct()
  90. {
  91. $this->subscriptions = new ArrayCollection();
  92. $this->applicationRoles = new ArrayCollection();
  93. $this->setRoles(['ROLE_USER']);
  94. $this->setVerified(false);
  95. $this->setVerificationToken(uniqid());
  96. $this->audits = new ArrayCollection();
  97. $this->comments = new ArrayCollection();
  98. $this->studentships = new ArrayCollection();
  99. $this->applicationReportRoles = new ArrayCollection();
  100. }
  101. public function getId(): ?int
  102. {
  103. return $this->id;
  104. }
  105. public function getEmail(): ?string
  106. {
  107. return strpos($this->email, '@') ? $this->email : null;
  108. }
  109. public function setEmail(?string $email): self
  110. {
  111. $this->email = $email;
  112. return $this;
  113. }
  114. public function getUsername(): string
  115. {
  116. return (string)$this->email;
  117. }
  118. /**
  119. * A visual identifier that represents this user.
  120. *
  121. * @see UserInterface
  122. */
  123. public function getUserIdentifier(): string
  124. {
  125. return (string) $this->email;
  126. }
  127. public function getRoles(): array
  128. {
  129. $roles = $this->roles;
  130. // guarantee every user at least has ROLE_USER
  131. $roles[] = 'ROLE_USER';
  132. return array_unique($roles);
  133. }
  134. public function setRoles(array $roles): self
  135. {
  136. $this->roles = $roles;
  137. return $this;
  138. }
  139. public function removeRoles(array $roles)
  140. {
  141. $currentRoles = $this->getRoles();
  142. $this->roles = array_diff($currentRoles, $roles);
  143. return $this;
  144. }
  145. public function getPassword(): string
  146. {
  147. return (string)$this->password;
  148. }
  149. public function setPassword(string $password): self
  150. {
  151. $this->password = $password;
  152. return $this;
  153. }
  154. public function getSalt()
  155. {
  156. // not needed when using the "bcrypt" algorithm in security.yaml
  157. }
  158. public function eraseCredentials()
  159. {
  160. // If you store any temporary, sensitive data on the user, clear it here
  161. // $this->plainPassword = null;
  162. }
  163. public function isUserAdmin()
  164. {
  165. foreach($this->roles as $role) {
  166. if ($role == "ROLE_ADMIN") {
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. public function getFullName(): ?string
  173. {
  174. $title = $this->getTitle() ? self::TITLES[$this->getTitle()] : '';
  175. return implode(' ', array_filter([
  176. $title,
  177. $this->getForename(),
  178. $this->getSurname(),
  179. ]));
  180. }
  181. /**
  182. * @return Collection|Newsletter[]
  183. */
  184. public function getSubscriptions(): Collection
  185. {
  186. return $this->subscriptions;
  187. }
  188. public function addSubscription(Newsletter $subscription): self
  189. {
  190. if (!$this->subscriptions->contains($subscription)) {
  191. $this->subscriptions[] = $subscription;
  192. }
  193. return $this;
  194. }
  195. public function removeSubscription(Newsletter $subscription): self
  196. {
  197. if ($this->subscriptions->contains($subscription)) {
  198. $this->subscriptions->removeElement($subscription);
  199. }
  200. return $this;
  201. }
  202. /**
  203. * @return Collection|ApplicationRole[]
  204. */
  205. public function getApplicationRoles(): Collection
  206. {
  207. return $this->applicationRoles;
  208. }
  209. /**
  210. * Get all application roles where the role type acronym is not 'RL' or 'RM'
  211. *
  212. * @return Collection|ApplicationRole[]
  213. */
  214. public function getFilteredApplicationRoles(): Collection
  215. {
  216. return $this->applicationRoles->filter(function ($role) {
  217. $acronym = $role->getApplicationRoleType()->getAcronym();
  218. return $acronym !== 'RL' && $acronym !== 'RM' && $acronym !== '3R';
  219. });
  220. }
  221. public function addApplicationRole(ApplicationRole $applicationRole): self
  222. {
  223. if (!$this->applicationRoles->contains($applicationRole)) {
  224. $this->applicationRoles[] = $applicationRole;
  225. $applicationRole->setUser($this);
  226. }
  227. return $this;
  228. }
  229. public function removeApplicationRole(ApplicationRole $applicationRole): self
  230. {
  231. if ($this->applicationRoles->contains($applicationRole)) {
  232. $this->applicationRoles->removeElement($applicationRole);
  233. // set the owning side to null (unless already changed)
  234. if ($applicationRole->getUser() === $this) {
  235. $applicationRole->setUsery(null);
  236. }
  237. }
  238. return $this;
  239. }
  240. public function isVerified(): ?bool
  241. {
  242. return $this->verified;
  243. }
  244. public function setVerified(bool $verified): self
  245. {
  246. $this->verified = $verified;
  247. return $this;
  248. }
  249. public function getVerificationToken(): ?string
  250. {
  251. return $this->verificationToken;
  252. }
  253. public function setVerificationToken(?string $verificationToken): self
  254. {
  255. $this->verificationToken = $verificationToken;
  256. return $this;
  257. }
  258. public function getApplications(): Collection
  259. {
  260. $applications = new ArrayCollection();
  261. foreach ($this->applicationRoles as $role) {
  262. $applications->add($role->getApplication());
  263. }
  264. return $applications;
  265. }
  266. public function getTitle(): ?string
  267. {
  268. return $this->title;
  269. }
  270. public function setTitle(?string $title): self
  271. {
  272. $this->title = $title;
  273. return $this;
  274. }
  275. public function getForename(): ?string
  276. {
  277. return $this->forename;
  278. }
  279. public function getForenameById($id): ?string
  280. {
  281. return $this->getDoctrine()->getRepository(User::class)->find($id);
  282. }
  283. public function setForename(string $forename): self
  284. {
  285. $this->forename = $forename;
  286. return $this;
  287. }
  288. public function getSurname(): ?string
  289. {
  290. return $this->surname;
  291. }
  292. public function setSurname(string $surname): self
  293. {
  294. $this->surname = $surname;
  295. return $this;
  296. }
  297. public function getVerified(): ?bool
  298. {
  299. return $this->verified;
  300. }
  301. /**
  302. * @return Collection|Audit[]
  303. */
  304. public function getAudits(): Collection
  305. {
  306. return $this->audits;
  307. }
  308. public function addAudit(Audit $audit): self
  309. {
  310. if (!$this->audits->contains($audit)) {
  311. $this->audits[] = $audit;
  312. $audit->setUser($this);
  313. }
  314. return $this;
  315. }
  316. public function removeAudit(Audit $audit): self
  317. {
  318. if ($this->audits->contains($audit)) {
  319. $this->audits->removeElement($audit);
  320. // set the owning side to null (unless already changed)
  321. if ($audit->getUser() === $this) {
  322. $audit->setUser(null);
  323. }
  324. }
  325. return $this;
  326. }
  327. /**
  328. * @return Collection|Comment[]
  329. */
  330. public function getComments(): Collection
  331. {
  332. return $this->comments;
  333. }
  334. public function addComment(Comment $comment): self
  335. {
  336. if (!$this->comments->contains($comment)) {
  337. $this->comments[] = $comment;
  338. $comment->setUser($this);
  339. }
  340. return $this;
  341. }
  342. public function removeComment(Comment $comment): self
  343. {
  344. if ($this->comments->contains($comment)) {
  345. $this->comments->removeElement($comment);
  346. // set the owning side to null (unless already changed)
  347. if ($comment->getUser() === $this) {
  348. $comment->setUser(null);
  349. }
  350. }
  351. return $this;
  352. }
  353. /**
  354. * @return Collection|Student[]
  355. */
  356. public function getStudentships(): Collection
  357. {
  358. return $this->studentships;
  359. }
  360. public function addStudentship(Student $studentship): self
  361. {
  362. if (!$this->studentships->contains($studentship)) {
  363. $this->studentships[] = $studentship;
  364. $studentship->setUser($this);
  365. }
  366. return $this;
  367. }
  368. public function removeStudentship(Student $studentship): self
  369. {
  370. if ($this->studentships->contains($studentship)) {
  371. $this->studentships->removeElement($studentship);
  372. // set the owning side to null (unless already changed)
  373. if ($studentship->getUser() === $this) {
  374. $studentship->setUser(null);
  375. }
  376. }
  377. return $this;
  378. }
  379. /**
  380. * @return Collection<int, ApplicationReportRole>
  381. */
  382. public function getApplicationReportRoles(): Collection
  383. {
  384. return $this->applicationReportRoles;
  385. }
  386. public function addApplicationReportRole(ApplicationReportRole $applicationReportRole): self
  387. {
  388. if (!$this->applicationReportRoles->contains($applicationReportRole)) {
  389. $this->applicationReportRoles[] = $applicationReportRole;
  390. $applicationReportRole->setCreatedBy($this);
  391. }
  392. return $this;
  393. }
  394. public function removeApplicationReportRole(ApplicationReportRole $applicationReportRole): self
  395. {
  396. if ($this->applicationReportRoles->removeElement($applicationReportRole)) {
  397. // set the owning side to null (unless already changed)
  398. if ($applicationReportRole->getCreatedBy() === $this) {
  399. $applicationReportRole->setCreatedBy(null);
  400. }
  401. }
  402. return $this;
  403. }
  404. }