From 2efc3da018dc4e8c2e584688fdb03aac455d6d2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Thu, 18 Jul 2024 12:36:01 +0200 Subject: [PATCH] feat: add /me endpoint --- config/packages/api_platform.yaml | 18 ++++++++++++------ config/packages/security.yaml | 2 +- src/Controller/MeController.php | 23 +++++++++++++++++++++++ src/Entity/User.php | 18 ++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/Controller/MeController.php diff --git a/config/packages/api_platform.yaml b/config/packages/api_platform.yaml index dcec48b..d7e6ac1 100644 --- a/config/packages/api_platform.yaml +++ b/config/packages/api_platform.yaml @@ -2,19 +2,25 @@ api_platform: title: Domain Watchdog API version: 1.0.0 formats: - jsonld: ['application/ld+json'] - json: ['application/json'] + jsonld: [ 'application/ld+json' ] + json: [ 'application/json' ] docs_formats: - jsonld: ['application/ld+json'] - jsonopenapi: ['application/vnd.openapi+json'] - html: ['text/html'] + jsonld: [ 'application/ld+json' ] + jsonopenapi: [ 'application/vnd.openapi+json' ] + html: [ 'text/html' ] defaults: stateless: true cache_headers: - vary: ['Content-Type', 'Authorization', 'Origin'] + vary: [ 'Content-Type', 'Authorization', 'Origin' ] extra_properties: standard_put: true rfc_7807_compliant_errors: true keep_legacy_inflector: false use_symfony_listeners: true show_webby: false + swagger: + api_keys: + JWT: + name: Authorization + type: header + diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 5b2dca0..d96ec5a 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -17,7 +17,7 @@ security: pattern: ^/api/login stateless: true json_login: - check_path: /api/login + check_path: api_login success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure diff --git a/src/Controller/MeController.php b/src/Controller/MeController.php new file mode 100644 index 0000000..8aac5ea --- /dev/null +++ b/src/Controller/MeController.php @@ -0,0 +1,23 @@ +security->getUser(); + } + +} \ No newline at end of file diff --git a/src/Entity/User.php b/src/Entity/User.php index 9bac4a1..4ebadb3 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -2,6 +2,10 @@ namespace App\Entity; +use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use App\Controller\MeController; use App\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -9,10 +13,22 @@ 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\Serializer\Attribute\Groups; #[ORM\Entity(repositoryClass: UserRepository::class)] #[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])] #[UniqueEntity(fields: ['email'], message: 'There is already an account with this email')] +#[ApiResource( + operations: [ + new Get( + uriTemplate: '/me', + controller: MeController::class, + paginationEnabled: false, + normalizationContext: ["groups" => "user:list"], + read: false + ) + ] +)] class User implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] @@ -21,12 +37,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface private ?int $id = null; #[ORM\Column(length: 180)] + #[Groups(['user:list'])] private ?string $email = null; /** * @var array The user roles */ #[ORM\Column] + #[Groups(['user:list'])] private array $roles = []; /**