<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\AdminRepository;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Controller\ControllerAdminChangePassword;
use App\Controller\ControllerResetPasswordAdmin;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: AdminRepository::class)]
#[UniqueEntity(fields:'phone', message:'Le phone existe déjà')]
#[ApiResource(
normalizationContext:['groups'=>['admin:read']],
denormalizationContext:['groups'=>['admin:write']],
collectionOperations: [
"get" ,
"post",
'changePassword' => [
'method' => 'GET',
'path' => "admins/change-password/{userId}/{lastpassword}/{newpassword}",
'controller' => ControllerAdminChangePassword::class,
'read' => false,
],
'resetPassword' => [
'method' => 'GET',
'path' => '/reset-password/{phone}',
'controller' => ControllerResetPasswordAdmin::class,
'read' => false,
],
],
itemOperations: [
"get" ,
"put" ,
"delete"
]
)]
class Admin implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['admin:read','admin:write'])]
private $id;
#[ORM\Column(type: 'string', length: 70)]
#[Groups(['admin:read','admin:write'])]
private $firstName;
#[ORM\Column(type: 'string', length: 180)]
#[Groups(['admin:read','admin:write'])]
private $lastName;
#[ORM\Column(type: 'string', length: 80, nullable: true)]
#[Groups(['admin:read','admin:write'])]
private $email;
#[ORM\Column(type: 'string', length: 15)]
#[Groups(['admin:read','admin:write'])]
private $phone;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['admin:read','admin:write'])]
private $password = '1234';
#[ORM\ManyToMany(targetEntity: Role::class)]
#[Groups(['admin:read','admin:write'])]
private $role;
public function __construct()
{
$this->role = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
}
/**
* Returns the identifier for this user (e.g. its username or email address).
*/
public function getUserIdentifier(): string
{
return $this->phone;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @return array
*/
public function getRoles(): array
{
return ["ROLE_ADMIN"];
}
/**
* @return Collection<int, self>
*/
public function getRole(): Collection
{
return $this->role;
}
public function addRole(Role $role): self
{
if (!$this->role->contains($role)) {
$this->role[] = $role;
}
return $this;
}
public function removeRole(Role $role): self
{
$this->role->removeElement($role);
return $this;
}
}