src/Entity/User.php line 19

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\Component\Security\Core\User\UserInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Rollerworks\Component\PasswordStrength\Validator\Constraints as RollerworksPassword;
  9. use Bundles\Messenger\Entity\Event;
  10. use Bundles\Messenger\Entity\Notification;
  11. use Bundles\Messenger\Entity\WebpushSubscription;
  12. use Bundles\Portfolios\Entity\Portfolio;
  13. /**
  14. * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  15. */
  16. class User implements UserInterface {
  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. *
  35. * @RollerworksPassword\PasswordRequirements(requireLetters=true, requireNumbers=true, requireCaseDiff=true)
  36. */
  37. private $password;
  38. /**
  39. * @ORM\Column(type="boolean")
  40. */
  41. private $enabled = false;
  42. /**
  43. * @ORM\Column(type="datetime")
  44. */
  45. private $created;
  46. /**
  47. * @var LoggerInterface
  48. */
  49. protected $logger;
  50. /**
  51. * @ORM\Column(type="datetime", nullable=true)
  52. */
  53. private $lastLoginAt;
  54. /**
  55. * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="users")
  56. */
  57. private $customer;
  58. /**
  59. * @ORM\Column(type="boolean", nullable=true)
  60. */
  61. private $isConsultant = false;
  62. /**
  63. * @ORM\Column(type="boolean", nullable=true)
  64. */
  65. private $accessRemoteSystems = false;
  66. /**
  67. * @ORM\ManyToMany(targetEntity=Customer::class)
  68. * @ORM\JoinTable(name="user_consultant")
  69. *
  70. */
  71. private $consultants;
  72. /**
  73. * @ORM\Column(type="string", length=255, nullable=true)
  74. */
  75. private $passwRestoreKey;
  76. /**
  77. * @ORM\Column(type="datetime", nullable=true)
  78. */
  79. private $passwRestoreKeyValidTil;
  80. /**
  81. * @ORM\Column(type="datetime", nullable=true)
  82. */
  83. private $passwChanged;
  84. /**
  85. * @ORM\Column(type="smallint", nullable=true)
  86. */
  87. private $passwChangeCount;
  88. /**
  89. * @ORM\Column(type="string", length=20)
  90. */
  91. private $adminPhone;
  92. /**
  93. * @ORM\Column(type="string", length=255, nullable=true)
  94. */
  95. private $apiKey;
  96. /**
  97. * @ORM\Column(type="string", length=255, nullable=true)
  98. */
  99. private $allowedIps;
  100. /**
  101. * @ORM\OneToMany(targetEntity=Event::class, mappedBy="User")
  102. */
  103. private $events;
  104. /**
  105. * @ORM\Column(type="json", nullable=true)
  106. */
  107. private $notifications = [];
  108. /**
  109. * @ORM\Column(type="json", nullable=true)
  110. */
  111. private $notificationTypes = [];
  112. /**
  113. * @ORM\Column(type="string", length=10, nullable=true)
  114. */
  115. private $theme = 'light';
  116. /**
  117. * @ORM\OneToMany(targetEntity=WebpushSubscription::class, mappedBy="User", orphanRemoval=true)
  118. */
  119. private $webpushSubscriptions;
  120. /**
  121. * @ORM\ManyToMany(targetEntity=Portfolio::class)
  122. * @ORM\JoinTable(name="user_portfolios")
  123. */
  124. private $userPortfolios;
  125. /**
  126. * @var bool
  127. */
  128. private $sendInvitation;
  129. private $customers;
  130. public function __construct() {
  131. $this->setCreated(new \DateTime());
  132. $this->events = new ArrayCollection();
  133. $this->webpushSubscriptions = new ArrayCollection();
  134. $this->consultants = new ArrayCollection();
  135. $this->customers = new ArrayCollection();
  136. $this->userPortfolios = new ArrayCollection();
  137. }
  138. public function getLogger() {
  139. return $this->logger;
  140. }
  141. public function getId(): ?int {
  142. return $this->id;
  143. }
  144. public function setId($id): ?int {
  145. return $this->id;
  146. }
  147. public function getEmail(): ?string {
  148. return $this->email;
  149. }
  150. public function setEmail(string $email): self {
  151. $this->email = $email;
  152. return $this;
  153. }
  154. /**
  155. * @return string
  156. */
  157. public function getUserIdentifier(): string {
  158. return (string)$this->email;
  159. }
  160. /**
  161. * @see UserInterface
  162. */
  163. public function getRoles(): ?array {
  164. $roles = $this->roles;
  165. // guarantee every user at least has ROLE_USER
  166. // $roles[] = 'ROLE_USER';
  167. return array_unique($roles);
  168. }
  169. public function setRoles($roles): self {
  170. $this->roles = $roles;
  171. return $this;
  172. }
  173. /**
  174. * @see UserInterface
  175. */
  176. public function getPassword(): string {
  177. return (string)$this->password;
  178. }
  179. public function setPassword($password): self {
  180. if (!empty($password)) {
  181. $this->password = $password;
  182. }
  183. return $this;
  184. }
  185. /**
  186. * @return string|null
  187. */
  188. public function getSalt() {
  189. // not needed when using the "bcrypt" algorithm in security.yaml
  190. }
  191. /**
  192. * @see UserInterface
  193. */
  194. public function eraseCredentials() {
  195. // If you store any temporary, sensitive data on the user, clear it here
  196. }
  197. public function __toString() {
  198. return (string)$this->email;
  199. }
  200. public function getEnabled(): ?bool {
  201. return $this->enabled;
  202. }
  203. public function setEnabled(bool $enabled): self {
  204. $this->enabled = $enabled;
  205. return $this;
  206. }
  207. public function getCreated(): ?\DateTimeInterface {
  208. return $this->created;
  209. }
  210. public function setCreated(\DateTimeInterface $created): self {
  211. $this->created = $created;
  212. return $this;
  213. }
  214. public function getLastLoginAt(): ?\DateTimeInterface {
  215. return $this->lastLoginAt;
  216. }
  217. public function setLastLoginAt(?\DateTimeInterface $lastLoginAt): self {
  218. $this->lastLoginAt = $lastLoginAt;
  219. return $this;
  220. }
  221. public function getCustomer() {
  222. return $this->customer;
  223. }
  224. public function setCustomer($customer) {
  225. $this->customer = $customer;
  226. return $this;
  227. }
  228. public function isLimitedUser() {
  229. return $this->isUser() && $this->userPortfolios->count() > 0 ;
  230. }
  231. public function isUser() {
  232. $roles = $this->getRoles();
  233. $userRoles = array_unique($roles);
  234. return count($userRoles) === 1 && in_array('ROLE_USER', $userRoles, true);
  235. }
  236. public function isManager() {
  237. $roles = $this->getRoles();
  238. return in_array('ROLE_MANAGER', $roles);
  239. }
  240. public function isAdmin() {
  241. $roles = $this->getRoles();
  242. return (in_array('ROLE_ADMIN', $roles) || in_array('ROLE_SUPER_ADMIN', $roles));
  243. }
  244. public function isSuperAdmin() {
  245. $roles = $this->getRoles();
  246. return in_array('ROLE_SUPER_ADMIN', $roles);
  247. }
  248. public function getPasswRestoreKey(): ?string {
  249. return $this->passwRestoreKey;
  250. }
  251. public function setPasswRestoreKey(?string $passwRestoreKey): self {
  252. $this->passwRestoreKey = $passwRestoreKey;
  253. return $this;
  254. }
  255. public function getPasswRestoreKeyValidTil(): ?\DateTimeInterface {
  256. return $this->passwRestoreKeyValidTil;
  257. }
  258. public function setPasswRestoreKeyValidTil(?\DateTimeInterface $passwRestoreKeyValidTil): self {
  259. $this->passwRestoreKeyValidTil = $passwRestoreKeyValidTil;
  260. return $this;
  261. }
  262. public function getPasswChanged(): ?\DateTimeInterface {
  263. return $this->passwChanged;
  264. }
  265. public function setPasswChanged(?\DateTimeInterface $passwChanged): self {
  266. $this->passwChanged = $passwChanged;
  267. return $this;
  268. }
  269. public function getPasswChangeCount(): ?int {
  270. return $this->passwChangeCount;
  271. }
  272. public function setPasswChangeCount(?int $passwChangeCount): self {
  273. $this->passwChangeCount = $passwChangeCount;
  274. return $this;
  275. }
  276. public function getAdminPhone(): ?string {
  277. return $this->adminPhone;
  278. }
  279. public function setAdminPhone(string $adminPhone): self {
  280. $this->adminPhone = $adminPhone;
  281. return $this;
  282. }
  283. public function getApiKey(): ?string {
  284. return $this->apiKey;
  285. }
  286. public function setApiKey(?string $apiKey): self {
  287. $this->apiKey = $apiKey;
  288. return $this;
  289. }
  290. public function getAllowedIps(): ?string {
  291. return $this->allowedIps;
  292. }
  293. public function setAllowedIps(?string $allowedIps): self {
  294. $this->allowedIps = $allowedIps;
  295. return $this;
  296. }
  297. /**
  298. * @return Collection<int, Event>
  299. */
  300. public function getEvents(): Collection {
  301. return $this->events;
  302. }
  303. public function addEvent(Event $event): self {
  304. if (!$this->events->contains($event)) {
  305. $this->events[] = $event;
  306. $event->setUser($this);
  307. }
  308. return $this;
  309. }
  310. public function removeEvent(Event $event): self {
  311. if ($this->events->removeElement($event)) {
  312. // set the owning side to null (unless already changed)
  313. if ($event->getUser() === $this) {
  314. $event->setUser(null);
  315. }
  316. }
  317. return $this;
  318. }
  319. /**
  320. * @return Collection<int, Notification>
  321. */
  322. public function getNotifications() {
  323. return $this->notifications;
  324. }
  325. public function setNotifications($notifications) {
  326. return $this->notifications = $notifications;
  327. }
  328. /**
  329. * @return Collection<int, WebpushSubscription>
  330. */
  331. public function getWebpushSubscriptions(): Collection {
  332. return $this->webpushSubscriptions;
  333. }
  334. public function addWebpushSubscription(WebpushSubscription $webpushSubscription): self {
  335. if (!$this->webpushSubscriptions->contains($webpushSubscription)) {
  336. $this->webpushSubscriptions[] = $webpushSubscription;
  337. $webpushSubscription->setUser($this);
  338. }
  339. return $this;
  340. }
  341. public function removeWebpushSubscription(WebpushSubscription $webpushSubscription): self {
  342. if ($this->webpushSubscriptions->removeElement($webpushSubscription)) {
  343. // set the owning side to null (unless already changed)
  344. if ($webpushSubscription->getUser() === $this) {
  345. $webpushSubscription->setUser(null);
  346. }
  347. }
  348. return $this;
  349. }
  350. public function getUsername() {
  351. return $this->email;
  352. }
  353. public function getSendInvitation() {
  354. return $this->sendInvitation;
  355. }
  356. public function setCustomers($customers) {
  357. $this->customers = $customers;
  358. }
  359. public function getCustomers() {
  360. return $this->customers;
  361. }
  362. public function setSendInvitation($sendInvitation) {
  363. $this->sendInvitation = $sendInvitation;
  364. }
  365. public function getConsultants(): Collection|array {
  366. return $this->consultants;
  367. }
  368. public function setConsultants(Collection|array $consultants): void {
  369. $this->consultants = $consultants;
  370. }
  371. public function addConsultants(Customer $consultants): self {
  372. if (!$this->consultants->contains($consultants)) {
  373. $this->consultants[] = $consultants;
  374. }
  375. return $this;
  376. }
  377. public function removeConsultants(Collection $consultants): self {
  378. $this->consultants->removeElement($consultants);
  379. return $this;
  380. }
  381. public function isConsultant(): ?bool {
  382. return !empty($this->consultants);
  383. }
  384. public function setIsConsultant(?bool $isConsultant): void {
  385. $this->isConsultant = $isConsultant;
  386. }
  387. public function getNotificationTypes() {
  388. return $this->notificationTypes;
  389. }
  390. public function setNotificationTypes(array $notificationTypes): void {
  391. $this->notificationTypes = $notificationTypes;
  392. }
  393. public function getTheme(): string {
  394. return $this->theme ?? 'light';
  395. }
  396. public function setTheme(?string $theme): void {
  397. $this->theme = $theme;
  398. }
  399. public function isAccessRemoteSystems(): ?bool {
  400. return $this->accessRemoteSystems;
  401. }
  402. public function setAccessRemoteSystems(?bool $accessRemoteSystems): void {
  403. $this->accessRemoteSystems = $accessRemoteSystems;
  404. }
  405. public function getUserPortfolios(): Collection {
  406. return $this->userPortfolios;
  407. }
  408. public function setUserPortfolios(Collection $portfolios = null): self {
  409. $this->userPortfolios = new ArrayCollection();
  410. foreach ($portfolios as $portfolio) {
  411. $this->userPortfolios[] = $portfolio;
  412. }
  413. return $this;
  414. }
  415. public function removeUserPortfolio(Portfolio $portfolio): self
  416. {
  417. $this->userPortfolios->removeElement($portfolio);
  418. return $this;
  419. }
  420. }