diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
3 files changed, 148 insertions, 4 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 4801811d..31bb67fd 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -318,7 +318,7 @@ class EntryRestController extends WallabagRestController | |||
318 | 318 | ||
319 | $tags = $request->request->get('tags', ''); | 319 | $tags = $request->request->get('tags', ''); |
320 | if (!empty($tags)) { | 320 | if (!empty($tags)) { |
321 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | 321 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
322 | } | 322 | } |
323 | 323 | ||
324 | if (!is_null($isStarred)) { | 324 | if (!is_null($isStarred)) { |
@@ -379,7 +379,7 @@ class EntryRestController extends WallabagRestController | |||
379 | 379 | ||
380 | $tags = $request->request->get('tags', ''); | 380 | $tags = $request->request->get('tags', ''); |
381 | if (!empty($tags)) { | 381 | if (!empty($tags)) { |
382 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | 382 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
383 | } | 383 | } |
384 | 384 | ||
385 | $em = $this->getDoctrine()->getManager(); | 385 | $em = $this->getDoctrine()->getManager(); |
@@ -497,7 +497,7 @@ class EntryRestController extends WallabagRestController | |||
497 | 497 | ||
498 | $tags = $request->request->get('tags', ''); | 498 | $tags = $request->request->get('tags', ''); |
499 | if (!empty($tags)) { | 499 | if (!empty($tags)) { |
500 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | 500 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
501 | } | 501 | } |
502 | 502 | ||
503 | $em = $this->getDoctrine()->getManager(); | 503 | $em = $this->getDoctrine()->getManager(); |
@@ -626,7 +626,7 @@ class EntryRestController extends WallabagRestController | |||
626 | $tags = $element->tags; | 626 | $tags = $element->tags; |
627 | 627 | ||
628 | if (false !== $entry && !(empty($tags))) { | 628 | if (false !== $entry && !(empty($tags))) { |
629 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | 629 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
630 | 630 | ||
631 | $em = $this->getDoctrine()->getManager(); | 631 | $em = $this->getDoctrine()->getManager(); |
632 | $em->persist($entry); | 632 | $em->persist($entry); |
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php new file mode 100644 index 00000000..a1b78e3f --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php | |||
@@ -0,0 +1,139 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\ApiBundle\Controller; | ||
4 | |||
5 | use FOS\UserBundle\Event\UserEvent; | ||
6 | use FOS\UserBundle\FOSUserEvents; | ||
7 | use JMS\Serializer\SerializationContext; | ||
8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | ||
9 | use Symfony\Component\HttpFoundation\Request; | ||
10 | use Symfony\Component\HttpFoundation\JsonResponse; | ||
11 | use Wallabag\UserBundle\Entity\User; | ||
12 | |||
13 | class UserRestController extends WallabagRestController | ||
14 | { | ||
15 | /** | ||
16 | * Retrieve current logged in user informations. | ||
17 | * | ||
18 | * @ApiDoc() | ||
19 | * | ||
20 | * @return JsonResponse | ||
21 | */ | ||
22 | public function getUserAction() | ||
23 | { | ||
24 | $this->validateAuthentication(); | ||
25 | |||
26 | return $this->sendUser($this->getUser()); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Register an user. | ||
31 | * | ||
32 | * @ApiDoc( | ||
33 | * requirements={ | ||
34 | * {"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"="email", "dataType"="string", "required"=true, "description"="The user's email"} | ||
37 | * } | ||
38 | * ) | ||
39 | * | ||
40 | * @todo Make this method (or the whole API) accessible only through https | ||
41 | * | ||
42 | * @return JsonResponse | ||
43 | */ | ||
44 | public function putUserAction(Request $request) | ||
45 | { | ||
46 | if (!$this->container->getParameter('fosuser_registration')) { | ||
47 | $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json'); | ||
48 | |||
49 | return (new JsonResponse())->setJson($json)->setStatusCode(403); | ||
50 | } | ||
51 | |||
52 | $userManager = $this->get('fos_user.user_manager'); | ||
53 | $user = $userManager->createUser(); | ||
54 | // enable created user by default | ||
55 | $user->setEnabled(true); | ||
56 | |||
57 | $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [ | ||
58 | 'csrf_protection' => false, | ||
59 | ]); | ||
60 | |||
61 | // simulate form submission | ||
62 | $form->submit([ | ||
63 | 'username' => $request->request->get('username'), | ||
64 | 'plainPassword' => [ | ||
65 | 'first' => $request->request->get('password'), | ||
66 | 'second' => $request->request->get('password'), | ||
67 | ], | ||
68 | 'email' => $request->request->get('email'), | ||
69 | ]); | ||
70 | |||
71 | if ($form->isSubmitted() && false === $form->isValid()) { | ||
72 | $view = $this->view($form, 400); | ||
73 | $view->setFormat('json'); | ||
74 | |||
75 | // handle errors in a more beautiful way than the default view | ||
76 | $data = json_decode($this->handleView($view)->getContent(), true)['children']; | ||
77 | $errors = []; | ||
78 | |||
79 | if (isset($data['username']['errors'])) { | ||
80 | $errors['username'] = $this->translateErrors($data['username']['errors']); | ||
81 | } | ||
82 | |||
83 | if (isset($data['email']['errors'])) { | ||
84 | $errors['email'] = $this->translateErrors($data['email']['errors']); | ||
85 | } | ||
86 | |||
87 | if (isset($data['plainPassword']['children']['first']['errors'])) { | ||
88 | $errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']); | ||
89 | } | ||
90 | |||
91 | $json = $this->get('serializer')->serialize(['error' => $errors], 'json'); | ||
92 | |||
93 | return (new JsonResponse())->setJson($json)->setStatusCode(400); | ||
94 | } | ||
95 | |||
96 | $userManager->updateUser($user); | ||
97 | |||
98 | // dispatch a created event so the associated config will be created | ||
99 | $event = new UserEvent($user, $request); | ||
100 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | ||
101 | |||
102 | return $this->sendUser($user); | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Send user response. | ||
107 | * | ||
108 | * @param User $user | ||
109 | * | ||
110 | * @return JsonResponse | ||
111 | */ | ||
112 | private function sendUser(User $user) | ||
113 | { | ||
114 | $json = $this->get('serializer')->serialize( | ||
115 | $user, | ||
116 | 'json', | ||
117 | SerializationContext::create()->setGroups(['user_api']) | ||
118 | ); | ||
119 | |||
120 | return (new JsonResponse())->setJson($json); | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Translate errors message. | ||
125 | * | ||
126 | * @param array $errors | ||
127 | * | ||
128 | * @return array | ||
129 | */ | ||
130 | private function translateErrors($errors) | ||
131 | { | ||
132 | $translatedErrors = []; | ||
133 | foreach ($errors as $error) { | ||
134 | $translatedErrors[] = $this->get('translator')->trans($error); | ||
135 | } | ||
136 | |||
137 | return $translatedErrors; | ||
138 | } | ||
139 | } | ||
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 | |||
21 | user: | ||
22 | type: rest | ||
23 | resource: "WallabagApiBundle:UserRest" | ||
24 | name_prefix: api_ | ||