]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/UserRestController.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / UserRestController.php
index 1fc67d009fe96b92392a893b054f0d28c73605e5..6f47cff0dd85a8ddb759ef906c27f146ceffc01a 100644 (file)
@@ -6,8 +6,9 @@ use FOS\UserBundle\Event\UserEvent;
 use FOS\UserBundle\FOSUserEvents;
 use JMS\Serializer\SerializationContext;
 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
-use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Request;
+use Wallabag\ApiBundle\Entity\Client;
 use Wallabag\UserBundle\Entity\User;
 
 class UserRestController extends WallabagRestController
@@ -27,13 +28,14 @@ class UserRestController extends WallabagRestController
     }
 
     /**
-     * Register an user.
+     * Register an user and create a client.
      *
      * @ApiDoc(
      *      requirements={
      *          {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
      *          {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"},
-     *          {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"}
+     *          {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"},
+     *          {"name"="client_name", "dataType"="string", "required"=true, "description"="The client name (to be used by your app)"}
      *      }
      * )
      *
@@ -46,12 +48,14 @@ class UserRestController extends WallabagRestController
         if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) {
             $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
 
-            return (new JsonResponse())->setJson($json)->setStatusCode(403);
+            return (new JsonResponse())
+                ->setJson($json)
+                ->setStatusCode(JsonResponse::HTTP_FORBIDDEN);
         }
 
         $userManager = $this->get('fos_user.user_manager');
         $user = $userManager->createUser();
-        // user will be disabled BY DEFAULT to avoid spamming account to be created
+        // user will be disabled BY DEFAULT to avoid spamming account to be enabled
         $user->setEnabled(false);
 
         $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
@@ -90,34 +94,48 @@ class UserRestController extends WallabagRestController
 
             $json = $this->get('serializer')->serialize(['error' => $errors], 'json');
 
-            return (new JsonResponse())->setJson($json)->setStatusCode(400);
+            return (new JsonResponse())
+                ->setJson($json)
+                ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
         }
 
+        // create a default client
+        $client = new Client($user);
+        $client->setName($request->request->get('client_name', '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);
+        return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED);
     }
 
     /**
      * Send user response.
      *
-     * @param User $user
+     * @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)
+    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())->setJson($json);
+        return (new JsonResponse())
+            ->setJson($json)
+            ->setStatusCode($status);
     }
 
     /**