]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Remove user reference in tag
[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();
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->getDoctrine()
78 ->getRepository('WallabagCoreBundle:Entry')
79 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
80
81 $pager->setCurrentPage($page);
82 $pager->setMaxPerPage($perPage);
83
84 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
85 $paginatedCollection = $pagerfantaFactory->createRepresentation(
86 $pager,
87 new Route('api_get_entries', [], $absolute = true)
88 );
89
90 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
91
92 return $this->renderJsonResponse($json);
93 }
94
95 /**
96 * Retrieve a single entry.
97 *
98 * @ApiDoc(
99 * requirements={
100 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
101 * }
102 * )
103 *
104 * @return Entry
105 */
106 public function getEntryAction(Entry $entry)
107 {
108 $this->validateAuthentication();
109 $this->validateUserAccess($entry->getUser()->getId());
110
111 $json = $this->get('serializer')->serialize($entry, 'json');
112
113 return $this->renderJsonResponse($json);
114 }
115
116 /**
117 * Create an entry.
118 *
119 * @ApiDoc(
120 * parameters={
121 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
122 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
123 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
124 * }
125 * )
126 *
127 * @return Entry
128 */
129 public function postEntriesAction(Request $request)
130 {
131 $this->validateAuthentication();
132
133 $url = $request->request->get('url');
134
135 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
136 new Entry($this->getUser()),
137 $url
138 );
139
140 $tags = $request->request->get('tags', '');
141 if (!empty($tags)) {
142 $this->assignTagsToEntry($entry, $tags);
143 }
144
145 $em = $this->getDoctrine()->getManager();
146 $em->persist($entry);
147 $em->flush();
148
149 $json = $this->get('serializer')->serialize($entry, 'json');
150
151 return $this->renderJsonResponse($json);
152 }
153
154 /**
155 * Change several properties of an entry.
156 *
157 * @ApiDoc(
158 * requirements={
159 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
160 * },
161 * parameters={
162 * {"name"="title", "dataType"="string", "required"=false},
163 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
164 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."},
165 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."},
166 * }
167 * )
168 *
169 * @return Entry
170 */
171 public function patchEntriesAction(Entry $entry, Request $request)
172 {
173 $this->validateAuthentication();
174 $this->validateUserAccess($entry->getUser()->getId());
175
176 $title = $request->request->get('title');
177 $isArchived = $request->request->get('is_archived');
178 $isStarred = $request->request->get('is_starred');
179
180 if (!is_null($title)) {
181 $entry->setTitle($title);
182 }
183
184 if (!is_null($isArchived)) {
185 $entry->setArchived($isArchived);
186 }
187
188 if (!is_null($isStarred)) {
189 $entry->setStarred($isStarred);
190 }
191
192 $tags = $request->request->get('tags', '');
193 if (!empty($tags)) {
194 $this->assignTagsToEntry($entry, $tags);
195 }
196
197 $em = $this->getDoctrine()->getManager();
198 $em->flush();
199
200 $json = $this->get('serializer')->serialize($entry, 'json');
201
202 return $this->renderJsonResponse($json);
203 }
204
205 /**
206 * Delete **permanently** an entry.
207 *
208 * @ApiDoc(
209 * requirements={
210 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
211 * }
212 * )
213 *
214 * @return Entry
215 */
216 public function deleteEntriesAction(Entry $entry)
217 {
218 $this->validateAuthentication();
219 $this->validateUserAccess($entry->getUser()->getId());
220
221 $em = $this->getDoctrine()->getManager();
222 $em->remove($entry);
223 $em->flush();
224
225 $json = $this->get('serializer')->serialize($entry, 'json');
226
227 return $this->renderJsonResponse($json);
228 }
229
230 /**
231 * Retrieve all tags for an entry.
232 *
233 * @ApiDoc(
234 * requirements={
235 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
236 * }
237 * )
238 */
239 public function getEntriesTagsAction(Entry $entry)
240 {
241 $this->validateAuthentication();
242 $this->validateUserAccess($entry->getUser()->getId());
243
244 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
245
246 return $this->renderJsonResponse($json);
247 }
248
249 /**
250 * Add one or more tags to an entry.
251 *
252 * @ApiDoc(
253 * requirements={
254 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
255 * },
256 * parameters={
257 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
258 * }
259 * )
260 */
261 public function postEntriesTagsAction(Request $request, Entry $entry)
262 {
263 $this->validateAuthentication();
264 $this->validateUserAccess($entry->getUser()->getId());
265
266 $tags = $request->request->get('tags', '');
267 if (!empty($tags)) {
268 $this->assignTagsToEntry($entry, $tags);
269 }
270
271 $em = $this->getDoctrine()->getManager();
272 $em->persist($entry);
273 $em->flush();
274
275 $json = $this->get('serializer')->serialize($entry, 'json');
276
277 return $this->renderJsonResponse($json);
278 }
279
280 /**
281 * Permanently remove one tag for an entry.
282 *
283 * @ApiDoc(
284 * requirements={
285 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
286 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
287 * }
288 * )
289 */
290 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
291 {
292 $this->validateAuthentication();
293 $this->validateUserAccess($entry->getUser()->getId());
294
295 $entry->removeTag($tag);
296 $em = $this->getDoctrine()->getManager();
297 $em->persist($entry);
298 $em->flush();
299
300 $json = $this->get('serializer')->serialize($entry, 'json');
301
302 return $this->renderJsonResponse($json);
303 }
304
305 /**
306 * Retrieve all tags.
307 *
308 * @ApiDoc()
309 */
310 public function getTagsAction()
311 {
312 $this->validateAuthentication();
313
314 $tags = $this->getDoctrine()
315 ->getRepository('WallabagCoreBundle:Tag')
316 ->findAllTags($this->getUser()->getId());
317
318 $json = $this->get('serializer')->serialize($tags, 'json');
319
320 return $this->renderJsonResponse($json);
321 }
322
323 /**
324 * Permanently remove one tag from **every** entry.
325 *
326 * @ApiDoc(
327 * requirements={
328 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
329 * }
330 * )
331 */
332 public function deleteTagAction(Tag $tag)
333 {
334 $this->validateAuthentication();
335
336 $this->getDoctrine()
337 ->getRepository('WallabagCoreBundle:Entry')
338 ->removeTag($this->getUser()->getId(), $tag);
339
340 $em = $this->getDoctrine()->getManager();
341 $em->remove($tag);
342 $em->flush();
343
344 $json = $this->get('serializer')->serialize($tag, 'json');
345
346 return $this->renderJsonResponse($json);
347 }
348
349 /**
350 * Validate that the first id is equal to the second one.
351 * If not, throw exception. It means a user try to access information from an other user.
352 *
353 * @param int $requestUserId User id from the requested source
354 */
355 private function validateUserAccess($requestUserId)
356 {
357 $user = $this->get('security.token_storage')->getToken()->getUser();
358 if ($requestUserId != $user->getId()) {
359 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
360 }
361 }
362
363 /**
364 * Send a JSON Response.
365 * We don't use the Symfony JsonRespone, because it takes an array as parameter instead of a JSON string.
366 *
367 * @param string $json
368 *
369 * @return Response
370 */
371 private function renderJsonResponse($json)
372 {
373 return new Response($json, 200, array('application/json'));
374 }
375 }