aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2017-04-29 19:22:50 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2017-05-29 10:16:23 +0200
commit2251045901875aa815dee43ec467fb1af8d416d0 (patch)
tree45d49bfaee4afdc502231ce60f835846a22afeca /src/Wallabag/ApiBundle
parentb788add08787863ac2a0e68ddaf4620da4b2b33c (diff)
downloadwallabag-2251045901875aa815dee43ec467fb1af8d416d0.tar.gz
wallabag-2251045901875aa815dee43ec467fb1af8d416d0.tar.zst
wallabag-2251045901875aa815dee43ec467fb1af8d416d0.zip
WIP
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r--src/Wallabag/ApiBundle/Controller/UserRestController.php98
-rw-r--r--src/Wallabag/ApiBundle/Resources/config/routing_rest.yml5
2 files changed, 103 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php
new file mode 100644
index 00000000..c5ffbdf1
--- /dev/null
+++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php
@@ -0,0 +1,98 @@
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use FOS\UserBundle\Event\UserEvent;
6use FOS\UserBundle\FOSUserEvents;
7use JMS\Serializer\SerializationContext;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Symfony\Component\HttpFoundation\JsonResponse;
10
11class UserRestController extends WallabagRestController
12{
13 /**
14 * Retrieve user informations
15 *
16 * @ApiDoc()
17 *
18 * @return JsonResponse
19 */
20 public function getUserAction()
21 {
22 $this->validateAuthentication();
23
24 $serializationContext = SerializationContext::create()->setGroups(['user_api']);
25 $json = $this->get('serializer')->serialize($this->getUser(), 'json', $serializationContext);
26
27 return (new JsonResponse())->setJson($json);
28 }
29
30 /**
31 * Register an user
32 *
33 * @ApiDoc(
34 * requirements={
35 * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
36 * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"}
37 * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"}
38 * }
39 * )
40 * @return JsonResponse
41 */
42 // TODO : Make this method (or the whole API) accessible only through https
43 public function putUserAction($username, $password, $email)
44 {
45 if (!$this->container->getParameter('fosuser_registration')) {
46 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
47 return (new JsonResponse())->setJson($json)->setStatusCode(403);
48 }
49
50 if ($password === '') { // TODO : might be a good idea to enforce restrictions here
51 $json = $this->get('serializer')->serialize(['error' => 'Password is blank'], 'json');
52 return (new JsonResponse())->setJson($json)->setStatusCode(400);
53 }
54
55
56 // TODO : Make only one call to database by using a custom repository method
57 if ($this->getDoctrine()
58 ->getRepository('WallabagUserBundle:User')
59 ->findOneByUserName($username)) {
60 $json = $this->get('serializer')->serialize(['error' => 'Username is already taken'], 'json');
61 return (new JsonResponse())->setJson($json)->setStatusCode(409);
62 }
63
64 if ($this->getDoctrine()
65 ->getRepository('WallabagUserBundle:User')
66 ->findOneByEmail($email)) {
67 $json = $this->get('serializer')->serialize(['error' => 'An account with this email already exists'], 'json');
68 return (new JsonResponse())->setJson($json)->setStatusCode(409);
69 }
70
71 $em = $this->get('doctrine.orm.entity_manager');
72
73 $userManager = $this->get('fos_user.user_manager');
74 $user = $userManager->createUser();
75
76 $user->setUsername($username);
77
78 $user->setPlainPassword($password);
79
80 $user->setEmail($email);
81
82 $user->setEnabled(true);
83 $user->addRole('ROLE_USER');
84
85 $em->persist($user);
86
87 // dispatch a created event so the associated config will be created
88 $event = new UserEvent($user);
89 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
90
91 $serializationContext = SerializationContext::create()->setGroups(['user_api']);
92 $json = $this->get('serializer')->serialize($user, 'json', $serializationContext);
93
94 return (new JsonResponse())->setJson($json);
95
96 }
97
98}
diff --git a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
index 57d37f4b..c0283e71 100644
--- a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
+++ b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
@@ -17,3 +17,8 @@ misc:
17 type: rest 17 type: rest
18 resource: "WallabagApiBundle:WallabagRest" 18 resource: "WallabagApiBundle:WallabagRest"
19 name_prefix: api_ 19 name_prefix: api_
20
21user:
22 type: rest
23 resource: "WallabagApiBundle:UserRest"
24 name_prefix: api_