aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-05-22 13:20:31 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commit2899ebb5b5e82890c877151f5c02045266ac9973 (patch)
tree0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /application
parentaf290059d10319e76d1e7d78b592cab99c26d91a (diff)
downloadShaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.gz
Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.tar.zst
Shaarli-2899ebb5b5e82890c877151f5c02045266ac9973.zip
Initialize admin Slim controllers
- Reorganize visitor controllers - Fix redirection with Slim's requests base path - Fix daily links
Diffstat (limited to 'application')
-rw-r--r--application/front/ShaarliMiddleware.php7
-rw-r--r--application/front/controller/admin/LogoutController.php (renamed from application/front/controllers/LogoutController.php)6
-rw-r--r--application/front/controller/admin/SessionFilterController.php (renamed from application/front/controllers/SessionFilterController.php)14
-rw-r--r--application/front/controller/admin/ShaarliAdminController.php21
-rw-r--r--application/front/controller/visitor/DailyController.php (renamed from application/front/controllers/DailyController.php)6
-rw-r--r--application/front/controller/visitor/FeedController.php (renamed from application/front/controllers/FeedController.php)6
-rw-r--r--application/front/controller/visitor/LoginController.php (renamed from application/front/controllers/LoginController.php)6
-rw-r--r--application/front/controller/visitor/OpenSearchController.php (renamed from application/front/controllers/OpenSearchController.php)6
-rw-r--r--application/front/controller/visitor/PictureWallController.php (renamed from application/front/controllers/PictureWallController.php)6
-rw-r--r--application/front/controller/visitor/ShaarliVisitorController.php (renamed from application/front/controllers/ShaarliController.php)15
-rw-r--r--application/front/controller/visitor/TagCloudController.php (renamed from application/front/controllers/TagCloudController.php)6
-rw-r--r--application/front/controller/visitor/TagController.php (renamed from application/front/controllers/TagController.php)6
-rw-r--r--application/front/exceptions/LoginBannedException.php2
-rw-r--r--application/front/exceptions/ShaarliFrontException.php (renamed from application/front/exceptions/ShaarliException.php)2
-rw-r--r--application/front/exceptions/ThumbnailsDisabledException.php2
-rw-r--r--application/front/exceptions/UnauthorizedException.php15
16 files changed, 76 insertions, 50 deletions
diff --git a/application/front/ShaarliMiddleware.php b/application/front/ShaarliMiddleware.php
index fa6c6467..f8992e0b 100644
--- a/application/front/ShaarliMiddleware.php
+++ b/application/front/ShaarliMiddleware.php
@@ -3,7 +3,8 @@
3namespace Shaarli\Front; 3namespace Shaarli\Front;
4 4
5use Shaarli\Container\ShaarliContainer; 5use Shaarli\Container\ShaarliContainer;
6use Shaarli\Front\Exception\ShaarliException; 6use Shaarli\Front\Exception\ShaarliFrontException;
7use Shaarli\Front\Exception\UnauthorizedException;
7use Slim\Http\Request; 8use Slim\Http\Request;
8use Slim\Http\Response; 9use Slim\Http\Response;
9 10
@@ -39,7 +40,7 @@ class ShaarliMiddleware
39 { 40 {
40 try { 41 try {
41 $response = $next($request, $response); 42 $response = $next($request, $response);
42 } catch (ShaarliException $e) { 43 } catch (ShaarliFrontException $e) {
43 $this->container->pageBuilder->assign('message', $e->getMessage()); 44 $this->container->pageBuilder->assign('message', $e->getMessage());
44 if ($this->container->conf->get('dev.debug', false)) { 45 if ($this->container->conf->get('dev.debug', false)) {
45 $this->container->pageBuilder->assign( 46 $this->container->pageBuilder->assign(
@@ -50,6 +51,8 @@ class ShaarliMiddleware
50 51
51 $response = $response->withStatus($e->getCode()); 52 $response = $response->withStatus($e->getCode());
52 $response = $response->write($this->container->pageBuilder->render('error')); 53 $response = $response->write($this->container->pageBuilder->render('error'));
54 } catch (UnauthorizedException $e) {
55 return $response->withRedirect($request->getUri()->getBasePath() . '/login');
53 } 56 }
54 57
55 return $response; 58 return $response;
diff --git a/application/front/controllers/LogoutController.php b/application/front/controller/admin/LogoutController.php
index aba078c3..41e81984 100644
--- a/application/front/controllers/LogoutController.php
+++ b/application/front/controller/admin/LogoutController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Admin;
6 6
7use Shaarli\Security\LoginManager; 7use Shaarli\Security\LoginManager;
8use Slim\Http\Request; 8use Slim\Http\Request;
@@ -13,10 +13,8 @@ use Slim\Http\Response;
13 * 13 *
14 * Slim controller used to logout the user. 14 * Slim controller used to logout the user.
15 * It invalidates page cache and terminate the user session. Then it redirects to the homepage. 15 * It invalidates page cache and terminate the user session. Then it redirects to the homepage.
16 *
17 * @package Front\Controller
18 */ 16 */
19class LogoutController extends ShaarliController 17class LogoutController extends ShaarliAdminController
20{ 18{
21 public function index(Request $request, Response $response): Response 19 public function index(Request $request, Response $response): Response
22 { 20 {
diff --git a/application/front/controllers/SessionFilterController.php b/application/front/controller/admin/SessionFilterController.php
index a021dc37..69a16ec3 100644
--- a/application/front/controllers/SessionFilterController.php
+++ b/application/front/controller/admin/SessionFilterController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Admin;
6 6
7use Shaarli\Bookmark\BookmarkFilter; 7use Shaarli\Bookmark\BookmarkFilter;
8use Shaarli\Security\SessionManager; 8use Shaarli\Security\SessionManager;
@@ -13,10 +13,8 @@ use Slim\Http\Response;
13 * Class SessionFilterController 13 * Class SessionFilterController
14 * 14 *
15 * Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc. 15 * Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc.
16 *
17 * @package Shaarli\Front\Controller
18 */ 16 */
19class SessionFilterController extends ShaarliController 17class SessionFilterController extends ShaarliAdminController
20{ 18{
21 /** 19 /**
22 * GET /links-per-page: set the number of bookmarks to display per page in homepage 20 * GET /links-per-page: set the number of bookmarks to display per page in homepage
@@ -33,7 +31,7 @@ class SessionFilterController extends ShaarliController
33 abs(intval($linksPerPage)) 31 abs(intval($linksPerPage))
34 ); 32 );
35 33
36 return $this->redirectFromReferer($response, ['linksperpage'], ['nb']); 34 return $this->redirectFromReferer($request, $response, ['linksperpage'], ['nb']);
37 } 35 }
38 36
39 /** 37 /**
@@ -42,7 +40,7 @@ class SessionFilterController extends ShaarliController
42 public function visibility(Request $request, Response $response, array $args): Response 40 public function visibility(Request $request, Response $response, array $args): Response
43 { 41 {
44 if (false === $this->container->loginManager->isLoggedIn()) { 42 if (false === $this->container->loginManager->isLoggedIn()) {
45 return $this->redirectFromReferer($response, ['visibility']); 43 return $this->redirectFromReferer($request, $response, ['visibility']);
46 } 44 }
47 45
48 $newVisibility = $args['visibility'] ?? null; 46 $newVisibility = $args['visibility'] ?? null;
@@ -63,7 +61,7 @@ class SessionFilterController extends ShaarliController
63 $this->container->sessionManager->deleteSessionParameter(SessionManager::KEY_VISIBILITY); 61 $this->container->sessionManager->deleteSessionParameter(SessionManager::KEY_VISIBILITY);
64 } 62 }
65 63
66 return $this->redirectFromReferer($response, ['visibility']); 64 return $this->redirectFromReferer($request, $response, ['visibility']);
67 } 65 }
68 66
69 /** 67 /**
@@ -76,6 +74,6 @@ class SessionFilterController extends ShaarliController
76 empty($this->container->sessionManager->getSessionParameter(SessionManager::KEY_UNTAGGED_ONLY)) 74 empty($this->container->sessionManager->getSessionParameter(SessionManager::KEY_UNTAGGED_ONLY))
77 ); 75 );
78 76
79 return $this->redirectFromReferer($response, ['untaggedonly', 'untagged-only']); 77 return $this->redirectFromReferer($request, $response, ['untaggedonly', 'untagged-only']);
80 } 78 }
81} 79}
diff --git a/application/front/controller/admin/ShaarliAdminController.php b/application/front/controller/admin/ShaarliAdminController.php
new file mode 100644
index 00000000..ea703f62
--- /dev/null
+++ b/application/front/controller/admin/ShaarliAdminController.php
@@ -0,0 +1,21 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Controller\Admin;
6
7use Shaarli\Container\ShaarliContainer;
8use Shaarli\Front\Controller\Visitor\ShaarliVisitorController;
9use Shaarli\Front\Exception\UnauthorizedException;
10
11abstract class ShaarliAdminController extends ShaarliVisitorController
12{
13 public function __construct(ShaarliContainer $container)
14 {
15 parent::__construct($container);
16
17 if (true !== $this->container->loginManager->isLoggedIn()) {
18 throw new UnauthorizedException();
19 }
20 }
21}
diff --git a/application/front/controllers/DailyController.php b/application/front/controller/visitor/DailyController.php
index 4a0735aa..47e2503a 100644
--- a/application/front/controllers/DailyController.php
+++ b/application/front/controller/visitor/DailyController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use DateTime; 7use DateTime;
8use DateTimeImmutable; 8use DateTimeImmutable;
@@ -14,10 +14,8 @@ use Slim\Http\Response;
14 * Class DailyController 14 * Class DailyController
15 * 15 *
16 * Slim controller used to render the daily page. 16 * Slim controller used to render the daily page.
17 *
18 * @package Front\Controller
19 */ 17 */
20class DailyController extends ShaarliController 18class DailyController extends ShaarliVisitorController
21{ 19{
22 public static $DAILY_RSS_NB_DAYS = 8; 20 public static $DAILY_RSS_NB_DAYS = 8;
23 21
diff --git a/application/front/controllers/FeedController.php b/application/front/controller/visitor/FeedController.php
index 78d826d9..70664635 100644
--- a/application/front/controllers/FeedController.php
+++ b/application/front/controller/visitor/FeedController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Shaarli\Feed\FeedBuilder; 7use Shaarli\Feed\FeedBuilder;
8use Slim\Http\Request; 8use Slim\Http\Request;
@@ -12,10 +12,8 @@ use Slim\Http\Response;
12 * Class FeedController 12 * Class FeedController
13 * 13 *
14 * Slim controller handling ATOM and RSS feed. 14 * Slim controller handling ATOM and RSS feed.
15 *
16 * @package Front\Controller
17 */ 15 */
18class FeedController extends ShaarliController 16class FeedController extends ShaarliVisitorController
19{ 17{
20 public function atom(Request $request, Response $response): Response 18 public function atom(Request $request, Response $response): Response
21 { 19 {
diff --git a/application/front/controllers/LoginController.php b/application/front/controller/visitor/LoginController.php
index ae3599e0..4de2f55d 100644
--- a/application/front/controllers/LoginController.php
+++ b/application/front/controller/visitor/LoginController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Shaarli\Front\Exception\LoginBannedException; 7use Shaarli\Front\Exception\LoginBannedException;
8use Slim\Http\Request; 8use Slim\Http\Request;
@@ -15,10 +15,8 @@ use Slim\Http\Response;
15 * 15 *
16 * The login page is not available if the user is banned 16 * The login page is not available if the user is banned
17 * or if open shaarli setting is enabled. 17 * or if open shaarli setting is enabled.
18 *
19 * @package Front\Controller
20 */ 18 */
21class LoginController extends ShaarliController 19class LoginController extends ShaarliVisitorController
22{ 20{
23 public function index(Request $request, Response $response): Response 21 public function index(Request $request, Response $response): Response
24 { 22 {
diff --git a/application/front/controllers/OpenSearchController.php b/application/front/controller/visitor/OpenSearchController.php
index fa32c5f1..0fd68db6 100644
--- a/application/front/controllers/OpenSearchController.php
+++ b/application/front/controller/visitor/OpenSearchController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Slim\Http\Request; 7use Slim\Http\Request;
8use Slim\Http\Response; 8use Slim\Http\Response;
@@ -12,10 +12,8 @@ use Slim\Http\Response;
12 * 12 *
13 * Slim controller used to render open search template. 13 * Slim controller used to render open search template.
14 * This allows to add Shaarli as a search engine within the browser. 14 * This allows to add Shaarli as a search engine within the browser.
15 *
16 * @package front\controllers
17 */ 15 */
18class OpenSearchController extends ShaarliController 16class OpenSearchController extends ShaarliVisitorController
19{ 17{
20 public function index(Request $request, Response $response): Response 18 public function index(Request $request, Response $response): Response
21 { 19 {
diff --git a/application/front/controllers/PictureWallController.php b/application/front/controller/visitor/PictureWallController.php
index 08d31b29..4e1dce8c 100644
--- a/application/front/controllers/PictureWallController.php
+++ b/application/front/controller/visitor/PictureWallController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Shaarli\Front\Exception\ThumbnailsDisabledException; 7use Shaarli\Front\Exception\ThumbnailsDisabledException;
8use Shaarli\Thumbnailer; 8use Shaarli\Thumbnailer;
@@ -14,10 +14,8 @@ use Slim\Http\Response;
14 * 14 *
15 * Slim controller used to render the pictures wall page. 15 * Slim controller used to render the pictures wall page.
16 * If thumbnails mode is set to NONE, we just render the template without any image. 16 * If thumbnails mode is set to NONE, we just render the template without any image.
17 *
18 * @package Front\Controller
19 */ 17 */
20class PictureWallController extends ShaarliController 18class PictureWallController extends ShaarliVisitorController
21{ 19{
22 public function index(Request $request, Response $response): Response 20 public function index(Request $request, Response $response): Response
23 { 21 {
diff --git a/application/front/controllers/ShaarliController.php b/application/front/controller/visitor/ShaarliVisitorController.php
index bfff5fcf..655b3baa 100644
--- a/application/front/controllers/ShaarliController.php
+++ b/application/front/controller/visitor/ShaarliVisitorController.php
@@ -2,13 +2,14 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Shaarli\Bookmark\BookmarkFilter; 7use Shaarli\Bookmark\BookmarkFilter;
8use Shaarli\Container\ShaarliContainer; 8use Shaarli\Container\ShaarliContainer;
9use Slim\Http\Request;
9use Slim\Http\Response; 10use Slim\Http\Response;
10 11
11abstract class ShaarliController 12abstract class ShaarliVisitorController
12{ 13{
13 /** @var ShaarliContainer */ 14 /** @var ShaarliContainer */
14 protected $container; 15 protected $container;
@@ -89,9 +90,13 @@ abstract class ShaarliController
89 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop. 90 * @param array $loopTerms Terms to remove from path and query string to prevent direction loop.
90 * @param array $clearParams List of parameter to remove from the query string of the referrer. 91 * @param array $clearParams List of parameter to remove from the query string of the referrer.
91 */ 92 */
92 protected function redirectFromReferer(Response $response, array $loopTerms = [], array $clearParams = []): Response 93 protected function redirectFromReferer(
93 { 94 Request $request,
94 $defaultPath = './'; 95 Response $response,
96 array $loopTerms = [],
97 array $clearParams = []
98 ): Response {
99 $defaultPath = $request->getUri()->getBasePath();
95 $referer = $this->container->environment['HTTP_REFERER'] ?? null; 100 $referer = $this->container->environment['HTTP_REFERER'] ?? null;
96 101
97 if (null !== $referer) { 102 if (null !== $referer) {
diff --git a/application/front/controllers/TagCloudController.php b/application/front/controller/visitor/TagCloudController.php
index 1ff7c2e6..15b6d7b7 100644
--- a/application/front/controllers/TagCloudController.php
+++ b/application/front/controller/visitor/TagCloudController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Slim\Http\Request; 7use Slim\Http\Request;
8use Slim\Http\Response; 8use Slim\Http\Response;
@@ -11,10 +11,8 @@ use Slim\Http\Response;
11 * Class TagCloud 11 * Class TagCloud
12 * 12 *
13 * Slim controller used to render the tag cloud and tag list pages. 13 * Slim controller used to render the tag cloud and tag list pages.
14 *
15 * @package Front\Controller
16 */ 14 */
17class TagCloudController extends ShaarliController 15class TagCloudController extends ShaarliVisitorController
18{ 16{
19 protected const TYPE_CLOUD = 'cloud'; 17 protected const TYPE_CLOUD = 'cloud';
20 protected const TYPE_LIST = 'list'; 18 protected const TYPE_LIST = 'list';
diff --git a/application/front/controllers/TagController.php b/application/front/controller/visitor/TagController.php
index a1d5ad5b..a0bc1d1b 100644
--- a/application/front/controllers/TagController.php
+++ b/application/front/controller/visitor/TagController.php
@@ -2,7 +2,7 @@
2 2
3declare(strict_types=1); 3declare(strict_types=1);
4 4
5namespace Shaarli\Front\Controller; 5namespace Shaarli\Front\Controller\Visitor;
6 6
7use Slim\Http\Request; 7use Slim\Http\Request;
8use Slim\Http\Response; 8use Slim\Http\Response;
@@ -11,10 +11,8 @@ use Slim\Http\Response;
11 * Class TagController 11 * Class TagController
12 * 12 *
13 * Slim controller handle tags. 13 * Slim controller handle tags.
14 *
15 * @package Front\Controller
16 */ 14 */
17class TagController extends ShaarliController 15class TagController extends ShaarliVisitorController
18{ 16{
19 /** 17 /**
20 * Add another tag in the current search through an HTTP redirection. 18 * Add another tag in the current search through an HTTP redirection.
diff --git a/application/front/exceptions/LoginBannedException.php b/application/front/exceptions/LoginBannedException.php
index b31a4a14..79d0ea15 100644
--- a/application/front/exceptions/LoginBannedException.php
+++ b/application/front/exceptions/LoginBannedException.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
4 4
5namespace Shaarli\Front\Exception; 5namespace Shaarli\Front\Exception;
6 6
7class LoginBannedException extends ShaarliException 7class LoginBannedException extends ShaarliFrontException
8{ 8{
9 public function __construct() 9 public function __construct()
10 { 10 {
diff --git a/application/front/exceptions/ShaarliException.php b/application/front/exceptions/ShaarliFrontException.php
index 800bfbec..fc8eb92b 100644
--- a/application/front/exceptions/ShaarliException.php
+++ b/application/front/exceptions/ShaarliFrontException.php
@@ -13,7 +13,7 @@ use Throwable;
13 * 13 *
14 * @package Front\Exception 14 * @package Front\Exception
15 */ 15 */
16abstract class ShaarliException extends \Exception 16abstract class ShaarliFrontException extends \Exception
17{ 17{
18 /** Override parent constructor to force $message and $httpCode parameters to be set. */ 18 /** Override parent constructor to force $message and $httpCode parameters to be set. */
19 public function __construct(string $message, int $httpCode, Throwable $previous = null) 19 public function __construct(string $message, int $httpCode, Throwable $previous = null)
diff --git a/application/front/exceptions/ThumbnailsDisabledException.php b/application/front/exceptions/ThumbnailsDisabledException.php
index 1b9cf5b7..0ed337f5 100644
--- a/application/front/exceptions/ThumbnailsDisabledException.php
+++ b/application/front/exceptions/ThumbnailsDisabledException.php
@@ -4,7 +4,7 @@ declare(strict_types=1);
4 4
5namespace Shaarli\Front\Exception; 5namespace Shaarli\Front\Exception;
6 6
7class ThumbnailsDisabledException extends ShaarliException 7class ThumbnailsDisabledException extends ShaarliFrontException
8{ 8{
9 public function __construct() 9 public function __construct()
10 { 10 {
diff --git a/application/front/exceptions/UnauthorizedException.php b/application/front/exceptions/UnauthorizedException.php
new file mode 100644
index 00000000..4231094a
--- /dev/null
+++ b/application/front/exceptions/UnauthorizedException.php
@@ -0,0 +1,15 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Front\Exception;
6
7/**
8 * Class UnauthorizedException
9 *
10 * Exception raised if the user tries to access a ShaarliAdminController while logged out.
11 */
12class UnauthorizedException extends \Exception
13{
14
15}