src/UserBundle/Entity/User.php line 56

Open in your IDE?
  1. <?php
  2. namespace UserBundle\Entity;
  3. use CoreBundle\Entity\Institution;
  4. use CoreBundle\Entity\SelfStudy;
  5. use DateTime;
  6. use FOS\UserBundle\Model\User as BaseUser;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Doctrine\ORM\Mapping\AttributeOverrides;
  9. use Doctrine\ORM\Mapping\AttributeOverride;
  10. use Scheb\TwoFactorBundle\Model\Totp\TotpConfiguration;
  11. use Scheb\TwoFactorBundle\Model\Totp\TotpConfigurationInterface;
  12. use Scheb\TwoFactorBundle\Model\Totp\TwoFactorInterface;
  13. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  14. use JsonSerializable;
  15. /**
  16. * @ORM\Table(name="user")
  17. * @ORM\Entity(repositoryClass="UserBundle\Entity\UserRepository")
  18. * @AttributeOverrides({
  19. * @AttributeOverride(name="password",
  20. * column=@ORM\Column(
  21. * name="password",
  22. * type="string",
  23. * length=255,
  24. * nullable=true
  25. * )
  26. * ),
  27. * @AttributeOverride(name="email",
  28. * column=@ORM\Column(
  29. * name="email",
  30. * type="string",
  31. * length=255,
  32. * unique=false,
  33. * nullable=true
  34. * )
  35. * ),
  36. * @AttributeOverride(name="emailCanonical",
  37. * column=@ORM\Column(
  38. * name="email_canonical",
  39. * type="string",
  40. * length=255,
  41. * unique=false,
  42. * nullable=true
  43. * )
  44. * )
  45. * })
  46. * @UniqueEntity(
  47. * fields={"username"},
  48. * message="user.username.taken"
  49. * )
  50. */
  51. class User extends BaseUser implements JsonSerializable, TwoFactorInterface
  52. {
  53. const role_admin = 'ROLE_ADMIN';
  54. const role_student = 'ROLE_STUDENT';
  55. const role_teacher = 'ROLE_TEACHER';
  56. /**
  57. * @ORM\Id
  58. * @ORM\Column(type="integer")
  59. * @ORM\GeneratedValue(strategy="AUTO")
  60. */
  61. protected $id;
  62. /**
  63. * @var string
  64. *
  65. * @ORM\Column(name="first_name", type="string", length=255)
  66. */
  67. protected $firstName;
  68. /**
  69. * @var string
  70. * @ORM\Column(name="surname", type="string", length=255)
  71. */
  72. protected $surname;
  73. /**
  74. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Institution", cascade={"persist"})
  75. * @ORM\JoinColumn(name="institution", referencedColumnName="id", onDelete="CASCADE", nullable = true)
  76. */
  77. protected ?Institution $institution = null;
  78. /**
  79. * @var string
  80. *
  81. * @ORM\Column(name="nickname", type="string", length=255, nullable = true)
  82. */
  83. protected $nickname;
  84. /**
  85. * @var string
  86. *
  87. * @ORM\Column(name="first_login", type="boolean", options={"default" = true})
  88. */
  89. protected $firstLogin = true;
  90. /**
  91. * @var bool
  92. *
  93. * @ORM\Column(name="is_subscribed", type="boolean", options={"default" = false})
  94. */
  95. protected $isSubscribed = false;
  96. /**
  97. * @var bool
  98. *
  99. * @ORM\Column(name="is_demo", type="boolean", options={"default" = false})
  100. */
  101. protected $isDemo = false;
  102. /**
  103. * @var int
  104. * @ORM\Column(name="desired_grade", type="smallint", nullable=true)
  105. */
  106. private $desiredGrade;
  107. /**
  108. * @var int
  109. * @ORM\Column(name="class_level", type="smallint", nullable=true)
  110. */
  111. private $classLevel;
  112. /**
  113. * @var int
  114. * @ORM\Column(name="self_study_level_adjustment", type="smallint", options={"default" = 0})
  115. */
  116. private $selfStudyLevelAdjustment = 0;
  117. /**
  118. * @var string
  119. * @ORM\Column(type="string", nullable=true)
  120. */
  121. private $userAgent;
  122. /**
  123. * @var bool
  124. * @ORM\Column(name="show_audio", type="boolean", options={"default" = false}, nullable=true)
  125. */
  126. private $showAudio;
  127. /**
  128. * @var bool
  129. * @ORM\Column(name="classroom_overview", type="boolean", options={"default" = true}, nullable=true)
  130. */
  131. private $classroomOverview;
  132. /**
  133. * @var \DateTime
  134. *
  135. * @ORM\Column(name="expiration_basis", type="datetime", nullable=true)
  136. */
  137. private $expirationBasis;
  138. /**
  139. * @var \DateTime
  140. *
  141. * @ORM\Column(name="expiration_self_study", type="datetime", nullable=true)
  142. */
  143. private $expirationSelfStudy;
  144. /**
  145. * @var \DateTime
  146. *
  147. * @ORM\Column(name="expiration_textbook", type="datetime", nullable=true)
  148. */
  149. private $expirationTextbook;
  150. /**
  151. * @var \DateTime
  152. *
  153. * @ORM\Column(name="last_login_with_unilogin", type="datetime", nullable=true)
  154. */
  155. private $lastLoginWithUnilogin; // note that the last_login in the database is handled by FOSUserBundle and will be removed
  156. /**
  157. * @var SelfStudy
  158. *
  159. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\SelfStudy")
  160. * @ORM\JoinColumn(name="selected_self_study", referencedColumnName="id", nullable=true)
  161. */
  162. private $selectedSelfStudy;
  163. /**
  164. * @var string
  165. *
  166. * @ORM\Column(name="ekeys_user_id", type="string", nullable=true)
  167. */
  168. private ?string $ekeysUserId;
  169. /**
  170. * @ORM\Column(name="totp_secret", type="string", nullable=true)
  171. */
  172. private ?string $totpSecret;
  173. /**
  174. * @ORM\Column(name="totp_validated", type="boolean", options={"default" = false})
  175. */
  176. private bool $totpValidated = false;
  177. /**
  178. * @ORM\Column(name="class_name", type="string", nullable=true)
  179. */
  180. private ?string $className;
  181. public function __construct()
  182. {
  183. parent::__construct();
  184. // your own logic
  185. }
  186. public function getClassName(): ?string
  187. {
  188. return $this->className;
  189. }
  190. /**
  191. * @param string|null $className
  192. * @return void
  193. */
  194. public function setClassName(?string $className): void
  195. {
  196. $this->className = $className;
  197. }
  198. /**
  199. * @param ?Institution $institution
  200. *
  201. * @return User
  202. */
  203. public function setInstitution(?Institution $institution)
  204. {
  205. $this->institution = $institution;
  206. return $this;
  207. }
  208. public function getInstitution(): ?Institution
  209. {
  210. return $this->institution;
  211. }
  212. /**
  213. * @return string|null
  214. */
  215. public function getEkeysUserId(): ?string
  216. {
  217. return $this->ekeysUserId;
  218. }
  219. /**
  220. * @param string|null $ekeysUserId
  221. * @return void
  222. */
  223. public function setEkeysUserId(?string $ekeysUserId): void
  224. {
  225. $this->ekeysUserId = $ekeysUserId;
  226. }
  227. /**
  228. * @return string
  229. */
  230. public function getClassLevelName(?bool $isSelfStudy = false) {
  231. $levelName = '';
  232. $classLevel = $isSelfStudy ? $this->getSelfStudyLevel() : $this->getClassLevel();
  233. if ($classLevel <= 3) {
  234. $levelName = 'indskoling';
  235. }
  236. elseif ($classLevel >= 4 && $classLevel <= 6) {
  237. $levelName = 'mellemtrin';
  238. }
  239. elseif ($classLevel >= 7) {
  240. $levelName = 'udskoling';
  241. }
  242. return $levelName;
  243. }
  244. /**
  245. * @return string
  246. */
  247. public function getNickname()
  248. {
  249. if (!empty($this->nickname)) {
  250. return $this->nickname;
  251. }
  252. if (!empty($this->getFirstName()) && !empty($this->getSurname())) {
  253. return $this->getFirstName() . ' ' . $this->getSurname();
  254. }
  255. $nickname = $this->getFirstName();
  256. if ($surname = $this->getSurname()) {
  257. preg_match_all('/(?<=\s|^)[a-z]/i', $surname, $matches);
  258. $nickname .= implode('', $matches[0]);
  259. }
  260. return $nickname;
  261. }
  262. /**
  263. * @param string | null $nickname
  264. *
  265. * @return $this
  266. */
  267. public function setNickname($nickname = null)
  268. {
  269. if (!empty($nickname)) {
  270. $this->nickname = $nickname;
  271. return $this;
  272. }
  273. if (!empty($this->getFirstName()) && !empty($this->getSurname())) {
  274. $this->nickname = $this->getFirstName() . ' ' . $this->getSurname();
  275. return $this;
  276. }
  277. $nickname = $this->getFirstName();
  278. if ($surname = $this->getSurname()) {
  279. preg_match_all('/(?<=\s|^)[a-z]/i', $surname, $matches);
  280. $nickname .= implode('', $matches[0]);
  281. }
  282. $this->nickname = $nickname;
  283. return $this;
  284. }
  285. /**
  286. * @return string
  287. */
  288. public function getFirstLogin()
  289. {
  290. return $this->firstLogin;
  291. }
  292. /**
  293. * @param string $firstLogin
  294. *
  295. * @return $this
  296. */
  297. public function setFirstLogin($firstLogin)
  298. {
  299. $this->firstLogin = $firstLogin;
  300. return $this;
  301. }
  302. /**
  303. * @return mixed
  304. */
  305. public function getId()
  306. {
  307. return $this->id;
  308. }
  309. /**
  310. * @param mixed $id
  311. */
  312. public function setId($id)
  313. {
  314. $this->id = $id;
  315. }
  316. /**
  317. * @return string
  318. */
  319. public function getSurname()
  320. {
  321. return $this->surname;
  322. }
  323. /**
  324. * @param string $surname
  325. *
  326. * @return $this
  327. */
  328. public function setSurname(string $surname)
  329. {
  330. $this->surname = $surname;
  331. return $this;
  332. }
  333. /**
  334. * @return mixed
  335. */
  336. public function getIsSubscribed()
  337. {
  338. return $this->isSubscribed;
  339. }
  340. /**
  341. * @param mixed $isSubscribed
  342. */
  343. public function setIsSubscribed($isSubscribed)
  344. {
  345. $this->isSubscribed = $isSubscribed;
  346. }
  347. /**
  348. * @return string
  349. */
  350. public function getFirstName()
  351. {
  352. return $this->firstName;
  353. }
  354. /**
  355. * @param string $firstName
  356. *
  357. * @return $this
  358. */
  359. public function setFirstName(string $firstName)
  360. {
  361. $this->firstName = $firstName;
  362. return $this;
  363. }
  364. /**
  365. * @return bool
  366. */
  367. public function isDemo()
  368. {
  369. return $this->isDemo;
  370. }
  371. /**
  372. * @param bool $isDemo
  373. */
  374. public function setIsDemo(bool $isDemo)
  375. {
  376. $this->isDemo = $isDemo;
  377. }
  378. public function isDebugUser()
  379. {
  380. return $this->getUsername() === "teacher1_demo";
  381. }
  382. /**
  383. * @return int | null
  384. */
  385. public function getDesiredGrade()
  386. {
  387. return $this->desiredGrade;
  388. }
  389. /**
  390. * @param int | null $desiredGrade
  391. */
  392. public function setDesiredGrade($desiredGrade)
  393. {
  394. $this->desiredGrade = $desiredGrade;
  395. }
  396. /**
  397. * @return int
  398. */
  399. public function getSelfStudyLevelAdjustment(): int
  400. {
  401. return $this->selfStudyLevelAdjustment;
  402. }
  403. /**
  404. * @param int $levelAdjustment
  405. */
  406. public function setSelfStudyLevelAdjustment(int $levelAdjustment)
  407. {
  408. $this->selfStudyLevelAdjustment = $levelAdjustment;
  409. }
  410. /**
  411. * @return int|null
  412. */
  413. public function getSelfStudyLevel(): ?int
  414. {
  415. return $this->getClassLevel() === null ? null : $this->getClassLevel() + $this->getSelfStudyLevelAdjustment();
  416. }
  417. public function getUserAgent(): ?string
  418. {
  419. return $this->userAgent;
  420. }
  421. public function setUserAgent(string $userAgent)
  422. {
  423. $this->userAgent = $userAgent;
  424. }
  425. /**
  426. * @return string
  427. */
  428. public function getFullName()
  429. {
  430. return ucwords($this->firstName) . ' ' . ucwords($this->surname);
  431. }
  432. /**
  433. * @return string
  434. */
  435. public function getCompactName()
  436. {
  437. return ucwords($this->firstName) . ' ' . strtoupper(substr($this->surname, 0, 2));
  438. }
  439. public function getClassLevel(): ?int
  440. {
  441. return $this->classLevel;
  442. }
  443. /**
  444. * @param $classLevel
  445. */
  446. public function setClassLevel($classLevel): void
  447. {
  448. $this->classLevel = (int) $classLevel;
  449. }
  450. /**
  451. * @return bool
  452. */
  453. public function isShowAudio(): ?bool
  454. {
  455. return $this->showAudio;
  456. }
  457. /**
  458. * @param bool $showAudio
  459. */
  460. public function setShowAudio(bool $showAudio): void
  461. {
  462. $this->showAudio = $showAudio;
  463. }
  464. /**
  465. * @return bool
  466. */
  467. public function getClassroomOverview(): ?bool
  468. {
  469. return $this->classroomOverview;
  470. }
  471. /**
  472. * @param bool $classroomOverview
  473. */
  474. public function setClassroomOverview(bool $classroomOverview): void
  475. {
  476. $this->classroomOverview = $classroomOverview;
  477. }
  478. /**
  479. * @return \DateTime|null
  480. */
  481. public function getExpirationBasis(): ?\DateTime
  482. {
  483. return $this->expirationBasis;
  484. }
  485. /**
  486. * @param \DateTime|null $expirationBasis
  487. */
  488. public function setExpirationBasis(?\DateTime $expirationBasis): void
  489. {
  490. $this->expirationBasis = $expirationBasis;
  491. }
  492. /**
  493. * @return \DateTime|null
  494. */
  495. public function getExpirationSelfStudy(): ?\DateTime
  496. {
  497. return $this->expirationSelfStudy;
  498. }
  499. /**
  500. * @param \DateTime|null $expirationSelfStudy
  501. */
  502. public function setExpirationSelfStudy(?\DateTime $expirationSelfStudy): void
  503. {
  504. $this->expirationSelfStudy = $expirationSelfStudy;
  505. }
  506. /**
  507. * @return \DateTime|null
  508. */
  509. public function getExpirationTextbook(): ?\DateTime
  510. {
  511. return $this->expirationTextbook;
  512. }
  513. /**
  514. * @param \DateTime|null $expirationTextbook
  515. */
  516. public function setExpirationTextbook(?\DateTime $expirationTextbook): void
  517. {
  518. $this->expirationTextbook = $expirationTextbook;
  519. }
  520. public function jsonSerialize(): mixed
  521. {
  522. return [
  523. "id" => $this->getId(),
  524. "name" => $this->getFullName(),
  525. "classLevel" => $this->getClassLevel()
  526. ];
  527. }
  528. public function getLastLoginWithUnilogin(): ?DateTime
  529. {
  530. return $this->lastLoginWithUnilogin;
  531. }
  532. public function setLastLoginWithUnilogin(DateTime $lastLoginWithUnilogin): void
  533. {
  534. $this->lastLoginWithUnilogin = $lastLoginWithUnilogin;
  535. }
  536. public function getSelectedSelfStudy(): ?SelfStudy
  537. {
  538. return $this->selectedSelfStudy;
  539. }
  540. public function setSelectedSelfStudy(SelfStudy $selectedSelfStudy): void
  541. {
  542. $this->selectedSelfStudy = $selectedSelfStudy;
  543. }
  544. public function setTotpSecret(string $secret): void
  545. {
  546. $this->totpSecret = $secret;
  547. }
  548. public function isTotpAuthenticationEnabled(): bool
  549. {
  550. return $this->totpValidated;
  551. }
  552. public function getTotpAuthenticationUsername(): string
  553. {
  554. return $this->username;
  555. }
  556. public function getTotpAuthenticationConfiguration(): ?TotpConfigurationInterface
  557. {
  558. // SHA1, 30 seconds and 6 digits is will make it work with Google authenticator and equivalent.
  559. return new TotpConfiguration($this->totpSecret, TotpConfiguration::ALGORITHM_SHA1, 30, 6);
  560. }
  561. public function isTotpValidated(): bool
  562. {
  563. return $this->totpValidated;
  564. }
  565. public function setTotpValidated(bool $totpValidated): void
  566. {
  567. $this->totpValidated = $totpValidated;
  568. }
  569. }