aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/UserRestController.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2017-06-07 23:23:28 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2017-06-07 23:23:28 +0200
commit0c00e5251671c3648eabb8888271c09137ad902d (patch)
tree2fe5de8701fa80ea9481e4098203b95d787ce576 /src/Wallabag/ApiBundle/Controller/UserRestController.php
parent7bb3aa31776ffce2735a3b16f6ad80bb17946d4d (diff)
downloadwallabag-0c00e5251671c3648eabb8888271c09137ad902d.tar.gz
wallabag-0c00e5251671c3648eabb8888271c09137ad902d.tar.zst
wallabag-0c00e5251671c3648eabb8888271c09137ad902d.zip
Create a client when creating a user using the api
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.
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/UserRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/UserRestController.php20
1 files changed, 15 insertions, 5 deletions
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;
9use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\JsonResponse; 10use Symfony\Component\HttpFoundation\JsonResponse;
11use Wallabag\UserBundle\Entity\User; 11use Wallabag\UserBundle\Entity\User;
12use Wallabag\ApiBundle\Entity\Client;
12 13
13class UserRestController extends WallabagRestController 14class UserRestController extends WallabagRestController
14{ 15{
@@ -97,29 +98,38 @@ class UserRestController extends WallabagRestController
97 ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); 98 ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
98 } 99 }
99 100
101 // create a default client
102 $client = new Client($user);
103 $client->setName('Default client');
104
105 $this->getDoctrine()->getManager()->persist($client);
106
107 $user->addClient($client);
108
100 $userManager->updateUser($user); 109 $userManager->updateUser($user);
101 110
102 // dispatch a created event so the associated config will be created 111 // dispatch a created event so the associated config will be created
103 $event = new UserEvent($user, $request); 112 $event = new UserEvent($user, $request);
104 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); 113 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
105 114
106 return $this->sendUser($user, JsonResponse::HTTP_CREATED); 115 return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED);
107 } 116 }
108 117
109 /** 118 /**
110 * Send user response. 119 * Send user response.
111 * 120 *
112 * @param User $user 121 * @param User $user
113 * @param int $status HTTP Status code to send 122 * @param string $group Used to define with serialized group might be used
123 * @param int $status HTTP Status code to send
114 * 124 *
115 * @return JsonResponse 125 * @return JsonResponse
116 */ 126 */
117 private function sendUser(User $user, $status = JsonResponse::HTTP_OK) 127 private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK)
118 { 128 {
119 $json = $this->get('serializer')->serialize( 129 $json = $this->get('serializer')->serialize(
120 $user, 130 $user,
121 'json', 131 'json',
122 SerializationContext::create()->setGroups(['user_api']) 132 SerializationContext::create()->setGroups([$group])
123 ); 133 );
124 134
125 return (new JsonResponse()) 135 return (new JsonResponse())