]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
WIP
authorThomas Citharel <tcit@tcit.fr>
Sat, 29 Apr 2017 17:22:50 +0000 (19:22 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Mon, 29 May 2017 08:16:23 +0000 (10:16 +0200)
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
src/Wallabag/ApiBundle/Controller/UserRestController.php [new file with mode: 0644]
src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
src/Wallabag/UserBundle/Entity/User.php

diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php
new file mode 100644 (file)
index 0000000..c5ffbdf
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+
+namespace Wallabag\ApiBundle\Controller;
+
+use FOS\UserBundle\Event\UserEvent;
+use FOS\UserBundle\FOSUserEvents;
+use JMS\Serializer\SerializationContext;
+use Nelmio\ApiDocBundle\Annotation\ApiDoc;
+use Symfony\Component\HttpFoundation\JsonResponse;
+
+class UserRestController extends WallabagRestController
+{
+    /**
+     * Retrieve user informations
+     *
+     * @ApiDoc()
+     *
+     * @return JsonResponse
+     */
+    public function getUserAction()
+    {
+        $this->validateAuthentication();
+
+        $serializationContext = SerializationContext::create()->setGroups(['user_api']);
+        $json = $this->get('serializer')->serialize($this->getUser(), 'json', $serializationContext);
+
+        return (new JsonResponse())->setJson($json);
+    }
+
+    /**
+     * Register an user
+     *
+     * @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"}
+     *      }
+     * )
+     * @return JsonResponse
+     */
+    // TODO : Make this method (or the whole API) accessible only through https
+    public function putUserAction($username, $password, $email)
+    {
+        if (!$this->container->getParameter('fosuser_registration')) {
+            $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
+            return (new JsonResponse())->setJson($json)->setStatusCode(403);
+        }
+
+        if ($password === '') { // TODO : might be a good idea to enforce restrictions here
+            $json = $this->get('serializer')->serialize(['error' => 'Password is blank'], 'json');
+            return (new JsonResponse())->setJson($json)->setStatusCode(400);
+        }
+
+
+        // TODO : Make only one call to database by using a custom repository method
+        if ($this->getDoctrine()
+            ->getRepository('WallabagUserBundle:User')
+            ->findOneByUserName($username)) {
+            $json = $this->get('serializer')->serialize(['error' => 'Username is already taken'], 'json');
+            return (new JsonResponse())->setJson($json)->setStatusCode(409);
+        }
+
+        if ($this->getDoctrine()
+            ->getRepository('WallabagUserBundle:User')
+            ->findOneByEmail($email)) {
+            $json = $this->get('serializer')->serialize(['error' => 'An account with this email already exists'], 'json');
+            return (new JsonResponse())->setJson($json)->setStatusCode(409);
+        }
+
+        $em = $this->get('doctrine.orm.entity_manager');
+
+        $userManager = $this->get('fos_user.user_manager');
+        $user = $userManager->createUser();
+
+        $user->setUsername($username);
+
+        $user->setPlainPassword($password);
+
+        $user->setEmail($email);
+
+        $user->setEnabled(true);
+        $user->addRole('ROLE_USER');
+
+        $em->persist($user);
+
+        // dispatch a created event so the associated config will be created
+        $event = new UserEvent($user);
+        $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
+
+        $serializationContext = SerializationContext::create()->setGroups(['user_api']);
+        $json = $this->get('serializer')->serialize($user, 'json', $serializationContext);
+
+        return (new JsonResponse())->setJson($json);
+
+    }
+
+}
index 57d37f4b454fa3aa15d3f4d6a36f194fd428d2ee..c0283e71f159603317b1179bbf194c21514c12fa 100644 (file)
@@ -17,3 +17,8 @@ misc:
   type: rest
   resource: "WallabagApiBundle:WallabagRest"
   name_prefix:  api_
+
+user:
+  type: rest
+  resource: "WallabagApiBundle:UserRest"
+  name_prefix:  api_
index 3a167de740608567ae03b6e42f88ab8d7d512bf6..1863c966ffe50d7253523b9ab5dc871754b31e2d 100644 (file)
@@ -4,6 +4,7 @@ namespace Wallabag\UserBundle\Entity;
 
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
+use JMS\Serializer\Annotation\Groups;
 use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
 use Scheb\TwoFactorBundle\Model\TrustedComputerInterface;
 use FOS\UserBundle\Model\User as BaseUser;
@@ -35,6 +36,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
+     * @Groups({"user_api"})
      */
     protected $id;
 
@@ -42,6 +44,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      * @var string
      *
      * @ORM\Column(name="name", type="text", nullable=true)
+     * @Groups({"user_api"})
      */
     protected $name;
 
@@ -49,6 +52,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      * @var date
      *
      * @ORM\Column(name="created_at", type="datetime")
+     * @Groups({"user_api"})
      */
     protected $createdAt;
 
@@ -56,6 +60,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf
      * @var date
      *
      * @ORM\Column(name="updated_at", type="datetime")
+     * @Groups({"user_api"})
      */
     protected $updatedAt;