src/Entity/User.php line 71
<?php
namespace App\Entity;
use App\Repository\UserRepository;
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 ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Controller\Api\AdminController;
use App\Controller\Api\AuthController;
use App\Controller\Api\UserController;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')]
#[ApiResource(
normalizationContext: ['groups' => ['user:read']],
denormalizationContext: ['groups' => ['user:write']],
operations: [
new Get(
name: 'auth_user',
routeName: 'api_auth_user',
controller: AuthController::class
),
// new GetCollection(security: 'is_granted("ROLE_SUPER_ADMIN")'),
new GetCollection(
name: 'users',
routeName: 'api_users',
controller: UserController::class,
security: 'is_granted("ROLE_ADMIN")'
),
new GetCollection(
name: 'users',
routeName: 'api_users_employees',
controller: UserController::class,
security: 'is_granted("ROLE_ADMIN")'
),
new Delete(security: 'is_granted("ROLE_SUPER_ADMIN")'),
new Post(
name: 'user',
routeName: 'api_user',
controller: UserController::class
),
new Post(
name: 'reset_token',
routeName: 'api_user_reset_token',
controller: AuthController::class
),
new Post(
name: 'reset_password',
routeName: 'api_user_reset_password',
controller: AuthController::class
),
new Post(security: 'is_granted("ROLE_SUPER_ADMIN") or is_granted("ROLE_ADMIN")'),
new Put(
name: 'user_edit',
routeName: 'api_user_edit',
controller: UserController::class,
security: 'is_granted("ROLE_SUPER_ADMIN") or is_granted("ROLE_ADMIN")'
)
],
paginationEnabled: false
)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['user:read', 'invoice:read', 'invoiceDetail:read'])]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Groups(['user:read', 'user:write', 'invoice:read', 'invoiceDetail:read'])]
private ?string $email = null;
#[ORM\Column]
#[Groups(['user:read', 'user:write', 'invoice:read', 'invoiceDetail:read'])]
private array $roles = [];
#[ORM\Column(type: 'integer')]
#[Groups(['user:read', 'user:write', 'invoice:read', 'invoiceDetail:read'])]
private ?int $status = 1;
#[ORM\Column(type: 'text', length: 65535, nullable: true)]
private ?string $reset_password_token = null;
#[ORM\OneToOne(targetEntity: Employee::class, inversedBy: 'user')]
#[ORM\JoinColumn(name: 'employee_id', referencedColumnName: 'id', nullable: true)]
#[Groups(['user:read', 'user:write'])]
private ?Employee $employee = null;
/**
* @var string The hashed password
*/
#[ORM\Column]
#[Groups(['user:write'])]
private ?string $password = null;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getResetPasswordToken(): ?string
{
return $this->reset_password_token;
}
public function setResetPasswordToken(string $reset_password_token): self
{
$this->reset_password_token = $reset_password_token;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): 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): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setHashedPassword(string $plainPassword): self
{
$this->password = password_hash($plainPassword, PASSWORD_BCRYPT);
return $this;
}
public function setPassword(string $plainPassword): self
{
if (!password_get_info($plainPassword)['algo']) {
$this->setHashedPassword($plainPassword);
}
return $this;
}
public function getEmployee(): ?Employee
{
return $this->employee;
}
public function setEmployee(?Employee $employee): self
{
if ($employee === null && $this->employee !== null) {
$this->employee->setUser(null);
}
if ($employee !== null && $employee->getUser() !== $this) {
$employee->setUser($this);
}
$this->employee = $employee;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
}