aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/UserRestController.php
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2017-06-08 17:24:49 +0200
committerGitHub <noreply@github.com>2017-06-08 17:24:49 +0200
commit3f474025d889c3eff20b481f005f4d292f1ef29d (patch)
treee74288b7df91e226b1d23a4047a3af5cc04a6484 /src/Wallabag/ApiBundle/Controller/UserRestController.php
parent2da8f071cfcc6e50be7be66c037de23f0d073bea (diff)
parenta8d3fe50df52ec486add5691a3b67fe5205a032e (diff)
downloadwallabag-3f474025d889c3eff20b481f005f4d292f1ef29d.tar.gz
wallabag-3f474025d889c3eff20b481f005f4d292f1ef29d.tar.zst
wallabag-3f474025d889c3eff20b481f005f4d292f1ef29d.zip
Merge pull request #3187 from wallabag/api-client-credentials
Create (and return) a client after creating a new user using the API
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/UserRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/UserRestController.php25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php
index 8f675b8d..7471f5f6 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{
@@ -27,13 +28,14 @@ class UserRestController extends WallabagRestController
27 } 28 }
28 29
29 /** 30 /**
30 * Register an user. 31 * Register an user and create a client.
31 * 32 *
32 * @ApiDoc( 33 * @ApiDoc(
33 * requirements={ 34 * requirements={
34 * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"}, 35 * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
35 * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"}, 36 * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"},
36 * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"} 37 * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"},
38 * {"name"="client_name", "dataType"="string", "required"=true, "description"="The client name (to be used by your app)"}
37 * } 39 * }
38 * ) 40 * )
39 * 41 *
@@ -97,29 +99,38 @@ class UserRestController extends WallabagRestController
97 ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); 99 ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
98 } 100 }
99 101
102 // create a default client
103 $client = new Client($user);
104 $client->setName($request->request->get('client_name', 'Default client'));
105
106 $this->getDoctrine()->getManager()->persist($client);
107
108 $user->addClient($client);
109
100 $userManager->updateUser($user); 110 $userManager->updateUser($user);
101 111
102 // dispatch a created event so the associated config will be created 112 // dispatch a created event so the associated config will be created
103 $event = new UserEvent($user, $request); 113 $event = new UserEvent($user, $request);
104 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); 114 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
105 115
106 return $this->sendUser($user, JsonResponse::HTTP_CREATED); 116 return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED);
107 } 117 }
108 118
109 /** 119 /**
110 * Send user response. 120 * Send user response.
111 * 121 *
112 * @param User $user 122 * @param User $user
113 * @param int $status HTTP Status code to send 123 * @param string $group Used to define with serialized group might be used
124 * @param int $status HTTP Status code to send
114 * 125 *
115 * @return JsonResponse 126 * @return JsonResponse
116 */ 127 */
117 private function sendUser(User $user, $status = JsonResponse::HTTP_OK) 128 private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK)
118 { 129 {
119 $json = $this->get('serializer')->serialize( 130 $json = $this->get('serializer')->serialize(
120 $user, 131 $user,
121 'json', 132 'json',
122 SerializationContext::create()->setGroups(['user_api']) 133 SerializationContext::create()->setGroups([$group])
123 ); 134 );
124 135
125 return (new JsonResponse()) 136 return (new JsonResponse())