src/Entity/Account/User.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Account;
  3. use App\Entity\Channel\Channel;
  4. use App\Entity\Channel\Group;
  5. use App\Entity\Channel\Service;
  6. use App\Entity\Channel\UserFromChannel;
  7. use App\Entity\ChannelUserData\ChannelUserData;
  8. use App\Entity\Common\Address;
  9. use App\Entity\Common\Contact;
  10. use App\Entity\Common\ImportableData\ImportableDataInterface;
  11. use App\Entity\Common\ImportableData\ImportableDataTrait;
  12. use App\Entity\Sync\SyncableWithExcludedPropertiesInterface;
  13. use App\Repository\Account\UserRepository;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Drosalys\Bundle\ApiBundle\Serializer\Attributes\IdGroups;
  18. use DrosalysWeb\ObjectExtensions\Blame\Model\BlameInterface;
  19. use DrosalysWeb\ObjectExtensions\Blame\Model\BlameTrait;
  20. use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
  21. use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
  22. use Nellapp\Bundle\SDKBundle\Auth\Entity\EnabledUserInterface;
  23. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  24. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserMethodTrait;
  25. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserWithContact\UserWithContactInterface;
  26. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserWithContact\UserWithContactTrait;
  27. use Nellapp\Bundle\SDKBundle\Sync\EntityKeys;
  28. use OpenApi\Annotations as OA;
  29. use Symfony\Bridge\Doctrine\IdGenerator\UuidGenerator;
  30. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  31. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  32. use Symfony\Component\Serializer\Annotation as Serial;
  33. use Symfony\Component\Validator\Constraints as Assert;
  34. /**
  35. * @method self|null getCreatedBy()
  36. * @method self setCreatedBy(self $createdBy)
  37. * @method self|null getUpdatedBy()
  38. * @method self setUpdatedBy(self $updatedBy)
  39. */
  40. #[UniqueEntity(fields: 'email', message: 'user.entity.email.UniqueEntity', groups: ['register', 'user_edition', 'super_admin_user_edition', 'admin_user_edition', 'flow_channel_user_create_step1', 'admin_learner_profile', 'admin_admin_profile'])]
  41. #[UniqueEntity(fields: 'id', message: 'user.entity.id.UniqueEntity', groups: ['register', 'flow_channel_user_create_step1'])]
  42. #[
  43. ORM\Entity(repositoryClass: UserRepository::class),
  44. ORM\Table(name: 'account_users'),
  45. ORM\Index(fields: ['email'], flags: ['fulltext']),
  46. ORM\Index(fields: ['firstName'], flags: ['fulltext']),
  47. ORM\Index(fields: ['lastName'], flags: ['fulltext']),
  48. ]
  49. class User implements
  50. UserInterface,
  51. EnabledUserInterface,
  52. UserWithContactInterface,
  53. PasswordAuthenticatedUserInterface,
  54. TimestampInterface,
  55. BlameInterface,
  56. SyncableWithExcludedPropertiesInterface,
  57. ImportableDataInterface
  58. {
  59. use BlameTrait;
  60. use TimestampTrait;
  61. use UserMethodTrait;
  62. use UserWithContactTrait;
  63. use ImportableDataTrait;
  64. #[
  65. ORM\Id, ORM\GeneratedValue(strategy: 'CUSTOM'),
  66. ORM\CustomIdGenerator(class: UuidGenerator::class),
  67. ORM\Column(type: 'string', length: 36)
  68. ]
  69. #[Serial\Groups(['Profile', 'Manager', 'Sync', 'GlobalJSVariable', 'activity:get'])]
  70. private ?string $id = null;
  71. #[ORM\Column(type: 'string', length: 180, unique: true)]
  72. #[
  73. Assert\NotBlank(message: 'user.entity.email.NotBlank', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step1', 'admin_learner_profile', 'admin_admin_profile']),
  74. Assert\Email(message: 'user.entity.email.Email', mode: 'strict', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step1', 'admin_learner_profile', 'admin_admin_profile']),
  75. Assert\Length(
  76. min: 6,
  77. max: 64,
  78. minMessage: 'user.entity.email.Length.min',
  79. maxMessage: 'user.entity.email.Length.max',
  80. groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step1', 'admin_learner_profile', 'admin_admin_profile']),
  81. ]
  82. #[Serial\Groups(['Profile', 'Manager', 'UserPostManager', 'Sync', 'activity:get'])]
  83. private ?string $email = null;
  84. /**
  85. * @var string[]
  86. */
  87. #[ORM\Column(type: 'json')]
  88. #[Serial\Groups(['Profile', 'Sync'])]
  89. private array $roles = [];
  90. #[ORM\Column(type: 'string')]
  91. #[
  92. Assert\NotBlank(message: 'user.entity.password.NotBlank', groups: ['register', 'user_edition']),
  93. Assert\Length(
  94. min: 8,
  95. max: 255,
  96. minMessage: 'user.entity.password.Length.min',
  97. maxMessage: 'user.entity.password.Length.max',
  98. groups: ['register', 'user_edition']),
  99. Assert\Regex(
  100. pattern: '/(?=.*[0-9])/',
  101. message: 'user.entity.password.Regex.digit',
  102. groups: ['register', 'user_edition']),
  103. Assert\Regex(
  104. pattern: '/(?=.*[A-Z])/',
  105. message: 'user.entity.password.Regex.up',
  106. groups: ['register', 'user_edition']),
  107. Assert\Regex(
  108. pattern: '/(?=.*[a-z])/',
  109. message: 'user.entity.password.Regex.low',
  110. groups: ['register', 'user_edition']),
  111. ]
  112. private ?string $password = null;
  113. #[ORM\Column(type: 'boolean')]
  114. #[
  115. Assert\NotNull(message: 'user.entity.enabled.NotNull'),
  116. Assert\Type(type: 'boolean', message: 'user.entity.enabled.Type')
  117. ]
  118. private bool $enabled = true;
  119. #[ORM\Column(type: 'string', length: 63, nullable: true)]
  120. private ?string $passwordToken = null;
  121. #[ORM\Column(type: 'datetime', nullable: true)]
  122. private ?\DateTimeInterface $passwordTokenAt = null;
  123. #[ORM\Column(type: 'string', nullable: true)]
  124. #[
  125. Assert\NotBlank(message: 'user.entity.firstName.NotBlank', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile', 'admin_admin_profile']),
  126. Assert\Length(
  127. min: 2,
  128. max: 255,
  129. minMessage: 'user.entity.firstName.Length.min',
  130. maxMessage: 'user.entity.firstName.Length.max',
  131. groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile', 'admin_admin_profile'])
  132. ]
  133. #[Serial\Groups(['Profile', 'Manager', 'UserPostManager', 'Sync', 'GlobalJSVariable', 'activity:get'])]
  134. private ?string $firstName = null;
  135. #[ORM\Column(type: 'string', nullable: true)]
  136. #[
  137. Assert\NotBlank(message: 'user.entity.lastName.NotBlank', groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile', 'admin_admin_profile']),
  138. Assert\Length(
  139. min: 2,
  140. max: 255,
  141. minMessage: 'user.entity.lastName.Length.min',
  142. maxMessage: 'user.entity.lastName.Length.max',
  143. groups: ['register', 'user_edition', 'admin_user_edition', 'super_admin_user_edition', 'group_user', 'flow_channel_user_create_step2', 'admin_learner_profile', 'admin_admin_profile'])
  144. ]
  145. #[Serial\Groups(['Profile', 'Manager', 'UserPostManager', 'Sync', 'GlobalJSVariable', 'activity:get'])]
  146. private ?string $lastName = null;
  147. #[ORM\Column(type: 'string', length: 20, nullable: true)]
  148. #[Assert\Length(
  149. min: 8,
  150. max: 20,
  151. minMessage: 'user.entity.phone.Length.min',
  152. maxMessage: 'user.entity.phone.Length.max',
  153. groups: ['user_edition', 'admin_user_edition', 'super_admin_user_edition', 'flow_channel_user_create_step2', 'admin_learner_profile', 'admin_admin_profile'])
  154. ]
  155. private ?string $phoneNumber = null;
  156. #[ORM\Column(type: 'text', nullable: true)]
  157. #[Serial\Groups(['Profile', 'Sync'])]
  158. private ?string $description = null;
  159. #[ORM\Column(type: 'string', nullable: true)]
  160. #[Assert\Length(max: 255, groups: ['user_edition'])]
  161. private ?string $roleDescription = null;
  162. #[ORM\Column(type: 'boolean', nullable: true)]
  163. private ?bool $avatar = null;
  164. #[ORM\Column(type: 'text', nullable: true)]
  165. #[Serial\Groups(['Sync', 'Manager', 'GlobalJSVariable', 'activity:get'])]
  166. private ?string $avatarPath = null;
  167. #[ORM\Column(type: 'string', length: 2, nullable: true)]
  168. #[Assert\Choice(choices: ['m', 'f', 'o'], message: 'user.entity.gender.Choice', groups: ['user_edition', 'admin_user_edition', 'super_admin_user_edition', 'admin_learner_profile', 'admin_admin_profile'])]
  169. private ?string $gender = null;
  170. #[ORM\Column(type: 'date', nullable: true)]
  171. private ?\DateTimeInterface $birthAt = null;
  172. #[ORM\Embedded(class: Address::class)]
  173. #[Assert\Valid(groups: ['user_edition', 'flow_channel_user_create_step2', 'admin_learner_profile', 'admin_admin_profile'])]
  174. private ?Address $address = null;
  175. #[ORM\Column(type: 'boolean')]
  176. private bool $isVerified = false;
  177. #[ORM\OneToOne(mappedBy: 'user', targetEntity: UserFromChannel::class, cascade: ['remove'], orphanRemoval: true)]
  178. private ?UserFromChannel $userFromChannel = null;
  179. #[ORM\OneToOne(inversedBy: 'user', targetEntity: Contact::class, cascade: ['persist'])]
  180. #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
  181. #[IdGroups(['Sync'])]
  182. private ?Contact $contact = null;
  183. #[ORM\Column(type: 'datetime', nullable: true)]
  184. #[Serial\Groups(['Profile', 'Sync', 'UserPutSigningDate'])]
  185. private ?\DateTime $signingLimitDate = null;
  186. #[ORM\Column(type: 'datetime', nullable: true)]
  187. private ?\DateTime $lastConnectedAt = null;
  188. #[ORM\Column(type: 'boolean', nullable: false)]
  189. private bool $shownAlert = false; // Modal alert displayed for general informations
  190. #[ORM\OneToMany(mappedBy: 'user', targetEntity: ChannelUserData::class)]
  191. private ?Collection $channelUserDatas;
  192. #[ORM\ManyToMany(targetEntity: Group::class, mappedBy: 'users', fetch: 'EXTRA_LAZY')]
  193. private ?Collection $groups;
  194. #[ORM\ManyToMany(targetEntity: Service::class, mappedBy: 'users', fetch: 'EXTRA_LAZY')]
  195. private ?Collection $services;
  196. public function __construct()
  197. {
  198. $this->channelUserDatas = new ArrayCollection();
  199. $this->groups = new ArrayCollection();
  200. $this->services = new ArrayCollection();
  201. }
  202. /**
  203. * Overrides UserMethodTrait::__serialize(): the SDK trait serializes id/email/token,
  204. * but core's User has no $token property (renamed to $passwordToken in 160c40a2,
  205. * Nov 2023) — core is the OAuth server, it never needed the SDK's OAuth-client Token.
  206. */
  207. public function __serialize(): array
  208. {
  209. return [
  210. 'id' => $this->id,
  211. 'email' => $this->email,
  212. ];
  213. }
  214. public function __unserialize(array $data): void
  215. {
  216. $this->id = $data['id'];
  217. $this->email = $data['email'];
  218. }
  219. public static function getSyncKey(): string
  220. {
  221. return EntityKeys::USER;
  222. }
  223. public function getSyncDisplayLabel(): string
  224. {
  225. return $this->getEmail() ?? 'Email';
  226. }
  227. public static function getExcludedProperties(): array
  228. {
  229. return [
  230. 'lastConnectedAt',
  231. ];
  232. }
  233. #[
  234. Serial\SerializedName('username'),
  235. Serial\Groups(['Profile', 'Manager', 'Sync'])
  236. ]
  237. public function getUsername(): string
  238. {
  239. return $this;
  240. }
  241. /** @OA\Property(type="array", @OA\Items(type="string")) */
  242. #[
  243. Serial\Groups(['Profile'])
  244. ]
  245. public function getChannelIds(): array
  246. {
  247. return $this->getChannelUserDatas()->map(fn(ChannelUserData $cud) => $cud->getChannel()->getId())->toArray();
  248. }
  249. public function getRoles(): array
  250. {
  251. return array_unique(array_merge($this->roles, [
  252. 'ROLE_USER',
  253. ]));
  254. }
  255. public function setRoles(array $roles): static
  256. {
  257. $this->roles = array_unique(array_values($roles));
  258. return $this;
  259. }
  260. public function getPassword(): ?string
  261. {
  262. return $this->password;
  263. }
  264. public function setPassword(string $password): static
  265. {
  266. $this->password = $password;
  267. return $this;
  268. }
  269. public function getEnabled(): ?bool
  270. {
  271. return $this->enabled;
  272. }
  273. public function setEnabled(bool $enabled): static
  274. {
  275. $this->enabled = $enabled;
  276. return $this;
  277. }
  278. public function toggleEnabled(): static
  279. {
  280. $this->enabled = !$this->enabled;
  281. return $this;
  282. }
  283. public function getPasswordToken(): ?string
  284. {
  285. return $this->passwordToken;
  286. }
  287. public function setPasswordToken(?string $passwordToken): static
  288. {
  289. $this->passwordToken = $passwordToken;
  290. return $this;
  291. }
  292. public function getPasswordTokenAt(): ?\DateTimeInterface
  293. {
  294. return $this->passwordTokenAt;
  295. }
  296. public function setPasswordTokenAt(?\DateTimeInterface $passwordTokenAt): static
  297. {
  298. $this->passwordTokenAt = $passwordTokenAt;
  299. return $this;
  300. }
  301. public function getPhoneNumber(): ?string
  302. {
  303. return $this->phoneNumber;
  304. }
  305. public function setPhoneNumber(?string $phoneNumber): static
  306. {
  307. $this->phoneNumber = $phoneNumber;
  308. return $this;
  309. }
  310. public function getRoleDescription(): ?string
  311. {
  312. return $this->roleDescription;
  313. }
  314. public function setRoleDescription(?string $roleDescription): static
  315. {
  316. $this->roleDescription = $roleDescription;
  317. return $this;
  318. }
  319. public function getGender(): ?string
  320. {
  321. return $this->gender;
  322. }
  323. public function setGender(?string $gender): static
  324. {
  325. $this->gender = $gender;
  326. return $this;
  327. }
  328. public function getBirthAt(): ?\DateTimeInterface
  329. {
  330. return $this->birthAt;
  331. }
  332. public function setBirthAt(?\DateTimeInterface $birthAt): static
  333. {
  334. $this->birthAt = $birthAt;
  335. return $this;
  336. }
  337. /**
  338. * @return \DateTime|null
  339. */
  340. public function getLastConnectedAt(): ?\DateTime
  341. {
  342. return $this->lastConnectedAt;
  343. }
  344. /**
  345. * @param \DateTime|null $lastConnectedAt
  346. */
  347. public function setLastConnectedAt(?\DateTime $lastConnectedAt): static
  348. {
  349. $this->lastConnectedAt = $lastConnectedAt;
  350. return $this;
  351. }
  352. public function getAddress(): Address
  353. {
  354. return $this->address ?: $this->address = new Address();
  355. }
  356. public function setAddress(Address $address): static
  357. {
  358. $this->address = $address;
  359. return $this;
  360. }
  361. public function isVerified(): bool
  362. {
  363. return $this->isVerified;
  364. }
  365. public function setIsVerified(bool $isVerified): static
  366. {
  367. $this->isVerified = $isVerified;
  368. return $this;
  369. }
  370. public function getGroups(): ?Collection
  371. {
  372. return $this->groups;
  373. }
  374. public function getGroupsByChannel(Channel $channel): ?array
  375. {
  376. return array_filter($this->groups->toArray(), function (Group $group) use ($channel) {
  377. return $group->getChannel() === $channel;
  378. });
  379. }
  380. public function setGroups(?Collection $groups): User
  381. {
  382. $this->groups = $groups;
  383. return $this;
  384. }
  385. public function getServices(): ?Collection
  386. {
  387. return $this->services;
  388. }
  389. public function getUserFromChannel(): ?UserFromChannel
  390. {
  391. return $this->userFromChannel;
  392. }
  393. public function setUserFromChannel(?UserFromChannel $userFromChannel): self
  394. {
  395. $this->userFromChannel = $userFromChannel;
  396. return $this;
  397. }
  398. public function isShownAlert(): bool
  399. {
  400. return $this->shownAlert;
  401. }
  402. public function setShownAlert(bool $shownAlert): User
  403. {
  404. $this->shownAlert = $shownAlert;
  405. return $this;
  406. }
  407. public function serialize(): ?string
  408. {
  409. return serialize([
  410. $this->id,
  411. $this->email,
  412. $this->password,
  413. $this->enabled,
  414. ]);
  415. }
  416. public function unserialize($data): void
  417. {
  418. list(
  419. $this->id,
  420. $this->email,
  421. $this->password,
  422. $this->enabled
  423. ) = unserialize($data);
  424. }
  425. }