]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
74bfe4dcdb286a3337e00d889a80ffb3d1918b05
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
1 <?php
2
3 namespace Wallabag\ApiBundle\Controller;
4
5 use FOS\RestBundle\Controller\FOSRestController;
6 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Wallabag\CoreBundle\Entity\Entry;
10 use Wallabag\CoreBundle\Entity\Tag;
11 use Hateoas\Configuration\Route;
12 use Hateoas\Representation\Factory\PagerfantaFactory;
13
14 class WallabagRestController extends FOSRestController
15 {
16 /**
17 * @param Entry $entry
18 * @param string $tags
19 */
20 private function assignTagsToEntry(Entry $entry, $tags)
21 {
22 foreach (explode(',', $tags) as $label) {
23 $label = trim($label);
24 $tagEntity = $this
25 ->getDoctrine()
26 ->getRepository('WallabagCoreBundle:Tag')
27 ->findOneByLabel($label);
28
29 if (is_null($tagEntity)) {
30 $tagEntity = new Tag($this->getUser());
31 $tagEntity->setLabel($label);
32 }
33
34 // only add the tag on the entry if the relation doesn't exist
35 if (!$entry->getTags()->contains($tagEntity)) {
36 $entry->addTag($tagEntity);
37 }
38 }
39 }
40
41 private function validateAuthentication()
42 {
43 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
44 throw new AccessDeniedException();
45 }
46 }
47
48 /**
49 * Retrieve all entries. It could be filtered by many options.
50 *
51 * @ApiDoc(
52 * parameters={
53 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."},
54 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."},
55 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
56 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
57 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
58 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
59 * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
60 * }
61 * )
62 *
63 * @return Entry
64 */
65 public function getEntriesAction(Request $request)
66 {
67 $this->validateAuthentication();
68
69 $isArchived = $request->query->get('archive');
70 $isStarred = $request->query->get('star');
71 $sort = $request->query->get('sort', 'created');
72 $order = $request->query->get('order', 'desc');
73 $page = (int) $request->query->get('page', 1);
74 $perPage = (int) $request->query->get('perPage', 30);
75 $tags = $request->query->get('tags', []);
76
77 $pager = $this
78 ->getDoctrine()
79 ->getRepository('WallabagCoreBundle:Entry')
80 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
81
82 $pager->setCurrentPage($page);
83 $pager->setMaxPerPage($perPage);
84
85 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
86 $paginatedCollection = $pagerfantaFactory->createRepresentation(
87 $pager,
88 new Route('api_get_entries', [], $absolute = true)
89 );
90
91 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
92
93 return $this->renderJsonResponse($json);
94 }
95
96 /**
97 * Retrieve a single entry.
98 *
99 * @ApiDoc(
100 * requirements={
101 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
102 * }
103 * )
104 *
105 * @return Entry
106 */
107 public function getEntryAction(Entry $entry)
108 {
109 $this->validateAuthentication();
110 $this->validateUserAccess($entry->getUser()->getId());
111
112 $json = $this->get('serializer')->serialize($entry, 'json');
113
114 return $this->renderJsonResponse($json);
115 }
116
117 /**
118 * Create an entry.
119 *
120 * @ApiDoc(
121 * parameters={
122 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
123 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
124 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
125 * }
126 * )
127 *
128 * @return Entry
129 */
130 public function postEntriesAction(Request $request)
131 {
132 $this->validateAuthentication();
133
134 $url = $request->request->get('url');
135
136 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
137 new Entry($this->getUser()),
138 $url
139 );
140
141 $tags = $request->request->get('tags', '');
142 if (!empty($tags)) {
143 $this->assignTagsToEntry($entry, $tags);
144 }
145
146 $em = $this->getDoctrine()->getManager();
147 $em->persist($entry);
148 $em->flush();
149
150 $json = $this->get('serializer')->serialize($entry, 'json');
151
152 return $this->renderJsonResponse($json);
153 }
154
155 /**
156 * Change several properties of an entry.
157 *
158 * @ApiDoc(
159 * requirements={
160 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
161 * },
162 * parameters={
163 * {"name"="title", "dataType"="string", "required"=false},
164 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
165 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."},
166 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."},
167 * }
168 * )
169 *
170 * @return Entry
171 */
172 public function patchEntriesAction(Entry $entry, Request $request)
173 {
174 $this->validateAuthentication();
175 $this->validateUserAccess($entry->getUser()->getId());
176
177 $title = $request->request->get('title');
178 $isArchived = $request->request->get('is_archived');
179 $isStarred = $request->request->get('is_starred');
180
181 if (!is_null($title)) {
182 $entry->setTitle($title);
183 }
184
185 if (!is_null($isArchived)) {
186 $entry->setArchived($isArchived);
187 }
188
189 if (!is_null($isStarred)) {
190 $entry->setStarred($isStarred);
191 }
192
193 $tags = $request->request->get('tags', '');
194 if (!empty($tags)) {
195 $this->assignTagsToEntry($entry, $tags);
196 }
197
198 $em = $this->getDoctrine()->getManager();
199 $em->flush();
200
201 $json = $this->get('serializer')->serialize($entry, 'json');
202
203 return $this->renderJsonResponse($json);
204 }
205
206 /**
207 * Delete **permanently** an entry.
208 *
209 * @ApiDoc(
210 * requirements={
211 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
212 * }
213 * )
214 *
215 * @return Entry
216 */
217 public function deleteEntriesAction(Entry $entry)
218 {
219 $this->validateAuthentication();
220 $this->validateUserAccess($entry->getUser()->getId());
221
222 $em = $this->getDoctrine()->getManager();
223 $em->remove($entry);
224 $em->flush();
225
226 $json = $this->get('serializer')->serialize($entry, 'json');
227
228 return $this->renderJsonResponse($json);
229 }
230
231 /**
232 * Retrieve all tags for an entry.
233 *
234 * @ApiDoc(
235 * requirements={
236 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
237 * }
238 * )
239 */
240 public function getEntriesTagsAction(Entry $entry)
241 {
242 $this->validateAuthentication();
243 $this->validateUserAccess($entry->getUser()->getId());
244
245 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
246
247 return $this->renderJsonResponse($json);
248 }
249
250 /**
251 * Add one or more tags to an entry.
252 *
253 * @ApiDoc(
254 * requirements={
255 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
256 * },
257 * parameters={
258 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
259 * }
260 * )
261 */
262 public function postEntriesTagsAction(Request $request, Entry $entry)
263 {
264 $this->validateAuthentication();
265 $this->validateUserAccess($entry->getUser()->getId());
266
267 $tags = $request->request->get('tags', '');
268 if (!empty($tags)) {
269 $this->assignTagsToEntry($entry, $tags);
270 }
271
272 $em = $this->getDoctrine()->getManager();
273 $em->persist($entry);
274 $em->flush();
275
276 $json = $this->get('serializer')->serialize($entry, 'json');
277
278 return $this->renderJsonResponse($json);
279 }
280
281 /**
282 * Permanently remove one tag for an entry.
283 *
284 * @ApiDoc(
285 * requirements={
286 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
287 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
288 * }
289 * )
290 */
291 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
292 {
293 $this->validateAuthentication();
294 $this->validateUserAccess($entry->getUser()->getId());
295
296 $entry->removeTag($tag);
297 $em = $this->getDoctrine()->getManager();
298 $em->persist($entry);
299 $em->flush();
300
301 $json = $this->get('serializer')->serialize($entry, 'json');
302
303 return $this->renderJsonResponse($json);
304 }
305
306 /**
307 * Retrieve all tags.
308 *
309 * @ApiDoc()
310 */
311 public function getTagsAction()
312 {
313 $this->validateAuthentication();
314 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
315
316 return $this->renderJsonResponse($json);
317 }
318
319 /**
320 * Permanently remove one tag from **every** entry.
321 *
322 * @ApiDoc(
323 * requirements={
324 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
325 * }
326 * )
327 */
328 public function deleteTagAction(Tag $tag)
329 {
330 $this->validateAuthentication();
331 $this->validateUserAccess($tag->getUser()->getId());
332
333 $em = $this->getDoctrine()->getManager();
334 $em->remove($tag);
335 $em->flush();
336
337 $json = $this->get('serializer')->serialize($tag, 'json');
338
339 return $this->renderJsonResponse($json);
340 }
341
342 /**
343 * Validate that the first id is equal to the second one.
344 * If not, throw exception. It means a user try to access information from an other user.
345 *
346 * @param int $requestUserId User id from the requested source
347 */
348 private function validateUserAccess($requestUserId)
349 {
350 $user = $this->get('security.token_storage')->getToken()->getUser();
351 if ($requestUserId != $user->getId()) {
352 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
353 }
354 }
355
356 /**
357 * Send a JSON Response.
358 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
359 *
360 * @param string $json
361 *
362 * @return Response
363 */
364 private function renderJsonResponse($json)
365 {
366 return new Response($json, 200, array('application/json'));
367 }
368 }