src/Entity/Account/User.php line 50

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