feat : first commit
This commit is contained in:
0
src/ApiResource/.gitignore
vendored
Normal file
0
src/ApiResource/.gitignore
vendored
Normal file
103
src/Command/SeedEmployeesCommand.php
Normal file
103
src/Command/SeedEmployeesCommand.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\Employee;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'app:seed:employees',
|
||||
description: 'Seed employees (idempotent).'
|
||||
)]
|
||||
class SeedEmployeesCommand extends Command
|
||||
{
|
||||
private int $created = 0;
|
||||
private int $updated = 0;
|
||||
|
||||
public function __construct(private readonly EntityManagerInterface $entityManager)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$this->created = 0;
|
||||
$this->updated = 0;
|
||||
|
||||
$employees = [
|
||||
['lastName' => 'LIOT', 'firstName' => 'Geoffrey'],
|
||||
['lastName' => 'ARCHAMBAULT', 'firstName' => 'Emilie'],
|
||||
['lastName' => 'SARAZI', 'firstName' => 'Olivier'],
|
||||
['lastName' => 'SOURIAU', 'firstName' => 'Elodie'],
|
||||
['lastName' => 'BRUYERE', 'firstName' => 'Aurore'],
|
||||
['lastName' => 'PANGALLO', 'firstName' => 'Julie'],
|
||||
['lastName' => 'BACHELIER', 'firstName' => 'Delphine'],
|
||||
['lastName' => 'SAILLY', 'firstName' => 'Marine'],
|
||||
['lastName' => 'KUPCZYK', 'firstName' => 'Corinne'],
|
||||
['lastName' => 'ODUNCU', 'firstName' => 'Kemal'],
|
||||
['lastName' => 'PROUTEAU', 'firstName' => 'Damien'],
|
||||
['lastName' => 'COURLIVANT', 'firstName' => 'Franck'],
|
||||
['lastName' => 'GUILLOT', 'firstName' => 'Damien'],
|
||||
['lastName' => 'GELINET', 'firstName' => 'Alexandre'],
|
||||
['lastName' => 'ASSANI', 'firstName' => 'Chafainna'],
|
||||
['lastName' => 'BRIQUET', 'firstName' => 'Alain'],
|
||||
['lastName' => 'BOULOIZEAU', 'firstName' => 'Gwenael'],
|
||||
['lastName' => 'MENANTEAU', 'firstName' => 'Dimitri'],
|
||||
['lastName' => 'ROBERT', 'firstName' => 'Remi'],
|
||||
['lastName' => 'COUTEAU', 'firstName' => 'Johnny'],
|
||||
['lastName' => 'MAISSANT', 'firstName' => 'Guillaume'],
|
||||
['lastName' => 'GARRAUD', 'firstName' => 'Nadia'],
|
||||
['lastName' => 'CRUBILE', 'firstName' => 'Franck'],
|
||||
['lastName' => 'ALMAGRO', 'firstName' => 'Antonio'],
|
||||
['lastName' => 'MARCHAL SANCHEZ', 'firstName' => 'Jose'],
|
||||
['lastName' => 'LOISEAU', 'firstName' => 'Nicolas'],
|
||||
['lastName' => 'BRAAK', 'firstName' => 'Estelle'],
|
||||
['lastName' => 'RODRIGUES MENDES', 'firstName' => 'Hugo'],
|
||||
['lastName' => 'RICAUD', 'firstName' => 'Jason'],
|
||||
['lastName' => 'LECOINT', 'firstName' => 'Jerome'],
|
||||
['lastName' => 'DALEMBA', 'firstName' => 'Ewa'],
|
||||
];
|
||||
|
||||
foreach ($employees as $data) {
|
||||
$this->upsertEmployee($data['firstName'], $data['lastName']);
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$io->success(sprintf('Seed completed: %d created, %d updated.', $this->created, $this->updated));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function upsertEmployee(string $firstName, string $lastName): void
|
||||
{
|
||||
$repo = $this->entityManager->getRepository(Employee::class);
|
||||
$employee = $repo->findOneBy([
|
||||
'firstName' => $firstName,
|
||||
'lastName' => $lastName,
|
||||
]);
|
||||
|
||||
if (!$employee) {
|
||||
$employee = new Employee();
|
||||
++$this->created;
|
||||
} else {
|
||||
++$this->updated;
|
||||
}
|
||||
|
||||
$employee
|
||||
->setFirstName($firstName)
|
||||
->setLastName($lastName)
|
||||
;
|
||||
|
||||
$this->entityManager->persist($employee);
|
||||
}
|
||||
}
|
||||
0
src/Entity/.gitignore
vendored
Normal file
0
src/Entity/.gitignore
vendored
Normal file
120
src/Entity/Absence.php
Normal file
120
src/Entity/Absence.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiProperty;
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
normalizationContext: [
|
||||
'groups' => ['absence:read', 'employee:read', 'absence_type:read'],
|
||||
'datetime_format' => 'Y-m-d',
|
||||
],
|
||||
denormalizationContext: [
|
||||
'datetime_format' => 'Y-m-d',
|
||||
]
|
||||
)]
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'absences')]
|
||||
class Absence
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Groups(['absence:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ApiProperty(readableLink: true)]
|
||||
#[ORM\ManyToOne(targetEntity: Employee::class)]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Groups(['absence:read'])]
|
||||
private ?Employee $employee = null;
|
||||
|
||||
#[ApiProperty(readableLink: true)]
|
||||
#[ORM\ManyToOne(targetEntity: AbsenceType::class)]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Groups(['absence:read'])]
|
||||
private ?AbsenceType $type = null;
|
||||
|
||||
#[ORM\Column(type: 'date')]
|
||||
#[Groups(['absence:read'])]
|
||||
private DateTimeInterface $startDate;
|
||||
|
||||
#[ORM\Column(type: 'date')]
|
||||
#[Groups(['absence:read'])]
|
||||
private DateTimeInterface $endDate;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
#[Groups(['absence:read'])]
|
||||
private ?string $comment = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getEmployee(): ?Employee
|
||||
{
|
||||
return $this->employee;
|
||||
}
|
||||
|
||||
public function setEmployee(?Employee $employee): self
|
||||
{
|
||||
$this->employee = $employee;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getType(): ?AbsenceType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function setType(?AbsenceType $type): self
|
||||
{
|
||||
$this->type = $type;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStartDate(): DateTimeInterface
|
||||
{
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function setStartDate(DateTimeInterface $startDate): self
|
||||
{
|
||||
$this->startDate = $startDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEndDate(): DateTimeInterface
|
||||
{
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function setEndDate(DateTimeInterface $endDate): self
|
||||
{
|
||||
$this->endDate = $endDate;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getComment(): ?string
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
public function setComment(?string $comment): self
|
||||
{
|
||||
$this->comment = $comment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
74
src/Entity/AbsenceType.php
Normal file
74
src/Entity/AbsenceType.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(normalizationContext: ['groups' => ['absence_type:read']])]
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'absence_types')]
|
||||
class AbsenceType
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Groups(['absence:read', 'absence_type:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 10)]
|
||||
#[Groups(['absence:read', 'absence_type:read'])]
|
||||
private string $code = '';
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
#[Groups(['absence:read', 'absence_type:read'])]
|
||||
private string $label = '';
|
||||
|
||||
#[ORM\Column(type: 'string', length: 20)]
|
||||
#[Groups(['absence:read', 'absence_type:read'])]
|
||||
private string $color = '';
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getCode(): string
|
||||
{
|
||||
return $this->code;
|
||||
}
|
||||
|
||||
public function setCode(string $code): self
|
||||
{
|
||||
$this->code = $code;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
public function setLabel(string $label): self
|
||||
{
|
||||
$this->label = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getColor(): string
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
|
||||
public function setColor(string $color): self
|
||||
{
|
||||
$this->color = $color;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
72
src/Entity/Employee.php
Normal file
72
src/Entity/Employee.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(normalizationContext: ['groups' => ['employee:read']])]
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'employees')]
|
||||
class Employee
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Groups(['absence:read', 'employee:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
#[Groups(['absence:read', 'employee:read'])]
|
||||
private string $firstName = '';
|
||||
|
||||
#[ORM\Column(type: 'string', length: 100)]
|
||||
#[Groups(['absence:read', 'employee:read'])]
|
||||
private string $lastName = '';
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private DateTimeImmutable $createdAt;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new DateTimeImmutable();
|
||||
}
|
||||
|
||||
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 getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
}
|
||||
99
src/Entity/User.php
Normal file
99
src/Entity/User.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\State\CurrentUserProvider;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new Get(
|
||||
uriTemplate: '/me',
|
||||
normalizationContext: ['groups' => ['user:read']],
|
||||
security: "is_granted('ROLE_USER')",
|
||||
provider: CurrentUserProvider::class
|
||||
),
|
||||
]
|
||||
)]
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'users')]
|
||||
#[ORM\UniqueConstraint(name: 'uniq_users_username', fields: ['username'])]
|
||||
class User implements UserInterface, PasswordAuthenticatedUserInterface
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 180)]
|
||||
private string $username = '';
|
||||
|
||||
#[ORM\Column(type: 'json')]
|
||||
private array $roles = [];
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $password = '';
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername(string $username): self
|
||||
{
|
||||
$this->username = $username;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUserIdentifier(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public function getRoles(): array
|
||||
{
|
||||
$roles = $this->roles;
|
||||
$roles[] = 'ROLE_USER';
|
||||
|
||||
return array_values(array_unique($roles));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
public function setRoles(array $roles): self
|
||||
{
|
||||
$this->roles = $roles;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function setPassword(string $password): self
|
||||
{
|
||||
$this->password = $password;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function eraseCredentials(): void {}
|
||||
}
|
||||
13
src/Kernel.php
Normal file
13
src/Kernel.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
|
||||
|
||||
class Kernel extends BaseKernel
|
||||
{
|
||||
use MicroKernelTrait;
|
||||
}
|
||||
22
src/State/CurrentUserProvider.php
Normal file
22
src/State/CurrentUserProvider.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Entity\User;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
|
||||
final class CurrentUserProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(private readonly Security $security) {}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?User
|
||||
{
|
||||
$user = $this->security->getUser();
|
||||
|
||||
return $user instanceof User ? $user : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user