aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas Lœuillet <nicolas@loeuillet.org>2015-02-26 09:41:42 +0100
committerNicolas Lœuillet <nicolas@loeuillet.org>2015-03-06 21:09:15 +0100
commit092ca70725b0263390e45c46f93828c613eca3f0 (patch)
tree7b9fe8b824505b645bda6896a3589f09e25b1ccc
parenta36737f4859e3acbddf5cfe90c279ba725a6d88a (diff)
downloadwallabag-092ca70725b0263390e45c46f93828c613eca3f0.tar.gz
wallabag-092ca70725b0263390e45c46f93828c613eca3f0.tar.zst
wallabag-092ca70725b0263390e45c46f93828c613eca3f0.zip
add relation between user and tags, tests are broken
-rw-r--r--src/Wallabag/CoreBundle/Controller/WallabagRestController.php104
-rw-r--r--src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php4
-rw-r--r--src/Wallabag/CoreBundle/Entity/Entry.php5
-rw-r--r--src/Wallabag/CoreBundle/Entity/Tag.php8
-rw-r--r--src/Wallabag/CoreBundle/Entity/User.php30
-rw-r--r--src/Wallabag/CoreBundle/Repository/EntryRepository.php7
-rw-r--r--src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php4
7 files changed, 131 insertions, 31 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
index e59ad4b7..81bfbe12 100644
--- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
@@ -9,10 +9,36 @@ use Symfony\Component\HttpFoundation\Response;
9use Wallabag\CoreBundle\Entity\Entry; 9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag; 10use Wallabag\CoreBundle\Entity\Tag;
11use Wallabag\CoreBundle\Service\Extractor; 11use Wallabag\CoreBundle\Service\Extractor;
12use Symfony\Component\Security\Core\Exception\AccessDeniedException;
12 13
13class WallabagRestController extends Controller 14class WallabagRestController extends Controller
14{ 15{
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 /**
16 * Retrieve salt for a giver user. 42 * Retrieve salt for a giver user.
17 * 43 *
18 * @ApiDoc( 44 * @ApiDoc(
@@ -87,6 +113,10 @@ class WallabagRestController extends Controller
87 */ 113 */
88 public function getEntryAction(Entry $entry) 114 public function getEntryAction(Entry $entry)
89 { 115 {
116 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
117 throw $this->createAccessDeniedException();
118 }
119
90 $json = $this->get('serializer')->serialize($entry, 'json'); 120 $json = $this->get('serializer')->serialize($entry, 'json');
91 121
92 return new Response($json, 200, array('application/json')); 122 return new Response($json, 200, array('application/json'));
@@ -106,7 +136,6 @@ class WallabagRestController extends Controller
106 */ 136 */
107 public function postEntriesAction(Request $request) 137 public function postEntriesAction(Request $request)
108 { 138 {
109 //TODO gérer si on passe les tags
110 $url = $request->request->get('url'); 139 $url = $request->request->get('url');
111 140
112 $content = Extractor::extract($url); 141 $content = Extractor::extract($url);
@@ -114,6 +143,9 @@ class WallabagRestController extends Controller
114 $entry->setUrl($url); 143 $entry->setUrl($url);
115 $entry->setTitle($request->request->get('title') ?: $content->getTitle()); 144 $entry->setTitle($request->request->get('title') ?: $content->getTitle());
116 $entry->setContent($content->getBody()); 145 $entry->setContent($content->getBody());
146
147 $this->assignTagsToEntry($entry, $request->request->get('tags', array()));
148
117 $em = $this->getDoctrine()->getManager(); 149 $em = $this->getDoctrine()->getManager();
118 $em->persist($entry); 150 $em->persist($entry);
119 $em->flush(); 151 $em->flush();
@@ -141,8 +173,11 @@ class WallabagRestController extends Controller
141 */ 173 */
142 public function patchEntriesAction(Entry $entry, Request $request) 174 public function patchEntriesAction(Entry $entry, Request $request)
143 { 175 {
176 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
177 throw $this->createAccessDeniedException();
178 }
179
144 $title = $request->request->get("title"); 180 $title = $request->request->get("title");
145 $tags = $request->request->get("tags", array());
146 $isArchived = $request->request->get("archive"); 181 $isArchived = $request->request->get("archive");
147 $isStarred = $request->request->get("star"); 182 $isStarred = $request->request->get("star");
148 183
@@ -158,6 +193,8 @@ class WallabagRestController extends Controller
158 $entry->setStarred($isStarred); 193 $entry->setStarred($isStarred);
159 } 194 }
160 195
196 $this->assignTagsToEntry($entry, $request->request->get('tags', array()));
197
161 $em = $this->getDoctrine()->getManager(); 198 $em = $this->getDoctrine()->getManager();
162 $em->flush(); 199 $em->flush();
163 200
@@ -176,6 +213,10 @@ class WallabagRestController extends Controller
176 */ 213 */
177 public function deleteEntriesAction(Entry $entry) 214 public function deleteEntriesAction(Entry $entry)
178 { 215 {
216 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
217 throw $this->createAccessDeniedException();
218 }
219
179 $em = $this->getDoctrine()->getManager(); 220 $em = $this->getDoctrine()->getManager();
180 $em->remove($entry); 221 $em->remove($entry);
181 $em->flush(); 222 $em->flush();
@@ -196,6 +237,12 @@ class WallabagRestController extends Controller
196 */ 237 */
197 public function getEntriesTagsAction(Entry $entry) 238 public function getEntriesTagsAction(Entry $entry)
198 { 239 {
240 var_dump($entry->getUser()->getId());
241 var_dump($this->getUser()->getId());
242 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
243 throw $this->createAccessDeniedException();
244 }
245
199 $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); 246 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
200 247
201 return new Response($json, 200, array('application/json')); 248 return new Response($json, 200, array('application/json'));
@@ -215,25 +262,12 @@ class WallabagRestController extends Controller
215 */ 262 */
216 public function postEntriesTagsAction(Request $request, Entry $entry) 263 public function postEntriesTagsAction(Request $request, Entry $entry)
217 { 264 {
218 $tags = explode(',', $request->request->get('tags')); 265 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
219 266 throw $this->createAccessDeniedException();
220 foreach ($tags as $label) {
221 $tagEntity = $this
222 ->getDoctrine()
223 ->getRepository('WallabagCoreBundle:Tag')
224 ->findOneByLabel($label);
225
226 if (is_null($tagEntity)) {
227 $tagEntity = new Tag();
228 $tagEntity->setLabel($label);
229 }
230
231 // only add the tag on the entry if the relation doesn't exist
232 if (!$entry->getTags()->contains($tagEntity)) {
233 $entry->addTag($tagEntity);
234 }
235 } 267 }
236 268
269 $this->assignTagsToEntry($entry, $request->request->get('tags', array()));
270
237 $em = $this->getDoctrine()->getManager(); 271 $em = $this->getDoctrine()->getManager();
238 $em->persist($entry); 272 $em->persist($entry);
239 $em->flush(); 273 $em->flush();
@@ -255,17 +289,30 @@ class WallabagRestController extends Controller
255 */ 289 */
256 public function deleteEntriesTagsAction(Entry $entry, Tag $tag) 290 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
257 { 291 {
292 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
293 throw $this->createAccessDeniedException();
294 }
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 new Response($json, 200, array('application/json'));
258 } 304 }
259 305
260 /** 306 /**
261 * Retrieve all tags 307 * Retrieve all tags
262 * 308 *
263 * @ApiDoc( 309 * @ApiDoc()
264 * {"name"="user", "dataType"="integer", "requirement"="\w+", "description"="The user ID"}
265 * )
266 */ 310 */
267 public function getTagsUserAction() 311 public function getTagsAction()
268 { 312 {
313 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
314
315 return new Response($json, 200, array('application/json'));
269 } 316 }
270 317
271 /** 318 /**
@@ -279,5 +326,16 @@ class WallabagRestController extends Controller
279 */ 326 */
280 public function deleteTagAction(Tag $tag) 327 public function deleteTagAction(Tag $tag)
281 { 328 {
329 if ($tag->getUser()->getId() != $this->getUser()->getId()) {
330 throw $this->createAccessDeniedException();
331 }
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 new Response($json, 200, array('application/json'));
282 } 340 }
283} 341}
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
index edab9adc..b0f07755 100644
--- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
+++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadEntryData.php
@@ -38,9 +38,9 @@ class LoadEntryData extends AbstractFixture implements OrderedFixtureInterface
38 $entry3->setTitle('test title entry3'); 38 $entry3->setTitle('test title entry3');
39 $entry3->setContent('This is my content /o/'); 39 $entry3->setContent('This is my content /o/');
40 40
41 $tag1 = new Tag(); 41 $tag1 = new Tag($this->getReference('admin-user'));
42 $tag1->setLabel("foo"); 42 $tag1->setLabel("foo");
43 $tag2 = new Tag(); 43 $tag2 = new Tag($this->getReference('admin-user'));
44 $tag2->setLabel("bar"); 44 $tag2->setLabel("bar");
45 45
46 $entry3->addTag($tag1); 46 $entry3->addTag($tag1);
diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php
index 229a6704..75aeae84 100644
--- a/src/Wallabag/CoreBundle/Entity/Entry.php
+++ b/src/Wallabag/CoreBundle/Entity/Entry.php
@@ -406,4 +406,9 @@ class Entry
406 $this->tags[] = $tag; 406 $this->tags[] = $tag;
407 $tag->addEntry($this); 407 $tag->addEntry($this);
408 } 408 }
409
410 public function removeTag(Tag $tag)
411 {
412 $this->tags->removeElement($tag);
413 }
409} 414}
diff --git a/src/Wallabag/CoreBundle/Entity/Tag.php b/src/Wallabag/CoreBundle/Entity/Tag.php
index 5aed1fa0..29a5e4b5 100644
--- a/src/Wallabag/CoreBundle/Entity/Tag.php
+++ b/src/Wallabag/CoreBundle/Entity/Tag.php
@@ -41,8 +41,14 @@ class Tag
41 */ 41 */
42 private $entries; 42 private $entries;
43 43
44 public function __construct() 44 /**
45 * @ORM\ManyToOne(targetEntity="User", inversedBy="tags")
46 */
47 private $user;
48
49 public function __construct(User $user)
45 { 50 {
51 $this->user = $user;
46 $this->entries = new ArrayCollection(); 52 $this->entries = new ArrayCollection();
47 } 53 }
48 /** 54 /**
diff --git a/src/Wallabag/CoreBundle/Entity/User.php b/src/Wallabag/CoreBundle/Entity/User.php
index 5589c039..f05c8760 100644
--- a/src/Wallabag/CoreBundle/Entity/User.php
+++ b/src/Wallabag/CoreBundle/Entity/User.php
@@ -101,10 +101,17 @@ class User implements AdvancedUserInterface, \Serializable
101 */ 101 */
102 private $config; 102 private $config;
103 103
104 /**
105 * @ORM\OneToMany(targetEntity="Tag", mappedBy="user", cascade={"remove"})
106 */
107 private $tags;
108
104 public function __construct() 109 public function __construct()
105 { 110 {
106 $this->salt = md5(uniqid(null, true)); 111 $this->isActive = true;
107 $this->entries = new ArrayCollection(); 112 $this->salt = md5(uniqid(null, true));
113 $this->entries = new ArrayCollection();
114 $this->tags = new ArrayCollection();
108 } 115 }
109 116
110 /** 117 /**
@@ -279,6 +286,25 @@ class User implements AdvancedUserInterface, \Serializable
279 } 286 }
280 287
281 /** 288 /**
289 * @param Entry $entry
290 *
291 * @return User
292 */
293 public function addTag(Tag $tag)
294 {
295 $this->tags[] = $tag;
296
297 return $this;
298 }
299
300 /**
301 * @return ArrayCollection<Tag>
302 */
303 public function getTags()
304 {
305 return $this->tags;
306 }
307 /**
282 * @inheritDoc 308 * @inheritDoc
283 */ 309 */
284 public function eraseCredentials() 310 public function eraseCredentials()
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
index 10fb9bf7..a8085ac9 100644
--- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php
@@ -124,11 +124,14 @@ class EntryRepository extends EntityRepository
124 * 124 *
125 * @return Entry 125 * @return Entry
126 */ 126 */
127 public function findOneWithTags() 127 public function findOneWithTags($userId)
128 { 128 {
129 $qb = $this->createQueryBuilder('e') 129 $qb = $this->createQueryBuilder('e')
130 ->innerJoin('e.tags', 't') 130 ->innerJoin('e.tags', 't')
131 ->addSelect('t'); 131 ->addSelect('t')
132 ->where('t.user=:userId')->setParameter('userId', 1);
133
134 return $qb->getQuery()->getOneOrNullResult();
132 135
133 return $qb 136 return $qb
134 ->getQuery() 137 ->getQuery()
diff --git a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
index cadbb70b..04448537 100644
--- a/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
+++ b/src/Wallabag/CoreBundle/Tests/Controller/WallabagRestControllerTest.php
@@ -161,7 +161,9 @@ class WallabagRestControllerTest extends WallabagTestCase
161 $entry = $client->getContainer() 161 $entry = $client->getContainer()
162 ->get('doctrine.orm.entity_manager') 162 ->get('doctrine.orm.entity_manager')
163 ->getRepository('WallabagCoreBundle:Entry') 163 ->getRepository('WallabagCoreBundle:Entry')
164 ->findOneWithTags(); 164 ->findOneWithTags(1);
165
166 var_dump($entry->getTitle());
165 167
166 if (!$entry) { 168 if (!$entry) {
167 $this->markTestSkipped('No content found in db.'); 169 $this->markTestSkipped('No content found in db.');