Commit 5091e7d9 authored by Dominique Merle's avatar Dominique Merle 💬
Browse files

Add registration to the documentation, and fix its signature.

parent 4e79e530
......@@ -16,11 +16,11 @@ class ApiRestFeatureContext extends BaseFeatureContext
public function actorRegistersWithEmailAndPassword($actor, $try, $email, $password)
{
$this->actor($actor, true)->api(
'POST',"/users", null,
'POST',"/users",
[
'email' => $email,
'password' => ['plain' => $password], // weird, eh?
], !empty($try)
'password' => $password,
], [], !empty($try)
);
}
......
......@@ -15,6 +15,10 @@ use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
/**
* Register a new User.
* This documentation never appears anywhere, does it?
* See App\Entity\UserDocumentation
*
* @Route("/api/users", name="api_register", methods={"POST"})
*/
final class RegistrationController
......@@ -26,22 +30,21 @@ final class RegistrationController
EntityManagerInterface $em
): Response {
$data = [];
// if (\is_string($token = $request->query->get('token')) && null !== $invitation = $em->find(UserInvitation::class, $token)) {
// $data['email'] = $invitation->getEmail();
// }
$this->adaptRequest($request);
$form = $formFactory->createNamed('', RegistrationType::class, $data, ['csrf_protection' => false]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// $data['invitation_token'] = $token;
$bus->dispatch(new CreateUser($data));
return new JsonResponse([
'message' => "api.registration.success",
'data' => $data, // maybe instead output the user, with id?
], Response::HTTP_OK);
], JsonResponse::HTTP_CREATED);
// also provide ['Location' => $locationUrl]
}
$error = "api.registration.failure.message";
......@@ -56,4 +59,32 @@ final class RegistrationController
return new JsonResponse(['error' => $error], Response::HTTP_BAD_REQUEST);
}
/**
* Since ApiPlatform generated doc recommends usage of the Content instead of POST vars,
* let's convert what's in the content to POST vars before feeding it to the form.
* Whe also handle here the password['plain'] array quirk.
*
* This mutates the $request.
*
* @param Request $request
*/
protected function adaptRequest(Request $request)
{
$content = $request->getContent();
if ( ! empty($content)) {
try {
$content = json_decode($content, true, 3, JSON_BIGINT_AS_STRING&JSON_OBJECT_AS_ARRAY);
}
catch (\JsonException $e) { return; }
catch (\Exception $e) { return; }
if (isset($content["email"])) {
$request->request->set('email', $content["email"]);
}
if (isset($content["password"])) {
$request->request->set('password', ['plain' => $content["password"]]);
}
}
}
}
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
// This class exists as a (hacky) way to provide documentation.
/**
* You.
*
* @ApiResource(
* shortName="User",
* order=10,
* itemOperations={},
* collectionOperations={
* "post"={
* "controller"="App\Controller\RegistrationController",
* },
* },
* )
*/
class UserDocumentation
{
/**
* @var string Primary e-mail address
* @Groups({"user:read", "user:write"})
*/
public $email;
/**
* @var string|null Plain password (Required in "write")
* @Groups({"user:write"})
*/
public $password;
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment