From 0c00e5251671c3648eabb8888271c09137ad902d Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 7 Jun 2017 23:23:28 +0200 Subject: Create a client when creating a user using the api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While creating a new user using the API, we also create a new client for the current user. So the app which just create the user can use its newly created client to configure the app. That new client is only return after creating the user. When calling the endpoint /api/user to get user information, the new client information won’t be return. --- .../ApiBundle/Controller/UserRestController.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller') diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php index 8f675b8d..becbbb9e 100644 --- a/src/Wallabag/ApiBundle/Controller/UserRestController.php +++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php @@ -9,6 +9,7 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Wallabag\UserBundle\Entity\User; +use Wallabag\ApiBundle\Entity\Client; class UserRestController extends WallabagRestController { @@ -97,29 +98,38 @@ class UserRestController extends WallabagRestController ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); } + // create a default client + $client = new Client($user); + $client->setName('Default client'); + + $this->getDoctrine()->getManager()->persist($client); + + $user->addClient($client); + $userManager->updateUser($user); // dispatch a created event so the associated config will be created $event = new UserEvent($user, $request); $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); - return $this->sendUser($user, JsonResponse::HTTP_CREATED); + return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED); } /** * Send user response. * - * @param User $user - * @param int $status HTTP Status code to send + * @param User $user + * @param string $group Used to define with serialized group might be used + * @param int $status HTTP Status code to send * * @return JsonResponse */ - private function sendUser(User $user, $status = JsonResponse::HTTP_OK) + private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK) { $json = $this->get('serializer')->serialize( $user, 'json', - SerializationContext::create()->setGroups(['user_api']) + SerializationContext::create()->setGroups([$group]) ); return (new JsonResponse()) -- cgit v1.2.3