src/Entity/User.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Yaml\Yaml;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. #[ORM\Entity(repositoryClassUserRepository::class)]
  13. #[ORM\Table(name"user")]
  14. #[UniqueEntity(
  15.     fields: ['email'],
  16.     message'An account with this email already exists. Try logging in, or reset your password.'
  17. )]
  18. class User implements UserInterfacePasswordAuthenticatedUserInterface
  19. {
  20.     #[ORM\Id]
  21.     #[ORM\GeneratedValue]
  22.     #[ORM\Column(type"integer")]
  23.     private ?int $id null;
  24.     #[ORM\Column(type"string"length180uniquetrue)]
  25.     private string $email;
  26.     #[ORM\Column(length255nullabletrue)]
  27.     private ?string $email2 null;
  28.     #[ORM\Column(type'string'length255nullabletrue)]
  29.     private ?string $email3 null;
  30.     #[ORM\Column(type'boolean'nullabletrue)]
  31.     private ?bool $emailVerified null;
  32.     #[ORM\ManyToMany(targetEntityRole::class, inversedBy'users')]
  33.     #[ORM\JoinTable(name'user_role')]
  34.     private Collection $roles;
  35.     #[ORM\Column(type'string')]
  36.     private string $password;
  37.     #[ORM\Column(type'string'length255nullabletrue)]
  38.     private ?string $salutation null;
  39.     #[ORM\Column(type'string'length255nullabletrue)]
  40.     private ?string $firstName null;
  41.     #[ORM\Column(type'string'length255nullabletrue)]
  42.     private ?string $lastName null;
  43.     #[ORM\Column(type'string'length255nullabletrue)]
  44.     private ?string $fullName null;
  45.     #[ORM\Column(type'string'length255nullabletrue)]
  46.     private ?string $mobile null;
  47.     #[ORM\Column(type'string'length255nullabletrue)]
  48.     private ?string $mobile2 null;
  49.     #[ORM\Column(type'string'length255nullabletrue)]
  50.     private ?string $plainPassword null;
  51.     #[ORM\OneToMany(targetEntityLog::class, mappedBy'user'orphanRemovaltrue)]
  52.     private Collection $logs;
  53.     #[ORM\Column(type'string'length255nullabletrue)]
  54.     private ?string $company null;
  55.     #[ORM\Column(type'date'nullabletrue)]
  56.     private ?\DateTimeInterface $birthday null;
  57.     #[ORM\Column(type'string'length255nullabletrue)]
  58.     private ?string $webPage null;
  59.     #[ORM\Column(type'text'nullabletrue)]
  60.     private ?string $notes null;
  61.     #[ORM\Column(type'string'length255nullabletrue)]
  62.     private ?string $jobTitle null;
  63.     #[ORM\Column(type'string'length255nullabletrue)]
  64.     private ?string $linkedIn null;
  65.     #[ORM\Column(type'string'length255nullabletrue)]
  66.     private ?string $businessPhone null;
  67.     #[ORM\Column(type'string'length255nullabletrue)]
  68.     private ?string $businessStreet null;
  69.     #[ORM\Column(type'string'length255nullabletrue)]
  70.     private ?string $businessCity null;
  71.     #[ORM\Column(type'string'length255nullabletrue)]
  72.     private ?string $businessPostalCode null;
  73.     #[ORM\Column(type'string'length255nullabletrue)]
  74.     private ?string $businessCountry null;
  75.     #[ORM\Column(type'string'length255nullabletrue)]
  76.     private ?string $homePhone null;
  77.     #[ORM\Column(type'string'length255nullabletrue)]
  78.     private ?string $homePhone2 null;
  79.     #[ORM\Column(type'string'length255nullabletrue)]
  80.     private ?string $homeStreet null;
  81.     #[ORM\Column(type'string'length255nullabletrue)]
  82.     private ?string $homeCity null;
  83.     #[ORM\Column(type'string'length255nullabletrue)]
  84.     private ?string $homePostalCode null;
  85.     #[ORM\Column(type'string'length255nullabletrue)]
  86.     private ?string $homeCountry null;
  87.     #[ORM\ManyToOne]
  88.     private ?Languages $defaultLanguage null;
  89.     #[ORM\Column(length255nullabletrue)]
  90.     private ?string $photo null;
  91.     /**
  92.      * Inverse side of the ManyToMany with PhotoLocations::enabledUsers
  93.      */
  94.     #[ORM\ManyToMany(targetEntityPhotoLocations::class, mappedBy'enabledUsers')]
  95.     private Collection $photoLocations;
  96.     #[ORM\Column(length255nullabletrue)]
  97.     private ?string $autoLoginURL null;
  98.     #[ORM\Column(type"boolean"nullabletrueoptions: ["default" => null])]
  99.     private ?bool $pauseForBookmark null;
  100.     public function __construct()
  101.     {
  102.         $this->roles = new ArrayCollection();
  103.         $this->logs = new ArrayCollection();
  104.         $this->photoLocations = new ArrayCollection();
  105.     }
  106.     // ---------- ID ----------
  107.     public function getId(): ?int
  108.     {
  109.         return $this->id;
  110.     }
  111.     // ---------- Identity / Auth ----------
  112.     public function getUserIdentifier(): string
  113.     {
  114.         return $this->email ?? '';
  115.     }
  116.     public function getEmail(): ?string
  117.     {
  118.         return $this->email ?? '';
  119.     }
  120.     public function setEmail(string $email): self
  121.     {
  122.         // normalize for consistent uniqueness
  123.         $this->email mb_strtolower(trim($email));
  124.         return $this;
  125.     }
  126.     public function getUsername(): string
  127.     {
  128.         return (string) $this->email;
  129.     }
  130.     public function getPassword(): string
  131.     {
  132.         return $this->password;
  133.     }
  134.     public function setPassword(string $password): self
  135.     {
  136.         $this->password $password;
  137.         return $this;
  138.     }
  139.     public function getSalt(): ?string
  140.     {
  141.         return null;
  142.     }
  143.     public function eraseCredentials(): void
  144.     {
  145.         // clear temp sensitive data if any
  146.     }
  147.     // ---------- Roles ----------
  148.     public function addRole(Role $role): self
  149.     {
  150.         if (!$this->roles->contains($role)) {
  151.             $this->roles->add($role);
  152.         }
  153.         return $this;
  154.     }
  155.     public function removeRole(Role $role): self
  156.     {
  157.         $this->roles->removeElement($role);
  158.         return $this;
  159.     }
  160.     public function getRoles(): array
  161.     {
  162.         $roles $this->roles->toArray();
  163.         $roleStrings array_map(fn($r) => $r->getCode(), $roles);
  164.         $roleStrings[] = 'ROLE_USER';
  165.         return array_unique($roleStrings);
  166.     }
  167.     public function setRoleEntities(iterable $roles): self
  168.     {
  169.         $rolesArray is_array($roles) ? $roles iterator_to_array($roles);
  170.         foreach ($this->roles as $existingRole) {
  171.             if (!in_array($existingRole$rolesArraytrue)) {
  172.                 $this->roles->removeElement($existingRole);
  173.             }
  174.         }
  175.         foreach ($rolesArray as $role) {
  176.             if (!$this->roles->contains($role)) {
  177.                 $this->roles->add($role);
  178.             }
  179.         }
  180.         return $this;
  181.     }
  182.     public function getRoleEntities(): Collection
  183.     {
  184.         return $this->roles;
  185.     }
  186.     // ---------- Profile fields ----------
  187.     public function getSalutation(): ?string { return $this->salutation; }
  188.     public function setSalutation(?string $salutation): self $this->salutation $salutation; return $this; }
  189.     public function getFirstName(): ?string { return $this->firstName; }
  190.     public function setFirstName(?string $firstName): self $this->firstName $firstName; return $this; }
  191.     public function getLastName(): ?string { return $this->lastName; }
  192.     public function setLastName(?string $lastName): self $this->lastName $lastName; return $this; }
  193.     public function getFullName(): ?string
  194.     {
  195.         return $this->fullName ?? ($this->getFirstName() . ' ' $this->getLastName());
  196.     }
  197.     public function setFullName(?string $fullName): self $this->fullName $fullName; return $this; }
  198.     public function getMobile(): ?string { return $this->mobile; }
  199.     public function setMobile(?string $mobile): self $this->mobile $mobile; return $this; }
  200.     public function getMobile2(): ?string { return $this->mobile2; }
  201.     public function setMobile2(?string $mobile2): self $this->mobile2 $mobile2; return $this; }
  202.     public function getPlainPassword(): ?string { return $this->plainPassword; }
  203.     public function setPlainPassword(?string $plainPassword): self $this->plainPassword $plainPassword; return $this; }
  204.     // put near your other accessors
  205.     public function getEmail2(): ?string
  206.     {
  207.         return $this->email2;
  208.     }
  209.     public function setEmail2(?string $email2): self
  210.     {
  211.         $this->email2 $email2;
  212.         return $this;
  213.     }
  214.     public function getEmail3(): ?string
  215.     {
  216.         return $this->email3;
  217.     }
  218.     public function setEmail3(?string $email3): self
  219.     {
  220.         $this->email3 $email3;
  221.         return $this;
  222.     }
  223.     // ---------- Logs ----------
  224.     public function getLogs(): Collection { return $this->logs; }
  225.     public function addLog(Log $log): self
  226.     {
  227.         if (!$this->logs->contains($log)) {
  228.             $this->logs->add($log);
  229.             $log->setUser($this);
  230.         }
  231.         return $this;
  232.     }
  233.     public function removeLog(Log $log): self
  234.     {
  235.         if ($this->logs->removeElement($log)) {
  236.             if ($log->getUser() === $this) {
  237.                 $log->setUser(null);
  238.             }
  239.         }
  240.         return $this;
  241.     }
  242.     // ---------- Misc profile ----------
  243.     public function getCompany(): ?string { return $this->company; }
  244.     public function setCompany(?string $company): self $this->company $company; return $this; }
  245.     public function getBirthday(): ?\DateTimeInterface { return $this->birthday; }
  246.     public function setBirthday(?\DateTimeInterface $birthday): self $this->birthday $birthday; return $this; }
  247.     public function getWebPage(): ?string { return $this->webPage; }
  248.     public function setWebPage(?string $webPage): self $this->webPage $webPage; return $this; }
  249.     public function getNotes(): ?string { return $this->notes; }
  250.     public function setNotes(?string $notes): self $this->notes $notes; return $this; }
  251.     public function getJobTitle(): ?string { return $this->jobTitle; }
  252.     public function setJobTitle(?string $jobTitle): self $this->jobTitle $jobTitle; return $this; }
  253.     public function getLinkedIn(): ?string { return $this->linkedIn; }
  254.     public function setLinkedIn(?string $linkedIn): self $this->linkedIn $linkedIn; return $this; }
  255.     public function getBusinessPhone(): ?string { return $this->businessPhone; }
  256.     public function setBusinessPhone(?string $businessPhone): self $this->businessPhone $businessPhone; return $this; }
  257.     public function getBusinessStreet(): ?string { return $this->businessStreet; }
  258.     public function setBusinessStreet(?string $businessStreet): self $this->businessStreet $businessStreet; return $this; }
  259.     public function getBusinessCity(): ?string { return $this->businessCity; }
  260.     public function setBusinessCity(?string $businessCity): self $this->businessCity $businessCity; return $this; }
  261.     public function getBusinessPostalCode(): ?string { return $this->businessPostalCode; }
  262.     public function setBusinessPostalCode(?string $businessPostalCode): self $this->businessPostalCode $businessPostalCode; return $this; }
  263.     public function getBusinessCountry(): ?string { return $this->businessCountry; }
  264.     public function setBusinessCountry(?string $businessCountry): self $this->businessCountry $businessCountry; return $this; }
  265.     public function getHomePhone(): ?string { return $this->homePhone; }
  266.     public function setHomePhone(?string $homePhone): self $this->homePhone $homePhone; return $this; }
  267.     public function getHomePhone2(): ?string { return $this->homePhone2; }
  268.     public function setHomePhone2(?string $homePhone2): self $this->homePhone2 $homePhone2; return $this; }
  269.     public function getHomeStreet(): ?string { return $this->homeStreet; }
  270.     public function setHomeStreet(?string $homeStreet): self $this->homeStreet $homeStreet; return $this; }
  271.     public function getHomeCity(): ?string { return $this->homeCity; }
  272.     public function setHomeCity(?string $homeCity): self $this->homeCity $homeCity; return $this; }
  273.     public function getHomePostalCode(): ?string { return $this->homePostalCode; }
  274.     public function setHomePostalCode(?string $homePostalCode): self $this->homePostalCode $homePostalCode; return $this; }
  275.     public function getHomeCountry(): ?string { return $this->homeCountry; }
  276.     public function setHomeCountry(?string $homeCountry): self $this->homeCountry $homeCountry; return $this; }
  277.     // ---------- Photo Locations (inverse side) ----------
  278.     /** @return Collection<int, PhotoLocations> */
  279.     public function getPhotoLocations(): Collection
  280.     {
  281.         return $this->photoLocations;
  282.     }
  283.     public function addPhotoLocation(PhotoLocations $photoLocation): self
  284.     {
  285.         if (!$this->photoLocations->contains($photoLocation)) {
  286.             $this->photoLocations->add($photoLocation);
  287.             // sync owning side
  288.             $photoLocation->addEnabledUser($this);
  289.         }
  290.         return $this;
  291.     }
  292.     public function removePhotoLocation(PhotoLocations $photoLocation): self
  293.     {
  294.         if ($this->photoLocations->removeElement($photoLocation)) {
  295.             // sync owning side
  296.             $photoLocation->removeEnabledUser($this);
  297.         }
  298.         return $this;
  299.     }
  300.     public function getEmailVerified(): ?bool { return $this->emailVerified; }
  301.     public function setEmailVerified(?bool $emailVerified): self $this->emailVerified $emailVerified; return $this; }
  302.     public function getDefaultLanguage(): ?Languages { return $this->defaultLanguage; }
  303.     public function setDefaultLanguage(?Languages $defaultLanguage): static { $this->defaultLanguage $defaultLanguage; return $this; }
  304.     public function getPhoto(): ?string { return $this->photo; }
  305.     public function setPhoto(?string $photo): static { $this->photo $photo; return $this; }
  306.     public static function getAvailableRoles(KernelInterface $kernel): array
  307.     {
  308.         $rolesFilePath $kernel->getProjectDir() . '/config/roles.yaml';
  309.         if (file_exists($rolesFilePath)) {
  310.             $rolesConfig Yaml::parseFile($rolesFilePath);
  311.             return $rolesConfig['roles'] ?? [];
  312.         }
  313.         return [];
  314.     }
  315.     public function hasRole(Role $role): bool
  316.     {
  317.         return $this->roles->contains($role);
  318.     }
  319.     public function getAutoLoginURL(): ?string { return $this->autoLoginURL; }
  320.     public function setAutoLoginURL(?string $autoLoginURL): static { $this->autoLoginURL $autoLoginURL; return $this; }
  321.     public function isPauseForBookmark(): bool { return (bool) $this->pauseForBookmark; }
  322.     public function setPauseForBookmark(bool $pause): self $this->pauseForBookmark $pause; return $this; }
  323. }