<?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\Table(name: '`user`')]
#[UniqueEntity(fields: ['email'], message: "Cet email est déjà utilisé")]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Assert\Email]
#[Assert\Length(max: 180)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAccess::class, orphanRemoval: true)]
private Collection $accesses;
public function __construct()
{
$this->accesses = new ArrayCollection();
}
/**
* @param object $entity Resource to access
* @param string[] $requiredRoles Should have at least one of these roles. Ex: "role1" OR "role2"
*
* @return bool
*/
public function canAccessResource(object $entity, array $requiredRoles): bool
{
$fn = function(UserAccess $access) use ($entity, $requiredRoles) {
if (!in_array($access->getRole(), $requiredRoles)) {
return false;
}
return ($access->getType() === UserAccess::TYPE_MAGASIN && $access->getMagasin() === $entity)
|| ($access->getType() === UserAccess::TYPE_GROUPEMENT && $access->getGroupement() === $entity)
;
};
return $this->getAccesses()
->filter($fn)
->count() > 0;
}
public function getDefaultGroupement(): ?Groupement
{
foreach ($this->getAccesses() as $access) {
if ($access->getType() === UserAccess::TYPE_GROUPEMENT && $access->getRole() === UserAccess::ROLE_ADMIN) {
return $access->getGroupement();
}
}
return null;
}
public function getDefaultProject(): ?Project
{
foreach ($this->getAccesses() as $access) {
if ($access->getType() === UserAccess::TYPE_MAGASIN && $access->getRole() === UserAccess::ROLE_ADMIN) {
return $access->getMagasin()?->getProject();
}
}
return null;
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): static
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->email;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($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;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return Collection<int, UserAccess>
*/
public function getAccesses(): Collection
{
return $this->accesses;
}
public function addAccess(UserAccess $access): static
{
if (!$this->accesses->contains($access)) {
$this->accesses->add($access);
$access->setUser($this);
}
return $this;
}
public function removeAccess(UserAccess $access): static
{
if ($this->accesses->removeElement($access)) {
// set the owning side to null (unless already changed)
if ($access->getUser() === $this) {
$access->setUser(null);
}
}
return $this;
}
}