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