diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-04-15 07:58:01 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-04-15 07:58:01 +0200 |
commit | af497a641c2a46c99bbc67215e041a46c91695bc (patch) | |
tree | 40795f74b9bf9b6bcfeab4f71e39994c6688dd31 | |
parent | f2e5fdc3666a2a6525b4202ab48df05efeebaf5c (diff) | |
download | wallabag-af497a641c2a46c99bbc67215e041a46c91695bc.tar.gz wallabag-af497a641c2a46c99bbc67215e041a46c91695bc.tar.zst wallabag-af497a641c2a46c99bbc67215e041a46c91695bc.zip |
Redirect to homepage if referer is null
Fix #1924
5 files changed, 104 insertions, 4 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 17b72bd1..9443ae82 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php | |||
@@ -344,7 +344,9 @@ class EntryController extends Controller | |||
344 | $message | 344 | $message |
345 | ); | 345 | ); |
346 | 346 | ||
347 | return $this->redirect($request->headers->get('referer')); | 347 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
348 | |||
349 | return $this->redirect($redirectUrl); | ||
348 | } | 350 | } |
349 | 351 | ||
350 | /** | 352 | /** |
@@ -374,7 +376,9 @@ class EntryController extends Controller | |||
374 | $message | 376 | $message |
375 | ); | 377 | ); |
376 | 378 | ||
377 | return $this->redirect($request->headers->get('referer')); | 379 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
380 | |||
381 | return $this->redirect($redirectUrl); | ||
378 | } | 382 | } |
379 | 383 | ||
380 | /** | 384 | /** |
@@ -408,7 +412,11 @@ class EntryController extends Controller | |||
408 | ); | 412 | ); |
409 | 413 | ||
410 | // don't redirect user to the deleted entry | 414 | // don't redirect user to the deleted entry |
411 | return $this->redirect($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); | 415 | $to = ($url !== $request->headers->get('referer') ? $request->headers->get('referer') : $this->generateUrl('homepage')); |
416 | |||
417 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($to); | ||
418 | |||
419 | return $this->redirect($redirectUrl); | ||
412 | } | 420 | } |
413 | 421 | ||
414 | /** | 422 | /** |
diff --git a/src/Wallabag/CoreBundle/Controller/TagController.php b/src/Wallabag/CoreBundle/Controller/TagController.php index e8e9ecbe..16d14d79 100644 --- a/src/Wallabag/CoreBundle/Controller/TagController.php +++ b/src/Wallabag/CoreBundle/Controller/TagController.php | |||
@@ -65,7 +65,9 @@ class TagController extends Controller | |||
65 | } | 65 | } |
66 | $em->flush(); | 66 | $em->flush(); |
67 | 67 | ||
68 | return $this->redirect($request->headers->get('referer')); | 68 | $redirectUrl = $this->get('wallabag_core.helper.redirect')->to($request->headers->get('referer')); |
69 | |||
70 | return $this->redirect($redirectUrl); | ||
69 | } | 71 | } |
70 | 72 | ||
71 | /** | 73 | /** |
diff --git a/src/Wallabag/CoreBundle/Helper/Redirect.php b/src/Wallabag/CoreBundle/Helper/Redirect.php new file mode 100644 index 00000000..0921c3f9 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/Redirect.php | |||
@@ -0,0 +1,36 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Symfony\Component\Routing\Router; | ||
6 | |||
7 | class Redirect | ||
8 | { | ||
9 | private $router; | ||
10 | |||
11 | public function __construct(Router $router) | ||
12 | { | ||
13 | $this->router = $router; | ||
14 | } | ||
15 | |||
16 | /** | ||
17 | * @param string $url URL to redirect | ||
18 | * @param string $fallback Fallback URL if $url is null | ||
19 | * | ||
20 | * @return string | ||
21 | */ | ||
22 | public function to($url, $fallback = '') | ||
23 | { | ||
24 | $returnUrl = $url; | ||
25 | |||
26 | if (null === $url) { | ||
27 | if ('' !== $fallback) { | ||
28 | $returnUrl = $fallback; | ||
29 | } else { | ||
30 | $returnUrl = $this->router->generate('homepage'); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | return $returnUrl; | ||
35 | } | ||
36 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 6dc1f1d7..f8835198 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -114,3 +114,8 @@ services: | |||
114 | class: Wallabag\CoreBundle\Operator\Doctrine\Matches | 114 | class: Wallabag\CoreBundle\Operator\Doctrine\Matches |
115 | tags: | 115 | tags: |
116 | - { name: rulerz.operator, executor: rulerz.executor.doctrine, operator: matches, inline: true } | 116 | - { name: rulerz.operator, executor: rulerz.executor.doctrine, operator: matches, inline: true } |
117 | |||
118 | wallabag_core.helper.redirect: | ||
119 | class: Wallabag\CoreBundle\Helper\Redirect | ||
120 | arguments: | ||
121 | - "@router" | ||
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php new file mode 100644 index 00000000..da19cf58 --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Helper/RedirectTest.php | |||
@@ -0,0 +1,49 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Helper\Redirect; | ||
6 | |||
7 | class RedirectTest extends \PHPUnit_Framework_TestCase | ||
8 | { | ||
9 | /** @var \Symfony\Component\Routing\Router */ | ||
10 | private $routerMock; | ||
11 | |||
12 | /** @var Redirect */ | ||
13 | private $redirect; | ||
14 | |||
15 | public function setUp() | ||
16 | { | ||
17 | $this->routerMock = $this->getRouterMock(); | ||
18 | $this->redirect = new Redirect($this->routerMock); | ||
19 | } | ||
20 | |||
21 | public function testRedirectToNullWithFallback() | ||
22 | { | ||
23 | $redirectUrl = $this->redirect->to(null, 'fallback'); | ||
24 | |||
25 | $this->assertEquals('fallback', $redirectUrl); | ||
26 | } | ||
27 | |||
28 | public function testRedirectToNullWithoutFallback() | ||
29 | { | ||
30 | $redirectUrl = $this->redirect->to(null); | ||
31 | |||
32 | $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl); | ||
33 | } | ||
34 | |||
35 | public function testRedirectToValidUrl() | ||
36 | { | ||
37 | $redirectUrl = $this->redirect->to('/unread/list'); | ||
38 | |||
39 | $this->assertEquals('/unread/list', $redirectUrl); | ||
40 | } | ||
41 | |||
42 | private function getRouterMock() | ||
43 | { | ||
44 | return $this->getMockBuilder('Symfony\Component\Routing\Router') | ||
45 | ->setMethods(['generate']) | ||
46 | ->disableOriginalConstructor() | ||
47 | ->getMock(); | ||
48 | } | ||
49 | } | ||