177 lines
4.1 KiB
PHP
177 lines
4.1 KiB
PHP
<?php
|
||
|
||
namespace App\Entity;
|
||
|
||
use App\Repository\UserRepository;
|
||
use Doctrine\Common\Collections\ArrayCollection;
|
||
use Doctrine\Common\Collections\Collection;
|
||
use Doctrine\ORM\Mapping as ORM;
|
||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||
use Symfony\Component\Security\Core\User\UserInterface;
|
||
use Symfony\Component\Validator\Constraints as Assert;
|
||
|
||
#[ORM\Entity(repositoryClass: UserRepository::class)]
|
||
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
|
||
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
|
||
#[UniqueEntity(fields: ['email'], message: 'Cet email est déjà utilisé.')]
|
||
#[UniqueEntity(fields: ['username'], message: 'Ce nom d’utilisateur est déjà pris.')]
|
||
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||
{
|
||
#[ORM\Id]
|
||
#[ORM\GeneratedValue]
|
||
#[ORM\Column]
|
||
private ?int $id = null;
|
||
|
||
#[ORM\Column(length: 180, unique: true)]
|
||
private ?string $username = null;
|
||
|
||
#[ORM\Column]
|
||
private array $roles = [];
|
||
|
||
#[ORM\Column]
|
||
#[Assert\Regex(
|
||
pattern: '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}$/',
|
||
message: 'Le mot de passe doit contenir au moins 8 caractères, une lettre majuscule, une lettre minuscule et un caractère spécial.'
|
||
)]
|
||
private ?string $password = null;
|
||
|
||
#[ORM\Column(length: 255, nullable: true)]
|
||
private ?string $avatar = null;
|
||
|
||
#[ORM\Column(length: 255, unique: true)]
|
||
#[Assert\Email(message: 'Veuillez entrer un email valide.')]
|
||
private ?string $email = null;
|
||
|
||
#[ORM\ManyToMany(targetEntity: Company::class, inversedBy: 'users')]
|
||
private Collection $companys;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->companys = new ArrayCollection();
|
||
}
|
||
|
||
public function getId(): ?int
|
||
{
|
||
return $this->id;
|
||
}
|
||
|
||
public function getUsername(): ?string
|
||
{
|
||
return $this->username;
|
||
}
|
||
|
||
public function setUsername(string $username): static
|
||
{
|
||
$this->username = $username;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* A visual identifier that represents this user.
|
||
*
|
||
* @see UserInterface
|
||
*/
|
||
public function getUserIdentifier(): string
|
||
{
|
||
return (string) $this->username;
|
||
}
|
||
|
||
/**
|
||
* @see UserInterface
|
||
*
|
||
* @return list<string>
|
||
*/
|
||
public function getRoles(): array
|
||
{
|
||
$roles = $this->roles;
|
||
// guarantee every user at least has ROLE_USER
|
||
$roles[] = 'ROLE_USER';
|
||
|
||
return array_unique($roles);
|
||
}
|
||
|
||
/**
|
||
* @param list<string> $roles
|
||
*/
|
||
public function setRoles(array $roles): static
|
||
{
|
||
$this->roles = $roles;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* @see PasswordAuthenticatedUserInterface
|
||
*/
|
||
public function getPassword(): ?string
|
||
{
|
||
return $this->password;
|
||
}
|
||
|
||
public function setPassword(?string $password): static
|
||
{
|
||
$this->password = $password;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* @see UserInterface
|
||
*/
|
||
public function eraseCredentials(): void
|
||
{
|
||
// If you store any temporary, sensitive data on the user, clear it here
|
||
// $this->plainPassword = null;
|
||
}
|
||
|
||
public function getAvatar(): ?string
|
||
{
|
||
return $this->avatar ? $this->avatar : 'medias/avatar/noavatar.png';
|
||
}
|
||
|
||
public function setAvatar(?string $avatar): static
|
||
{
|
||
$this->avatar = $avatar;
|
||
|
||
return $this;
|
||
}
|
||
|
||
public function getEmail(): ?string
|
||
{
|
||
return $this->email;
|
||
}
|
||
|
||
public function setEmail(string $email): static
|
||
{
|
||
$this->email = $email;
|
||
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* @return Collection<int, Company>
|
||
*/
|
||
public function getCompanys(): Collection
|
||
{
|
||
return $this->companys;
|
||
}
|
||
|
||
public function addCompany(Company $company): static
|
||
{
|
||
if (!$this->companys->contains($company)) {
|
||
$this->companys->add($company);
|
||
}
|
||
|
||
return $this;
|
||
}
|
||
|
||
public function removeCompany(Company $company): static
|
||
{
|
||
$this->companys->removeElement($company);
|
||
|
||
return $this;
|
||
}
|
||
}
|