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\OneToMany(targetEntity=WebpushSubscription::class, mappedBy="User", orphanRemoval=true)
  114. */
  115. private $webpushSubscriptions;
  116. /**
  117. * @ORM\ManyToMany(targetEntity=Portfolio::class)
  118. * @ORM\JoinTable(name="user_portfolios")
  119. */
  120. private $userPortfolios;
  121. /**
  122. * @var bool
  123. */
  124. private $sendInvitation;
  125. private $customers;
  126. public function __construct() {
  127. $this->setCreated(new \DateTime());
  128. $this->events = new ArrayCollection();
  129. $this->webpushSubscriptions = new ArrayCollection();
  130. $this->consultants = new ArrayCollection();
  131. $this->customers = new ArrayCollection();
  132. $this->userPortfolios = new ArrayCollection();
  133. }
  134. public function getLogger() {
  135. return $this->logger;
  136. }
  137. public function getId(): ?int {
  138. return $this->id;
  139. }
  140. public function setId($id): ?int {
  141. return $this->id;
  142. }
  143. public function getEmail(): ?string {
  144. return $this->email;
  145. }
  146. public function setEmail(string $email): self {
  147. $this->email = $email;
  148. return $this;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getUserIdentifier(): string {
  154. return (string)$this->email;
  155. }
  156. /**
  157. * @see UserInterface
  158. */
  159. public function getRoles(): ?array {
  160. $roles = $this->roles;
  161. // guarantee every user at least has ROLE_USER
  162. // $roles[] = 'ROLE_USER';
  163. return array_unique($roles);
  164. }
  165. public function setRoles($roles): self {
  166. $this->roles = $roles;
  167. return $this;
  168. }
  169. /**
  170. * @see UserInterface
  171. */
  172. public function getPassword(): string {
  173. return (string)$this->password;
  174. }
  175. public function setPassword($password): self {
  176. if (!empty($password)) {
  177. $this->password = $password;
  178. }
  179. return $this;
  180. }
  181. /**
  182. * @return string|null
  183. */
  184. public function getSalt() {
  185. // not needed when using the "bcrypt" algorithm in security.yaml
  186. }
  187. /**
  188. * @see UserInterface
  189. */
  190. public function eraseCredentials() {
  191. // If you store any temporary, sensitive data on the user, clear it here
  192. }
  193. public function __toString() {
  194. return (string)$this->email;
  195. }
  196. public function getEnabled(): ?bool {
  197. return $this->enabled;
  198. }
  199. public function setEnabled(bool $enabled): self {
  200. $this->enabled = $enabled;
  201. return $this;
  202. }
  203. public function getCreated(): ?\DateTimeInterface {
  204. return $this->created;
  205. }
  206. public function setCreated(\DateTimeInterface $created): self {
  207. $this->created = $created;
  208. return $this;
  209. }
  210. public function getLastLoginAt(): ?\DateTimeInterface {
  211. return $this->lastLoginAt;
  212. }
  213. public function setLastLoginAt(?\DateTimeInterface $lastLoginAt): self {
  214. $this->lastLoginAt = $lastLoginAt;
  215. return $this;
  216. }
  217. public function getCustomer() {
  218. return $this->customer;
  219. }
  220. public function setCustomer($customer) {
  221. $this->customer = $customer;
  222. return $this;
  223. }
  224. public function isLimitedUser() {
  225. return $this->isUser() && $this->userPortfolios->count() > 0 ;
  226. }
  227. public function isUser() {
  228. $roles = $this->getRoles();
  229. $userRoles = array_unique($roles);
  230. return count($userRoles) === 1 && in_array('ROLE_USER', $userRoles, true);
  231. }
  232. public function isManager() {
  233. $roles = $this->getRoles();
  234. return in_array('ROLE_MANAGER', $roles);
  235. }
  236. public function isAdmin() {
  237. $roles = $this->getRoles();
  238. return (in_array('ROLE_ADMIN', $roles) || in_array('ROLE_SUPER_ADMIN', $roles));
  239. }
  240. public function isSuperAdmin() {
  241. $roles = $this->getRoles();
  242. return in_array('ROLE_SUPER_ADMIN', $roles);
  243. }
  244. public function getPasswRestoreKey(): ?string {
  245. return $this->passwRestoreKey;
  246. }
  247. public function setPasswRestoreKey(?string $passwRestoreKey): self {
  248. $this->passwRestoreKey = $passwRestoreKey;
  249. return $this;
  250. }
  251. public function getPasswRestoreKeyValidTil(): ?\DateTimeInterface {
  252. return $this->passwRestoreKeyValidTil;
  253. }
  254. public function setPasswRestoreKeyValidTil(?\DateTimeInterface $passwRestoreKeyValidTil): self {
  255. $this->passwRestoreKeyValidTil = $passwRestoreKeyValidTil;
  256. return $this;
  257. }
  258. public function getPasswChanged(): ?\DateTimeInterface {
  259. return $this->passwChanged;
  260. }
  261. public function setPasswChanged(?\DateTimeInterface $passwChanged): self {
  262. $this->passwChanged = $passwChanged;
  263. return $this;
  264. }
  265. public function getPasswChangeCount(): ?int {
  266. return $this->passwChangeCount;
  267. }
  268. public function setPasswChangeCount(?int $passwChangeCount): self {
  269. $this->passwChangeCount = $passwChangeCount;
  270. return $this;
  271. }
  272. public function getAdminPhone(): ?string {
  273. return $this->adminPhone;
  274. }
  275. public function setAdminPhone(string $adminPhone): self {
  276. $this->adminPhone = $adminPhone;
  277. return $this;
  278. }
  279. public function getApiKey(): ?string {
  280. return $this->apiKey;
  281. }
  282. public function setApiKey(?string $apiKey): self {
  283. $this->apiKey = $apiKey;
  284. return $this;
  285. }
  286. public function getAllowedIps(): ?string {
  287. return $this->allowedIps;
  288. }
  289. public function setAllowedIps(?string $allowedIps): self {
  290. $this->allowedIps = $allowedIps;
  291. return $this;
  292. }
  293. /**
  294. * @return Collection<int, Event>
  295. */
  296. public function getEvents(): Collection {
  297. return $this->events;
  298. }
  299. public function addEvent(Event $event): self {
  300. if (!$this->events->contains($event)) {
  301. $this->events[] = $event;
  302. $event->setUser($this);
  303. }
  304. return $this;
  305. }
  306. public function removeEvent(Event $event): self {
  307. if ($this->events->removeElement($event)) {
  308. // set the owning side to null (unless already changed)
  309. if ($event->getUser() === $this) {
  310. $event->setUser(null);
  311. }
  312. }
  313. return $this;
  314. }
  315. /**
  316. * @return Collection<int, Notification>
  317. */
  318. public function getNotifications() {
  319. return $this->notifications;
  320. }
  321. public function setNotifications($notifications) {
  322. return $this->notifications = $notifications;
  323. }
  324. /**
  325. * @return Collection<int, WebpushSubscription>
  326. */
  327. public function getWebpushSubscriptions(): Collection {
  328. return $this->webpushSubscriptions;
  329. }
  330. public function addWebpushSubscription(WebpushSubscription $webpushSubscription): self {
  331. if (!$this->webpushSubscriptions->contains($webpushSubscription)) {
  332. $this->webpushSubscriptions[] = $webpushSubscription;
  333. $webpushSubscription->setUser($this);
  334. }
  335. return $this;
  336. }
  337. public function removeWebpushSubscription(WebpushSubscription $webpushSubscription): self {
  338. if ($this->webpushSubscriptions->removeElement($webpushSubscription)) {
  339. // set the owning side to null (unless already changed)
  340. if ($webpushSubscription->getUser() === $this) {
  341. $webpushSubscription->setUser(null);
  342. }
  343. }
  344. return $this;
  345. }
  346. public function getUsername() {
  347. return $this->email;
  348. }
  349. public function getSendInvitation() {
  350. return $this->sendInvitation;
  351. }
  352. public function setCustomers($customers) {
  353. $this->customers = $customers;
  354. }
  355. public function getCustomers() {
  356. return $this->customers;
  357. }
  358. public function setSendInvitation($sendInvitation) {
  359. $this->sendInvitation = $sendInvitation;
  360. }
  361. public function getConsultants(): Collection|array {
  362. return $this->consultants;
  363. }
  364. public function setConsultants(Collection|array $consultants): void {
  365. $this->consultants = $consultants;
  366. }
  367. public function addConsultants(Customer $consultants): self {
  368. if (!$this->consultants->contains($consultants)) {
  369. $this->consultants[] = $consultants;
  370. }
  371. return $this;
  372. }
  373. public function removeConsultants(Collection $consultants): self {
  374. $this->consultants->removeElement($consultants);
  375. return $this;
  376. }
  377. public function isConsultant(): ?bool {
  378. return !empty($this->consultants);
  379. }
  380. public function setIsConsultant(?bool $isConsultant): void {
  381. $this->isConsultant = $isConsultant;
  382. }
  383. public function getNotificationTypes() {
  384. return $this->notificationTypes;
  385. }
  386. public function setNotificationTypes(array $notificationTypes): void {
  387. $this->notificationTypes = $notificationTypes;
  388. }
  389. public function isAccessRemoteSystems(): ?bool {
  390. return $this->accessRemoteSystems;
  391. }
  392. public function setAccessRemoteSystems(?bool $accessRemoteSystems): void {
  393. $this->accessRemoteSystems = $accessRemoteSystems;
  394. }
  395. public function getUserPortfolios(): Collection {
  396. return $this->userPortfolios;
  397. }
  398. public function setUserPortfolios(Collection $portfolios = null): self {
  399. $this->userPortfolios = new ArrayCollection();
  400. foreach ($portfolios as $portfolio) {
  401. $this->userPortfolios[] = $portfolio;
  402. }
  403. return $this;
  404. }
  405. public function removeUserPortfolio(Portfolio $portfolio): self
  406. {
  407. $this->userPortfolios->removeElement($portfolio);
  408. return $this;
  409. }
  410. }