src/Entity/User.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Building\Building;
  4. use App\Entity\Building\BuildingListField;
  5. use App\Entity\Client\Client;
  6. use App\Entity\Client\ClientGroup;
  7. use App\Entity\Common\UrlDomain;
  8. use App\Entity\Extension\DisabledTrait;
  9. use App\Entity\Extension\TimestampableTrait;
  10. use App\Entity\Gauge\Gauge;
  11. use App\Entity\Gauge\GaugeListField;
  12. use App\Repository\UserRepository;
  13. use App\Utils\Units;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. use Symfony\Component\Serializer\Annotation\Groups;
  22. /**
  23. * @ORM\Entity(repositoryClass=UserRepository::class)
  24. * @ORM\Table(name="users")
  25. *
  26. * @UniqueEntity("username")
  27. * @UniqueEntity("email")
  28. */
  29. class User implements UserInterface, PasswordAuthenticatedUserInterface
  30. {
  31. use TimestampableTrait;
  32. use DisabledTrait;
  33. /**
  34. * @var null|int
  35. * @ORM\Id()
  36. * @ORM\GeneratedValue()
  37. * @ORM\Column(type="integer")
  38. * @Groups({"list"})
  39. */
  40. private ?int $id = null;
  41. /**
  42. * @var string
  43. * @ORM\Column(type="string", length=180, unique=true)
  44. *
  45. * @Assert\NotBlank
  46. * @Assert\Type(type="string")
  47. * @Assert\Email
  48. * @Groups({"profile", "list"})
  49. */
  50. private string $email;
  51. /**
  52. * @var string
  53. * @ORM\Column(type="string", unique=true)
  54. *
  55. * @Assert\NotBlank
  56. * @Assert\Type(type="string")
  57. */
  58. private string $username;
  59. /**
  60. * @var string The hashed password
  61. * @ORM\Column(type="string")
  62. *
  63. * @Assert\NotBlank
  64. * @Assert\Type(type="string")
  65. */
  66. private string $password;
  67. /**
  68. * @var bool
  69. * @ORM\Column(type="boolean")
  70. *
  71. * @Assert\NotNull
  72. * @Assert\Type(type="boolean")
  73. * @Groups({"profile"})
  74. */
  75. private bool $hasDarkMode;
  76. /**
  77. * @var Role
  78. * @ORM\ManyToOne(targetEntity="App\Entity\Role", inversedBy="users")
  79. * @ORM\JoinColumn(name="role_id", referencedColumnName="id", nullable=false)
  80. * @Assert\NotBlank
  81. */
  82. private Role $role;
  83. /**
  84. * @var string|null
  85. * @ORM\Column(type="string", nullable=true)
  86. * @Groups({"list"})
  87. */
  88. private ?string $firstname;
  89. /**
  90. * @var string|null
  91. * @ORM\Column(type="string", nullable=true)
  92. * @Groups({"list"})
  93. */
  94. private ?string $lastname;
  95. /**
  96. * @var string|null
  97. * @ORM\Column(type="string", nullable=true)
  98. * @Groups({"list"})
  99. * @Assert\Length(
  100. * min = 9,
  101. * max = 13,
  102. * minMessage = "Telefon musí mít alespoň {{ limit }} znaků (bez mezer).",
  103. * maxMessage = "Telefon musí může mít maximálně {{ limit }} znaků."
  104. * )
  105. */
  106. private ?string $phone;
  107. /**
  108. * @var Client
  109. * @ORM\ManyToOne(targetEntity="App\Entity\Client\Client", inversedBy="users")
  110. * @ORM\JoinColumn(name="client_id", nullable=false)
  111. * @Groups({"list"})
  112. */
  113. private Client $client;
  114. /**
  115. * @var ClientGroup|null
  116. * @ORM\ManyToOne(targetEntity="App\Entity\Client\ClientGroup", inversedBy="users")
  117. */
  118. private ?ClientGroup $clientGroup;
  119. /**
  120. * @var boolean
  121. * @ORM\Column(type="boolean")
  122. * @Groups({"list"})
  123. */
  124. private bool $enabled;
  125. /**
  126. * @var string|null
  127. * @ORM\Column(type="string", nullable=true)
  128. */
  129. private ?string $entryPage = null;
  130. /**
  131. * @var Gauge|null
  132. * @ORM\ManyToOne(targetEntity="App\Entity\Gauge\Gauge")
  133. * @ORM\JoinColumn(name="entry_page_gauge_id", referencedColumnName="id", nullable=true)
  134. */
  135. private ?Gauge $entryPageGauge = null;
  136. /**
  137. * @var int
  138. * @ORM\Column(type="integer")
  139. */
  140. private int $primaryUnit = Units::KWH_VALUE; // default unit
  141. /**
  142. * @var Collection|Building[]
  143. * @ORM\ManyToMany(targetEntity="App\Entity\Building\Building", inversedBy="users", cascade={"persist"})
  144. * @ORM\OrderBy({"name" = "ASC"})
  145. * @ORM\JoinTable(name="users_buildings")
  146. */
  147. private $buildings;
  148. /**
  149. * @var Collection|BuildingListField[]
  150. * @ORM\OneToMany(targetEntity="App\Entity\Building\BuildingListField", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  151. * @ORM\OrderBy({"position" = "ASC"})
  152. * @Groups("user")
  153. */
  154. private $buildingListFields;
  155. /**
  156. * @var Collection|GaugeListField[]
  157. * @ORM\OneToMany(targetEntity="App\Entity\Gauge\GaugeListField", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
  158. * @ORM\OrderBy({"position" = "ASC"})
  159. * @Groups("user")
  160. */
  161. private $gaugeListFields;
  162. /**
  163. * @var Collection|ActivityLog[]
  164. * @ORM\OneToMany(targetEntity="App\Entity\ActivityLog", mappedBy="user")
  165. */
  166. private $activities;
  167. /**
  168. * just for creating form (not in dtb)
  169. * @var boolean
  170. */
  171. private bool $sendInitMail = false;
  172. /**
  173. * @var boolean
  174. * @ORM\Column(type="boolean")
  175. */
  176. private bool $emailReporting;
  177. /**
  178. * @var boolean
  179. * @ORM\Column(type="boolean")
  180. */
  181. private bool $smsReporting;
  182. /**
  183. * @var boolean
  184. * @ORM\Column(type="boolean", options={"default": 0})
  185. */
  186. private bool $incidentEmailReporting;
  187. /**
  188. * @var boolean
  189. * @ORM\Column(type="boolean", options={"default" : 1})
  190. */
  191. private bool $emailReminder;
  192. /**
  193. * @var Client|null
  194. * @ORM\OneToOne(targetEntity="App\Entity\Client\Client", mappedBy="mainManager")
  195. */
  196. private ?Client $mainManagerOf;
  197. /**
  198. * @var Collection|UserWidget[]
  199. * @ORM\OneToMany(targetEntity="App\Entity\UserWidget", mappedBy="user")
  200. */
  201. private $widgets;
  202. /**
  203. * @var \DateTime|null
  204. *
  205. * @ORM\Column(name="agreement_at", type="datetime", nullable=true)
  206. */
  207. private ?\DateTime $agreementAt;
  208. /**
  209. * @var Collection|UrlDomain[]
  210. * @ORM\ManyToMany(targetEntity="App\Entity\Common\UrlDomain", inversedBy="users", cascade={"persist"})
  211. * @ORM\OrderBy({"name" = "ASC"})
  212. */
  213. private Collection $allowedDomains;
  214. /**
  215. * @var \DateTimeInterface|null
  216. *
  217. * @ORM\Column(name="last_activity_at", type="datetime", nullable=true)
  218. */
  219. protected ?\DateTimeInterface $lastActivity;
  220. public function __construct()
  221. {
  222. $this->email = '';
  223. $this->username = '';
  224. $this->password = '';
  225. $this->enabled = false;
  226. $this->emailReporting = true;
  227. $this->smsReporting = false;
  228. $this->incidentEmailReporting = false;
  229. $this->hasDarkMode = false;
  230. $this->emailReminder = true;
  231. $this->buildings = new ArrayCollection();
  232. $this->buildingListFields = new ArrayCollection();
  233. $this->activities = new ArrayCollection();
  234. $this->widgets = new ArrayCollection();
  235. $this->allowedDomains = new ArrayCollection();
  236. }
  237. /**
  238. * @return int|null
  239. */
  240. public function getId(): ?int
  241. {
  242. return $this->id;
  243. }
  244. /**
  245. * A visual identifier that represents this user.
  246. *
  247. * @see UserInterface
  248. */
  249. public function getUsername(): string
  250. {
  251. return $this->username ?? '';
  252. }
  253. /**
  254. * @param string $username
  255. * @return $this
  256. */
  257. public function setUsername(string $username): self
  258. {
  259. $this->username = $username;
  260. return $this;
  261. }
  262. /**
  263. * @see UserInterface
  264. */
  265. public function getPassword(): string
  266. {
  267. return $this->password;
  268. }
  269. /**
  270. * @param string $password
  271. * @return $this
  272. */
  273. public function setPassword(string $password): self
  274. {
  275. $this->password = $password;
  276. return $this;
  277. }
  278. /**
  279. * @see UserInterface
  280. */
  281. public function getSalt(): ?string
  282. {
  283. return null;
  284. // not needed when using the "bcrypt" algorithm in security.yaml
  285. }
  286. /**
  287. * @see UserInterface
  288. */
  289. public function eraseCredentials(): void
  290. {
  291. // If you store any temporary, sensitive data on the user, clear it here
  292. // $this->plainPassword = null;
  293. }
  294. /**
  295. * @return string
  296. */
  297. public function getEmail(): string
  298. {
  299. return $this->email;
  300. }
  301. /**
  302. * @param string $email
  303. * @return User
  304. */
  305. public function setEmail(string $email): User
  306. {
  307. $this->email = $email;
  308. return $this;
  309. }
  310. /**
  311. * @return bool|null
  312. */
  313. public function getHasDarkMode(): ?bool
  314. {
  315. return $this->hasDarkMode;
  316. }
  317. /**
  318. * @param bool $hasDarkMode
  319. * @return $this
  320. */
  321. public function setHasDarkMode(bool $hasDarkMode): self
  322. {
  323. $this->hasDarkMode = $hasDarkMode;
  324. return $this;
  325. }
  326. /**
  327. * @return string|null
  328. */
  329. public function getFirstname(): ?string
  330. {
  331. return $this->firstname;
  332. }
  333. /**
  334. * @param string|null $firstname
  335. * @return User
  336. */
  337. public function setFirstname(?string $firstname): User
  338. {
  339. $this->firstname = $firstname;
  340. return $this;
  341. }
  342. /**
  343. * @return string|null
  344. */
  345. public function getLastname(): ?string
  346. {
  347. return $this->lastname;
  348. }
  349. /**
  350. * @param string|null $lastname
  351. * @return User
  352. */
  353. public function setLastname(?string $lastname): User
  354. {
  355. $this->lastname = $lastname;
  356. return $this;
  357. }
  358. /**
  359. * @return string
  360. */
  361. public function getName(): string
  362. {
  363. return $this->firstname . ' ' . $this->lastname;
  364. }
  365. /**
  366. * @return string|null
  367. */
  368. public function getPhone(): ?string
  369. {
  370. return $this->phone;
  371. }
  372. /**
  373. * @param string|null $phone
  374. * @return User
  375. */
  376. public function setPhone(?string $phone): User
  377. {
  378. $this->phone = $phone;
  379. return $this;
  380. }
  381. /**
  382. * @return Client
  383. */
  384. public function getClient(): Client
  385. {
  386. return $this->client;
  387. }
  388. /**
  389. * @param Client $client
  390. * @return User
  391. */
  392. public function setClient(Client $client): User
  393. {
  394. $this->client = $client;
  395. return $this;
  396. }
  397. /**
  398. * @return ClientGroup|null
  399. */
  400. public function getClientGroup(): ?ClientGroup
  401. {
  402. return $this->clientGroup;
  403. }
  404. /**
  405. * @param ClientGroup|null $clientGroup
  406. * @return User
  407. */
  408. public function setClientGroup(?ClientGroup $clientGroup): User
  409. {
  410. $this->clientGroup = $clientGroup;
  411. return $this;
  412. }
  413. /**
  414. * @return bool
  415. */
  416. public function isEnabled(): bool
  417. {
  418. return $this->enabled;
  419. }
  420. /**
  421. * @param bool $enabled
  422. * @return User
  423. */
  424. public function setEnabled(bool $enabled): User
  425. {
  426. $this->enabled = $enabled;
  427. return $this;
  428. }
  429. /**
  430. * @return string|null
  431. */
  432. public function getEntryPage(): ?string
  433. {
  434. return $this->entryPage;
  435. }
  436. /**
  437. * @param string|null $entryPage
  438. * @return User
  439. */
  440. public function setEntryPage(?string $entryPage): User
  441. {
  442. $this->entryPage = $entryPage;
  443. return $this;
  444. }
  445. /**
  446. * @return Gauge|null
  447. */
  448. public function getEntryPageGauge(): ?Gauge
  449. {
  450. return $this->entryPageGauge;
  451. }
  452. /**
  453. * @param Gauge|null $entryPageGauge
  454. * @return User
  455. */
  456. public function setEntryPageGauge(?Gauge $entryPageGauge): User
  457. {
  458. $this->entryPageGauge = $entryPageGauge;
  459. return $this;
  460. }
  461. /**
  462. * @return int
  463. */
  464. public function getPrimaryUnit(): int
  465. {
  466. return $this->primaryUnit;
  467. }
  468. /**
  469. * @param int $primaryUnit
  470. * @return User
  471. */
  472. public function setPrimaryUnit(int $primaryUnit): User
  473. {
  474. $this->primaryUnit = $primaryUnit;
  475. return $this;
  476. }
  477. /**
  478. * @return Building[]|Collection
  479. */
  480. public function getBuildings()
  481. {
  482. return $this->buildings;
  483. }
  484. /**
  485. * @param Building[]|Collection $buildings
  486. * @return User
  487. */
  488. public function setBuildings($buildings): User
  489. {
  490. $this->buildings = $buildings;
  491. return $this;
  492. }
  493. /**
  494. * @return bool
  495. */
  496. public function isEmailReporting(): bool
  497. {
  498. return $this->emailReporting;
  499. }
  500. /**
  501. * @param bool $emailReporting
  502. * @return User
  503. */
  504. public function setEmailReporting(bool $emailReporting): User
  505. {
  506. $this->emailReporting = $emailReporting;
  507. return $this;
  508. }
  509. /**
  510. * @return bool
  511. */
  512. public function isSmsReporting(): bool
  513. {
  514. return $this->smsReporting;
  515. }
  516. /**
  517. * @param bool $smsReporting
  518. * @return User
  519. */
  520. public function setSmsReporting(bool $smsReporting): User
  521. {
  522. $this->smsReporting = $smsReporting;
  523. return $this;
  524. }
  525. /**
  526. * @return bool|null
  527. */
  528. public function getEnabled(): ?bool
  529. {
  530. return $this->enabled;
  531. }
  532. /**
  533. * @return bool|null
  534. */
  535. public function getEmailReporting(): ?bool
  536. {
  537. return $this->emailReporting;
  538. }
  539. /**
  540. * @return bool|null
  541. */
  542. public function getSmsReporting(): ?bool
  543. {
  544. return $this->smsReporting;
  545. }
  546. /**
  547. * @return bool
  548. */
  549. public function isIncidentEmailReporting(): bool
  550. {
  551. return $this->incidentEmailReporting;
  552. }
  553. /**
  554. * @return bool|null
  555. */
  556. public function getIncidentEmailReporting(): ?bool
  557. {
  558. return $this->incidentEmailReporting;
  559. }
  560. /**
  561. * @param bool $incidentEmailReporting
  562. * @return User
  563. */
  564. public function setIncidentEmailReporting(bool $incidentEmailReporting): User
  565. {
  566. $this->incidentEmailReporting = $incidentEmailReporting;
  567. return $this;
  568. }
  569. /**
  570. * @param Building $building
  571. * @return $this
  572. */
  573. public function addBuilding(Building $building): self
  574. {
  575. if (!$this->buildings->contains($building)) {
  576. $this->buildings[] = $building;
  577. }
  578. return $this;
  579. }
  580. /**
  581. * @param Building $building
  582. * @return $this
  583. */
  584. public function removeBuilding(Building $building): self
  585. {
  586. if ($this->buildings->contains($building)) {
  587. $this->buildings->removeElement($building);
  588. }
  589. return $this;
  590. }
  591. /**
  592. * @return BuildingListField[]|Collection
  593. */
  594. public function getBuildingListFields()
  595. {
  596. return $this->buildingListFields;
  597. }
  598. /**
  599. * @param BuildingListField[]|Collection $buildingListFields
  600. * @return User
  601. */
  602. public function setBuildingListFields($buildingListFields): self
  603. {
  604. $this->buildingListFields = $buildingListFields;
  605. return $this;
  606. }
  607. /**
  608. * @return Role
  609. */
  610. public function getRole(): Role
  611. {
  612. return $this->role;
  613. }
  614. /**
  615. * @param Role $role
  616. * @return User
  617. */
  618. public function setRole(Role $role): User
  619. {
  620. $this->role = $role;
  621. return $this;
  622. }
  623. /**
  624. * @return string[]
  625. */
  626. public function getRoles(): array
  627. {
  628. $roles = [];
  629. $roles[] = $this->getRole()->getName();
  630. // guarantee every user at least has ROLE_USER
  631. if (!in_array('ROLE_USER', $roles, true)) {
  632. $roles[] = 'ROLE_USER';
  633. }
  634. return array_unique($roles);
  635. }
  636. /**
  637. * @return Collection|ActivityLog[]
  638. */
  639. public function getActivities(): Collection
  640. {
  641. return $this->activities;
  642. }
  643. /**
  644. * @return GaugeListField[]|Collection
  645. */
  646. public function getGaugeListFields()
  647. {
  648. return $this->gaugeListFields;
  649. }
  650. /**
  651. * @param GaugeListField[]|Collection $gaugeListFields
  652. * @return User
  653. */
  654. public function setGaugeListFields($gaugeListFields)
  655. {
  656. $this->gaugeListFields = $gaugeListFields;
  657. return $this;
  658. }
  659. /***
  660. * @return string
  661. */
  662. public function __toString() :string
  663. {
  664. return $this->getName();
  665. }
  666. /**
  667. * @return Client|null
  668. */
  669. public function getMainManagerOf(): ?Client
  670. {
  671. return $this->mainManagerOf;
  672. }
  673. /**
  674. * @param Client|null $mainManagerOf
  675. * @return User
  676. */
  677. public function setMainManagerOf(?Client $mainManagerOf): User
  678. {
  679. $this->mainManagerOf = $mainManagerOf;
  680. return $this;
  681. }
  682. /**
  683. * @return bool
  684. */
  685. public function isSendInitMail(): bool
  686. {
  687. return $this->sendInitMail;
  688. }
  689. /**
  690. * @param bool $sendInitMail
  691. * @return User
  692. */
  693. public function setSendInitMail(bool $sendInitMail): User
  694. {
  695. $this->sendInitMail = $sendInitMail;
  696. return $this;
  697. }
  698. /**
  699. * @return string
  700. */
  701. public function getUserIdentifier(): string
  702. {
  703. return $this->getUsername();
  704. }
  705. /**
  706. * @return bool
  707. */
  708. public function isEmailReminder(): bool
  709. {
  710. return $this->emailReminder;
  711. }
  712. /**
  713. * @param bool $emailReminder
  714. * @return User
  715. */
  716. public function setEmailReminder(bool $emailReminder): User
  717. {
  718. $this->emailReminder = $emailReminder;
  719. return $this;
  720. }
  721. /**
  722. * @return UserWidget[]|Collection
  723. */
  724. public function getWidgets()
  725. {
  726. return $this->widgets;
  727. }
  728. /**
  729. * @param UserWidget $userWidget
  730. * @return $this
  731. */
  732. public function addWidget(UserWidget $userWidget): User
  733. {
  734. $userWidget->setUser($this);
  735. if (!$this->widgets->contains($userWidget)) {
  736. $this->widgets[] = $userWidget;
  737. }
  738. return $this;
  739. }
  740. /**
  741. * @return \DateTime|null
  742. */
  743. public function getAgreementAt(): ?\DateTime
  744. {
  745. return $this->agreementAt;
  746. }
  747. /**
  748. * @param \DateTime|null $agreementAt
  749. * @return User
  750. */
  751. public function setAgreementAt(?\DateTime $agreementAt): User
  752. {
  753. $this->agreementAt = $agreementAt;
  754. return $this;
  755. }
  756. /**
  757. * @return Building[]|Collection
  758. */
  759. public function getAllowedBuildings()
  760. {
  761. if ($this->getRole()->hasPermission('ROLE_CAN_SEE_ALL_BUILDINGS')) {
  762. return $this->getClient()->getBuildings();
  763. }
  764. return $this->getBuildings();
  765. }
  766. /**
  767. * @return array
  768. */
  769. public function getAllowedGauges():array
  770. {
  771. $gauges = [];
  772. foreach ($this->getAllowedBuildings() as $building) {
  773. foreach ($building->getGauges() as $gauge) {
  774. $gauges[] = $gauge;
  775. }
  776. }
  777. return $gauges;
  778. }
  779. public function getAllowedDomains(): Collection
  780. {
  781. return $this->allowedDomains;
  782. }
  783. public function setAllowedDomains(Collection $allowedDomains): User
  784. {
  785. $this->allowedDomains = $allowedDomains;
  786. return $this;
  787. }
  788. public function addAllowedDomain(UrlDomain $domain): User
  789. {
  790. if (!$this->allowedDomains->contains($domain)) {
  791. $this->allowedDomains->add($domain);
  792. }
  793. return $this;
  794. }
  795. public function hasAccessToDomain(string $hostDomain):bool
  796. {
  797. foreach ($this->allowedDomains as $domainOnj) {
  798. if (in_array($hostDomain, $domainOnj->getAliases(), true)) {
  799. return true;
  800. }
  801. }
  802. return false;
  803. }
  804. public function getDefaultDomain(): UrlDomain
  805. {
  806. foreach ($this->allowedDomains as $domain) {
  807. if ($domain->getName() === UrlDomain::DEFAULT_DOMAIN) {
  808. return $domain;
  809. }
  810. }
  811. return $this->allowedDomains->first();
  812. }
  813. public function getLastActivity(): ?\DateTimeInterface
  814. {
  815. return $this->lastActivity;
  816. }
  817. public function setLastActivity(?\DateTimeInterface $lastActivity): User
  818. {
  819. $this->lastActivity = $lastActivity;
  820. return $this;
  821. }
  822. }