diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-05-22 13:20:31 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-07-23 21:19:21 +0200 |
commit | 2899ebb5b5e82890c877151f5c02045266ac9973 (patch) | |
tree | 0c4e2684c7f6d161f92a21181bfa4b2f78d6a82f /application/front/controller/admin | |
parent | af290059d10319e76d1e7d78b592cab99c26d91a (diff) | |
download | Shaarli-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/front/controller/admin')
3 files changed, 129 insertions, 0 deletions
diff --git a/application/front/controller/admin/LogoutController.php b/application/front/controller/admin/LogoutController.php new file mode 100644 index 00000000..41e81984 --- /dev/null +++ b/application/front/controller/admin/LogoutController.php | |||
@@ -0,0 +1,29 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use Shaarli\Security\LoginManager; | ||
8 | use Slim\Http\Request; | ||
9 | use Slim\Http\Response; | ||
10 | |||
11 | /** | ||
12 | * Class LogoutController | ||
13 | * | ||
14 | * Slim controller used to logout the user. | ||
15 | * It invalidates page cache and terminate the user session. Then it redirects to the homepage. | ||
16 | */ | ||
17 | class LogoutController extends ShaarliAdminController | ||
18 | { | ||
19 | public function index(Request $request, Response $response): Response | ||
20 | { | ||
21 | $this->container->pageCacheManager->invalidateCaches(); | ||
22 | $this->container->sessionManager->logout(); | ||
23 | |||
24 | // TODO: switch to a simple Cookie manager allowing to check the session, and create mocks. | ||
25 | setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, $this->container->webPath); | ||
26 | |||
27 | return $response->withRedirect('./'); | ||
28 | } | ||
29 | } | ||
diff --git a/application/front/controller/admin/SessionFilterController.php b/application/front/controller/admin/SessionFilterController.php new file mode 100644 index 00000000..69a16ec3 --- /dev/null +++ b/application/front/controller/admin/SessionFilterController.php | |||
@@ -0,0 +1,79 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use Shaarli\Bookmark\BookmarkFilter; | ||
8 | use Shaarli\Security\SessionManager; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | /** | ||
13 | * Class SessionFilterController | ||
14 | * | ||
15 | * Slim controller used to handle filters stored in the user session, such as visibility, links per page, etc. | ||
16 | */ | ||
17 | class SessionFilterController extends ShaarliAdminController | ||
18 | { | ||
19 | /** | ||
20 | * GET /links-per-page: set the number of bookmarks to display per page in homepage | ||
21 | */ | ||
22 | public function linksPerPage(Request $request, Response $response): Response | ||
23 | { | ||
24 | $linksPerPage = $request->getParam('nb') ?? null; | ||
25 | if (null === $linksPerPage || false === is_numeric($linksPerPage)) { | ||
26 | $linksPerPage = $this->container->conf->get('general.links_per_page', 20); | ||
27 | } | ||
28 | |||
29 | $this->container->sessionManager->setSessionParameter( | ||
30 | SessionManager::KEY_LINKS_PER_PAGE, | ||
31 | abs(intval($linksPerPage)) | ||
32 | ); | ||
33 | |||
34 | return $this->redirectFromReferer($request, $response, ['linksperpage'], ['nb']); | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | * GET /visibility: allows to display only public or only private bookmarks in linklist | ||
39 | */ | ||
40 | public function visibility(Request $request, Response $response, array $args): Response | ||
41 | { | ||
42 | if (false === $this->container->loginManager->isLoggedIn()) { | ||
43 | return $this->redirectFromReferer($request, $response, ['visibility']); | ||
44 | } | ||
45 | |||
46 | $newVisibility = $args['visibility'] ?? null; | ||
47 | if (false === in_array($newVisibility, [BookmarkFilter::$PRIVATE, BookmarkFilter::$PUBLIC], true)) { | ||
48 | $newVisibility = null; | ||
49 | } | ||
50 | |||
51 | $currentVisibility = $this->container->sessionManager->getSessionParameter(SessionManager::KEY_VISIBILITY); | ||
52 | |||
53 | // Visibility not set or not already expected value, set expected value, otherwise reset it | ||
54 | if ($newVisibility !== null && (null === $currentVisibility || $currentVisibility !== $newVisibility)) { | ||
55 | // See only public bookmarks | ||
56 | $this->container->sessionManager->setSessionParameter( | ||
57 | SessionManager::KEY_VISIBILITY, | ||
58 | $newVisibility | ||
59 | ); | ||
60 | } else { | ||
61 | $this->container->sessionManager->deleteSessionParameter(SessionManager::KEY_VISIBILITY); | ||
62 | } | ||
63 | |||
64 | return $this->redirectFromReferer($request, $response, ['visibility']); | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * GET /untagged-only: allows to display only bookmarks without any tag | ||
69 | */ | ||
70 | public function untaggedOnly(Request $request, Response $response): Response | ||
71 | { | ||
72 | $this->container->sessionManager->setSessionParameter( | ||
73 | SessionManager::KEY_UNTAGGED_ONLY, | ||
74 | empty($this->container->sessionManager->getSessionParameter(SessionManager::KEY_UNTAGGED_ONLY)) | ||
75 | ); | ||
76 | |||
77 | return $this->redirectFromReferer($request, $response, ['untaggedonly', 'untagged-only']); | ||
78 | } | ||
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 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use Shaarli\Container\ShaarliContainer; | ||
8 | use Shaarli\Front\Controller\Visitor\ShaarliVisitorController; | ||
9 | use Shaarli\Front\Exception\UnauthorizedException; | ||
10 | |||
11 | abstract 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 | } | ||