Skip to content

Commit 953c350

Browse files
committed
Add: get auth user endpoint
1 parent 9907f4e commit 953c350

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/Identity/Controller/SessionController.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpList\RestBundle\Common\Controller\BaseController;
1313
use PhpList\RestBundle\Common\Validator\RequestValidator;
1414
use PhpList\RestBundle\Identity\Request\CreateSessionRequest;
15+
use PhpList\RestBundle\Identity\Serializer\AdministratorNormalizer;
1516
use PhpList\RestBundle\Identity\Serializer\AdministratorTokenNormalizer;
1617
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1718
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -36,6 +37,7 @@ public function __construct(
3637
RequestValidator $validator,
3738
SessionManager $sessionManager,
3839
private readonly EntityManagerInterface $entityManager,
40+
private readonly AdministratorNormalizer $normalizer,
3941
) {
4042
parent::__construct($authentication, $validator);
4143

@@ -170,4 +172,46 @@ public function deleteSession(
170172

171173
return $this->json(null, Response::HTTP_NO_CONTENT);
172174
}
175+
176+
#[Route('/me', name: 'me', methods: ['GET'])]
177+
#[OA\Get(
178+
path: '/api/v2/sessions/me',
179+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
180+
'Get auth user data.',
181+
summary: 'Get auth user data.',
182+
tags: ['sessions'],
183+
parameters: [
184+
new OA\Parameter(
185+
name: 'php-auth-pw',
186+
description: 'Session key obtained from login',
187+
in: 'header',
188+
required: true,
189+
schema: new OA\Schema(type: 'string')
190+
),
191+
],
192+
responses: [
193+
new OA\Response(
194+
response: 200,
195+
description: 'Administrator found',
196+
content: new OA\JsonContent(ref: '#/components/schemas/Administrator')
197+
),
198+
new OA\Response(
199+
response: 401,
200+
description: 'Failure',
201+
content: new OA\JsonContent(
202+
properties: [
203+
new OA\Property(property: 'message', type: 'string', example: 'Not authorized.')
204+
]
205+
)
206+
)
207+
]
208+
)]
209+
public function getSessionUser(Request $request): JsonResponse
210+
{
211+
$administrator = $this->requireAuthentication($request);
212+
213+
$json = $this->normalizer->normalize($administrator, 'json');
214+
215+
return $this->json($json, Response::HTTP_OK);
216+
}
173217
}

tests/Integration/Identity/Controller/SessionControllerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,32 @@ public function testDeleteSessionWithNoSuchSessionReturns404(): void
227227
$this->authenticatedJsonRequest('DELETE', '/api/v2/sessions/999999');
228228
$this->assertHttpNotFound();
229229
}
230+
231+
public function testGetSessionUserWithoutAuthenticationReturnsForbiddenStatus(): void
232+
{
233+
self::getClient()->request('GET', '/api/v2/sessions/me');
234+
235+
$this->assertHttpForbidden();
236+
}
237+
238+
public function testGetSessionUserWithAuthenticationReturnsOkayStatus(): void
239+
{
240+
$this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class]);
241+
$this->authenticatedJsonRequest('GET', '/api/v2/sessions/me');
242+
243+
$this->assertHttpOkay();
244+
}
245+
246+
public function testGetSessionUserWithAuthenticationReturnsAdministratorData(): void
247+
{
248+
$this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class]);
249+
$this->authenticatedJsonRequest('GET', '/api/v2/sessions/me');
250+
251+
$data = $this->getDecodedJsonResponseContent();
252+
253+
self::assertSame(1, $data['id']);
254+
self::assertSame('john.doe', $data['login_name']);
255+
self::assertSame('john@example.com', $data['email']);
256+
self::assertTrue($data['super_user']);
257+
}
230258
}

0 commit comments

Comments
 (0)