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