Render login page through Slim controller
This commit is contained in:
parent
1410dce2db
commit
6c50a6ccce
20 changed files with 728 additions and 100 deletions
77
application/container/ContainerBuilder.php
Normal file
77
application/container/ContainerBuilder.php
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Container;
|
||||||
|
|
||||||
|
use Shaarli\Bookmark\BookmarkFileService;
|
||||||
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
||||||
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\History;
|
||||||
|
use Shaarli\Plugin\PluginManager;
|
||||||
|
use Shaarli\Render\PageBuilder;
|
||||||
|
use Shaarli\Security\LoginManager;
|
||||||
|
use Shaarli\Security\SessionManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ContainerBuilder
|
||||||
|
*
|
||||||
|
* Helper used to build a Slim container instance with Shaarli's object dependencies.
|
||||||
|
* Note that most injected objects MUST be added as closures, to let the container instantiate
|
||||||
|
* only the objects it requires during the execution.
|
||||||
|
*
|
||||||
|
* @package Container
|
||||||
|
*/
|
||||||
|
class ContainerBuilder
|
||||||
|
{
|
||||||
|
/** @var ConfigManager */
|
||||||
|
protected $conf;
|
||||||
|
|
||||||
|
/** @var SessionManager */
|
||||||
|
protected $session;
|
||||||
|
|
||||||
|
/** @var LoginManager */
|
||||||
|
protected $login;
|
||||||
|
|
||||||
|
public function __construct(ConfigManager $conf, SessionManager $session, LoginManager $login)
|
||||||
|
{
|
||||||
|
$this->conf = $conf;
|
||||||
|
$this->session = $session;
|
||||||
|
$this->login = $login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build(): ShaarliContainer
|
||||||
|
{
|
||||||
|
$container = new ShaarliContainer();
|
||||||
|
$container['conf'] = $this->conf;
|
||||||
|
$container['sessionManager'] = $this->session;
|
||||||
|
$container['loginManager'] = $this->login;
|
||||||
|
$container['plugins'] = function (ShaarliContainer $container): PluginManager {
|
||||||
|
return new PluginManager($container->conf);
|
||||||
|
};
|
||||||
|
|
||||||
|
$container['history'] = function (ShaarliContainer $container): History {
|
||||||
|
return new History($container->conf->get('resource.history'));
|
||||||
|
};
|
||||||
|
|
||||||
|
$container['bookmarkService'] = function (ShaarliContainer $container): BookmarkServiceInterface {
|
||||||
|
return new BookmarkFileService(
|
||||||
|
$container->conf,
|
||||||
|
$container->history,
|
||||||
|
$container->loginManager->isLoggedIn()
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
$container['pageBuilder'] = function (ShaarliContainer $container): PageBuilder {
|
||||||
|
return new PageBuilder(
|
||||||
|
$container->conf,
|
||||||
|
$container->sessionManager->getSession(),
|
||||||
|
$container->bookmarkService,
|
||||||
|
$container->sessionManager->generateToken(),
|
||||||
|
$container->loginManager->isLoggedIn()
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
return $container;
|
||||||
|
}
|
||||||
|
}
|
28
application/container/ShaarliContainer.php
Normal file
28
application/container/ShaarliContainer.php
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Container;
|
||||||
|
|
||||||
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
||||||
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\History;
|
||||||
|
use Shaarli\Render\PageBuilder;
|
||||||
|
use Shaarli\Security\LoginManager;
|
||||||
|
use Shaarli\Security\SessionManager;
|
||||||
|
use Slim\Container;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension of Slim container to document the injected objects.
|
||||||
|
*
|
||||||
|
* @property ConfigManager $conf
|
||||||
|
* @property SessionManager $sessionManager
|
||||||
|
* @property LoginManager $loginManager
|
||||||
|
* @property History $history
|
||||||
|
* @property BookmarkServiceInterface $bookmarkService
|
||||||
|
* @property PageBuilder $pageBuilder
|
||||||
|
*/
|
||||||
|
class ShaarliContainer extends Container
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
57
application/front/ShaarliMiddleware.php
Normal file
57
application/front/ShaarliMiddleware.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Shaarli\Front;
|
||||||
|
|
||||||
|
use Shaarli\Container\ShaarliContainer;
|
||||||
|
use Shaarli\Front\Exception\ShaarliException;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ShaarliMiddleware
|
||||||
|
*
|
||||||
|
* This will be called before accessing any Shaarli controller.
|
||||||
|
*/
|
||||||
|
class ShaarliMiddleware
|
||||||
|
{
|
||||||
|
/** @var ShaarliContainer contains all Shaarli DI */
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
public function __construct(ShaarliContainer $container)
|
||||||
|
{
|
||||||
|
$this->container = $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Middleware execution:
|
||||||
|
* - execute the controller
|
||||||
|
* - return the response
|
||||||
|
*
|
||||||
|
* In case of error, the error template will be displayed with the exception message.
|
||||||
|
*
|
||||||
|
* @param Request $request Slim request
|
||||||
|
* @param Response $response Slim response
|
||||||
|
* @param callable $next Next action
|
||||||
|
*
|
||||||
|
* @return Response response.
|
||||||
|
*/
|
||||||
|
public function __invoke(Request $request, Response $response, callable $next)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$response = $next($request, $response);
|
||||||
|
} catch (ShaarliException $e) {
|
||||||
|
$this->container->pageBuilder->assign('message', $e->getMessage());
|
||||||
|
if ($this->container->conf->get('dev.debug', false)) {
|
||||||
|
$this->container->pageBuilder->assign(
|
||||||
|
'stacktrace',
|
||||||
|
nl2br(get_class($this) .': '. $e->getTraceAsString())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $response->withStatus($e->getCode());
|
||||||
|
$response = $response->write($this->container->pageBuilder->render('error'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
46
application/front/controllers/LoginController.php
Normal file
46
application/front/controllers/LoginController.php
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Controller;
|
||||||
|
|
||||||
|
use Shaarli\Front\Exception\LoginBannedException;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LoginController
|
||||||
|
*
|
||||||
|
* Slim controller used to render the login page.
|
||||||
|
*
|
||||||
|
* The login page is not available if the user is banned
|
||||||
|
* or if open shaarli setting is enabled.
|
||||||
|
*
|
||||||
|
* @package Front\Controller
|
||||||
|
*/
|
||||||
|
class LoginController extends ShaarliController
|
||||||
|
{
|
||||||
|
public function index(Request $request, Response $response): Response
|
||||||
|
{
|
||||||
|
if ($this->ci->loginManager->isLoggedIn() || $this->ci->conf->get('security.open_shaarli', false)) {
|
||||||
|
return $response->withRedirect('./');
|
||||||
|
}
|
||||||
|
|
||||||
|
$userCanLogin = $this->ci->loginManager->canLogin($request->getServerParams());
|
||||||
|
if ($userCanLogin !== true) {
|
||||||
|
throw new LoginBannedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->getParam('username') !== null) {
|
||||||
|
$this->assignView('username', escape($request->getParam('username')));
|
||||||
|
}
|
||||||
|
|
||||||
|
$this
|
||||||
|
->assignView('returnurl', escape($request->getServerParam('HTTP_REFERER')))
|
||||||
|
->assignView('remember_user_default', $this->ci->conf->get('privacy.remember_user_default', true))
|
||||||
|
->assignView('pagetitle', t('Login') .' - '. $this->ci->conf->get('general.title', 'Shaarli'))
|
||||||
|
;
|
||||||
|
|
||||||
|
return $response->write($this->ci->pageBuilder->render('loginform'));
|
||||||
|
}
|
||||||
|
}
|
31
application/front/controllers/ShaarliController.php
Normal file
31
application/front/controllers/ShaarliController.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Controller;
|
||||||
|
|
||||||
|
use Shaarli\Container\ShaarliContainer;
|
||||||
|
|
||||||
|
abstract class ShaarliController
|
||||||
|
{
|
||||||
|
/** @var ShaarliContainer */
|
||||||
|
protected $ci;
|
||||||
|
|
||||||
|
/** @param ShaarliContainer $ci Slim container (extended for attribute completion). */
|
||||||
|
public function __construct(ShaarliContainer $ci)
|
||||||
|
{
|
||||||
|
$this->ci = $ci;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assign variables to RainTPL template through the PageBuilder.
|
||||||
|
*
|
||||||
|
* @param mixed $value Value to assign to the template
|
||||||
|
*/
|
||||||
|
protected function assignView(string $name, $value): self
|
||||||
|
{
|
||||||
|
$this->ci->pageBuilder->assign($name, $value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
15
application/front/exceptions/LoginBannedException.php
Normal file
15
application/front/exceptions/LoginBannedException.php
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Exception;
|
||||||
|
|
||||||
|
class LoginBannedException extends ShaarliException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$message = t('You have been banned after too many failed login attempts. Try again later.');
|
||||||
|
|
||||||
|
parent::__construct($message, 401);
|
||||||
|
}
|
||||||
|
}
|
23
application/front/exceptions/ShaarliException.php
Normal file
23
application/front/exceptions/ShaarliException.php
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Exception;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ShaarliException
|
||||||
|
*
|
||||||
|
* Abstract exception class used to defined any custom exception thrown during front rendering.
|
||||||
|
*
|
||||||
|
* @package Front\Exception
|
||||||
|
*/
|
||||||
|
abstract class ShaarliException extends \Exception
|
||||||
|
{
|
||||||
|
/** Override parent constructor to force $message and $httpCode parameters to be set. */
|
||||||
|
public function __construct(string $message, int $httpCode, Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, $httpCode, $previous);
|
||||||
|
}
|
||||||
|
}
|
|
@ -199,6 +199,23 @@ public function renderPage($page)
|
||||||
$this->tpl->draw($page);
|
$this->tpl->draw($page);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a specific page as string (using a template file).
|
||||||
|
* e.g. $pb->render('picwall');
|
||||||
|
*
|
||||||
|
* @param string $page Template filename (without extension).
|
||||||
|
*
|
||||||
|
* @return string Processed template content
|
||||||
|
*/
|
||||||
|
public function render(string $page): string
|
||||||
|
{
|
||||||
|
if ($this->tpl === false) {
|
||||||
|
$this->initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->tpl->draw($page, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a 404 page (uses the template : tpl/404.tpl)
|
* Render a 404 page (uses the template : tpl/404.tpl)
|
||||||
* usage: $PAGE->render404('The link was deleted')
|
* usage: $PAGE->render404('The link was deleted')
|
||||||
|
|
|
@ -196,4 +196,10 @@ public function hasClientIpChanged($clientIpId)
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return array Local reference to the global $_SESSION array */
|
||||||
|
public function getSession(): array
|
||||||
|
{
|
||||||
|
return $this->session;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1236,8 +1236,19 @@ form {
|
||||||
color: $dark-grey;
|
color: $dark-grey;
|
||||||
}
|
}
|
||||||
|
|
||||||
.page404-container {
|
.pageError-container {
|
||||||
color: $dark-grey;
|
color: $dark-grey;
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
margin: 70px 0 25px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pre {
|
||||||
|
text-align: left;
|
||||||
|
margin: 0 20%;
|
||||||
|
padding: 20px 0;
|
||||||
|
line-height: 0.7em;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EDIT LINK
|
// EDIT LINK
|
||||||
|
|
|
@ -48,9 +48,13 @@
|
||||||
"Shaarli\\Bookmark\\Exception\\": "application/bookmark/exception",
|
"Shaarli\\Bookmark\\Exception\\": "application/bookmark/exception",
|
||||||
"Shaarli\\Config\\": "application/config/",
|
"Shaarli\\Config\\": "application/config/",
|
||||||
"Shaarli\\Config\\Exception\\": "application/config/exception",
|
"Shaarli\\Config\\Exception\\": "application/config/exception",
|
||||||
|
"Shaarli\\Container\\": "application/container",
|
||||||
"Shaarli\\Exceptions\\": "application/exceptions",
|
"Shaarli\\Exceptions\\": "application/exceptions",
|
||||||
"Shaarli\\Feed\\": "application/feed",
|
"Shaarli\\Feed\\": "application/feed",
|
||||||
"Shaarli\\Formatter\\": "application/formatter",
|
"Shaarli\\Formatter\\": "application/formatter",
|
||||||
|
"Shaarli\\Front\\": "application/front",
|
||||||
|
"Shaarli\\Front\\Controller\\": "application/front/controllers",
|
||||||
|
"Shaarli\\Front\\Exception\\": "application/front/exceptions",
|
||||||
"Shaarli\\Http\\": "application/http",
|
"Shaarli\\Http\\": "application/http",
|
||||||
"Shaarli\\Legacy\\": "application/legacy",
|
"Shaarli\\Legacy\\": "application/legacy",
|
||||||
"Shaarli\\Netscape\\": "application/netscape",
|
"Shaarli\\Netscape\\": "application/netscape",
|
||||||
|
|
66
index.php
66
index.php
|
@ -61,29 +61,31 @@
|
||||||
require_once 'application/TimeZone.php';
|
require_once 'application/TimeZone.php';
|
||||||
require_once 'application/Utils.php';
|
require_once 'application/Utils.php';
|
||||||
|
|
||||||
use \Shaarli\ApplicationUtils;
|
use Shaarli\ApplicationUtils;
|
||||||
use Shaarli\Bookmark\BookmarkServiceInterface;
|
|
||||||
use \Shaarli\Bookmark\Exception\BookmarkNotFoundException;
|
|
||||||
use Shaarli\Bookmark\Bookmark;
|
use Shaarli\Bookmark\Bookmark;
|
||||||
use Shaarli\Bookmark\BookmarkFilter;
|
|
||||||
use Shaarli\Bookmark\BookmarkFileService;
|
use Shaarli\Bookmark\BookmarkFileService;
|
||||||
use \Shaarli\Config\ConfigManager;
|
use Shaarli\Bookmark\BookmarkFilter;
|
||||||
use \Shaarli\Feed\CachedPage;
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
||||||
use \Shaarli\Feed\FeedBuilder;
|
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
|
||||||
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\Container\ContainerBuilder;
|
||||||
|
use Shaarli\Feed\CachedPage;
|
||||||
|
use Shaarli\Feed\FeedBuilder;
|
||||||
use Shaarli\Formatter\BookmarkMarkdownFormatter;
|
use Shaarli\Formatter\BookmarkMarkdownFormatter;
|
||||||
use Shaarli\Formatter\FormatterFactory;
|
use Shaarli\Formatter\FormatterFactory;
|
||||||
use \Shaarli\History;
|
use Shaarli\History;
|
||||||
use \Shaarli\Languages;
|
use Shaarli\Languages;
|
||||||
use \Shaarli\Netscape\NetscapeBookmarkUtils;
|
use Shaarli\Netscape\NetscapeBookmarkUtils;
|
||||||
use \Shaarli\Plugin\PluginManager;
|
use Shaarli\Plugin\PluginManager;
|
||||||
use \Shaarli\Render\PageBuilder;
|
use Shaarli\Render\PageBuilder;
|
||||||
use \Shaarli\Render\ThemeUtils;
|
use Shaarli\Render\ThemeUtils;
|
||||||
use \Shaarli\Router;
|
use Shaarli\Router;
|
||||||
use \Shaarli\Security\LoginManager;
|
use Shaarli\Security\LoginManager;
|
||||||
use \Shaarli\Security\SessionManager;
|
use Shaarli\Security\SessionManager;
|
||||||
use \Shaarli\Thumbnailer;
|
use Shaarli\Thumbnailer;
|
||||||
use \Shaarli\Updater\Updater;
|
use Shaarli\Updater\Updater;
|
||||||
use \Shaarli\Updater\UpdaterUtils;
|
use Shaarli\Updater\UpdaterUtils;
|
||||||
|
use Slim\App;
|
||||||
|
|
||||||
// Ensure the PHP version is supported
|
// Ensure the PHP version is supported
|
||||||
try {
|
try {
|
||||||
|
@ -594,19 +596,7 @@ function renderPage($conf, $pluginManager, $bookmarkService, $history, $sessionM
|
||||||
|
|
||||||
// -------- Display login form.
|
// -------- Display login form.
|
||||||
if ($targetPage == Router::$PAGE_LOGIN) {
|
if ($targetPage == Router::$PAGE_LOGIN) {
|
||||||
if ($conf->get('security.open_shaarli')) {
|
header('Location: ./login');
|
||||||
header('Location: ?');
|
|
||||||
exit;
|
|
||||||
} // No need to login for open Shaarli
|
|
||||||
if (isset($_GET['username'])) {
|
|
||||||
$PAGE->assign('username', escape($_GET['username']));
|
|
||||||
}
|
|
||||||
$PAGE->assign('returnurl', (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']):''));
|
|
||||||
// add default state of the 'remember me' checkbox
|
|
||||||
$PAGE->assign('remember_user_default', $conf->get('privacy.remember_user_default'));
|
|
||||||
$PAGE->assign('user_can_login', $loginManager->canLogin($_SERVER));
|
|
||||||
$PAGE->assign('pagetitle', t('Login') .' - '. $conf->get('general.title', 'Shaarli'));
|
|
||||||
$PAGE->renderPage('loginform');
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
// -------- User wants to logout.
|
// -------- User wants to logout.
|
||||||
|
@ -1930,11 +1920,9 @@ function install($conf, $sessionManager, $loginManager)
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$container = new \Slim\Container();
|
$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager);
|
||||||
$container['conf'] = $conf;
|
$container = $containerBuilder->build();
|
||||||
$container['plugins'] = $pluginManager;
|
$app = new App($container);
|
||||||
$container['history'] = $history;
|
|
||||||
$app = new \Slim\App($container);
|
|
||||||
|
|
||||||
// REST API routes
|
// REST API routes
|
||||||
$app->group('/api/v1', function () {
|
$app->group('/api/v1', function () {
|
||||||
|
@ -1953,6 +1941,10 @@ function install($conf, $sessionManager, $loginManager)
|
||||||
$this->get('/history', '\Shaarli\Api\Controllers\HistoryController:getHistory')->setName('getHistory');
|
$this->get('/history', '\Shaarli\Api\Controllers\HistoryController:getHistory')->setName('getHistory');
|
||||||
})->add('\Shaarli\Api\ApiMiddleware');
|
})->add('\Shaarli\Api\ApiMiddleware');
|
||||||
|
|
||||||
|
$app->group('', function () {
|
||||||
|
$this->get('/login', '\Shaarli\Front\Controller\LoginController:index')->setName('login');
|
||||||
|
})->add('\Shaarli\Front\ShaarliMiddleware');
|
||||||
|
|
||||||
$response = $app->run(true);
|
$response = $app->run(true);
|
||||||
|
|
||||||
// Hack to make Slim and Shaarli router work together:
|
// Hack to make Slim and Shaarli router work together:
|
||||||
|
|
49
tests/container/ContainerBuilderTest.php
Normal file
49
tests/container/ContainerBuilderTest.php
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Container;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
||||||
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\History;
|
||||||
|
use Shaarli\Render\PageBuilder;
|
||||||
|
use Shaarli\Security\LoginManager;
|
||||||
|
use Shaarli\Security\SessionManager;
|
||||||
|
|
||||||
|
class ContainerBuilderTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @var ConfigManager */
|
||||||
|
protected $conf;
|
||||||
|
|
||||||
|
/** @var SessionManager */
|
||||||
|
protected $sessionManager;
|
||||||
|
|
||||||
|
/** @var LoginManager */
|
||||||
|
protected $loginManager;
|
||||||
|
|
||||||
|
/** @var ContainerBuilder */
|
||||||
|
protected $containerBuilder;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
||||||
|
$this->sessionManager = $this->createMock(SessionManager::class);
|
||||||
|
$this->loginManager = $this->createMock(LoginManager::class);
|
||||||
|
|
||||||
|
$this->containerBuilder = new ContainerBuilder($this->conf, $this->sessionManager, $this->loginManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBuildContainer(): void
|
||||||
|
{
|
||||||
|
$container = $this->containerBuilder->build();
|
||||||
|
|
||||||
|
static::assertInstanceOf(ConfigManager::class, $container->conf);
|
||||||
|
static::assertInstanceOf(SessionManager::class, $container->sessionManager);
|
||||||
|
static::assertInstanceOf(LoginManager::class, $container->loginManager);
|
||||||
|
static::assertInstanceOf(History::class, $container->history);
|
||||||
|
static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService);
|
||||||
|
static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
|
||||||
|
}
|
||||||
|
}
|
70
tests/front/ShaarliMiddlewareTest.php
Normal file
70
tests/front/ShaarliMiddlewareTest.php
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\Container\ShaarliContainer;
|
||||||
|
use Shaarli\Front\Exception\LoginBannedException;
|
||||||
|
use Shaarli\Render\PageBuilder;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
class ShaarliMiddlewareTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @var ShaarliContainer */
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
/** @var ShaarliMiddleware */
|
||||||
|
protected $middleware;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->container = $this->createMock(ShaarliContainer::class);
|
||||||
|
$this->middleware = new ShaarliMiddleware($this->container);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMiddlewareExecution(): void
|
||||||
|
{
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
$controller = function (Request $request, Response $response): Response {
|
||||||
|
return $response->withStatus(418); // I'm a tea pot
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @var Response $result */
|
||||||
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(418, $result->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMiddlewareExecutionWithException(): void
|
||||||
|
{
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
$controller = function (): void {
|
||||||
|
$exception = new LoginBannedException();
|
||||||
|
|
||||||
|
throw new $exception;
|
||||||
|
};
|
||||||
|
|
||||||
|
$pageBuilder = $this->createMock(PageBuilder::class);
|
||||||
|
$pageBuilder->method('render')->willReturnCallback(function (string $message): string {
|
||||||
|
return $message;
|
||||||
|
});
|
||||||
|
$this->container->pageBuilder = $pageBuilder;
|
||||||
|
|
||||||
|
$conf = $this->createMock(ConfigManager::class);
|
||||||
|
$this->container->conf = $conf;
|
||||||
|
|
||||||
|
/** @var Response $result */
|
||||||
|
$result = $this->middleware->__invoke($request, $response, $controller);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(401, $result->getStatusCode());
|
||||||
|
static::assertContains('error', (string) $result->getBody());
|
||||||
|
}
|
||||||
|
}
|
173
tests/front/controller/LoginControllerTest.php
Normal file
173
tests/front/controller/LoginControllerTest.php
Normal file
|
@ -0,0 +1,173 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shaarli\Front\Controller;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shaarli\Config\ConfigManager;
|
||||||
|
use Shaarli\Container\ShaarliContainer;
|
||||||
|
use Shaarli\Front\Exception\LoginBannedException;
|
||||||
|
use Shaarli\Render\PageBuilder;
|
||||||
|
use Shaarli\Security\LoginManager;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
|
class LoginControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @var ShaarliContainer */
|
||||||
|
protected $container;
|
||||||
|
|
||||||
|
/** @var LoginController */
|
||||||
|
protected $controller;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->container = $this->createMock(ShaarliContainer::class);
|
||||||
|
$this->controller = new LoginController($this->container);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidControllerInvoke(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$request->expects(static::once())->method('getServerParam')->willReturn('> referer');
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$assignedVariables = [];
|
||||||
|
$this->container->pageBuilder
|
||||||
|
->expects(static::exactly(3))
|
||||||
|
->method('assign')
|
||||||
|
->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
|
||||||
|
$assignedVariables[$key] = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
$result = $this->controller->index($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(200, $result->getStatusCode());
|
||||||
|
static::assertSame('loginform', (string) $result->getBody());
|
||||||
|
|
||||||
|
static::assertSame('> referer', $assignedVariables['returnurl']);
|
||||||
|
static::assertSame(true, $assignedVariables['remember_user_default']);
|
||||||
|
static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidControllerInvokeWithUserName(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$request->expects(static::once())->method('getServerParam')->willReturn('> referer');
|
||||||
|
$request->expects(static::exactly(2))->method('getParam')->willReturn('myUser>');
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$assignedVariables = [];
|
||||||
|
$this->container->pageBuilder
|
||||||
|
->expects(static::exactly(4))
|
||||||
|
->method('assign')
|
||||||
|
->willReturnCallback(function ($key, $value) use (&$assignedVariables) {
|
||||||
|
$assignedVariables[$key] = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
})
|
||||||
|
;
|
||||||
|
|
||||||
|
$result = $this->controller->index($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(200, $result->getStatusCode());
|
||||||
|
static::assertSame('loginform', (string) $result->getBody());
|
||||||
|
|
||||||
|
static::assertSame('myUser>', $assignedVariables['username']);
|
||||||
|
static::assertSame('> referer', $assignedVariables['returnurl']);
|
||||||
|
static::assertSame(true, $assignedVariables['remember_user_default']);
|
||||||
|
static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLoginControllerWhileLoggedIn(): void
|
||||||
|
{
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$loginManager = $this->createMock(LoginManager::class);
|
||||||
|
$loginManager->expects(static::once())->method('isLoggedIn')->willReturn(true);
|
||||||
|
$this->container->loginManager = $loginManager;
|
||||||
|
|
||||||
|
$result = $this->controller->index($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['./'], $result->getHeader('Location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLoginControllerOpenShaarli(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$conf = $this->createMock(ConfigManager::class);
|
||||||
|
$conf->method('get')->willReturnCallback(function (string $parameter, $default) {
|
||||||
|
if ($parameter === 'security.open_shaarli') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return $default;
|
||||||
|
});
|
||||||
|
$this->container->conf = $conf;
|
||||||
|
|
||||||
|
$result = $this->controller->index($request, $response);
|
||||||
|
|
||||||
|
static::assertInstanceOf(Response::class, $result);
|
||||||
|
static::assertSame(302, $result->getStatusCode());
|
||||||
|
static::assertSame(['./'], $result->getHeader('Location'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLoginControllerWhileBanned(): void
|
||||||
|
{
|
||||||
|
$this->createValidContainerMockSet();
|
||||||
|
|
||||||
|
$request = $this->createMock(Request::class);
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
|
$loginManager = $this->createMock(LoginManager::class);
|
||||||
|
$loginManager->method('isLoggedIn')->willReturn(false);
|
||||||
|
$loginManager->method('canLogin')->willReturn(false);
|
||||||
|
$this->container->loginManager = $loginManager;
|
||||||
|
|
||||||
|
$this->expectException(LoginBannedException::class);
|
||||||
|
|
||||||
|
$this->controller->index($request, $response);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createValidContainerMockSet(): void
|
||||||
|
{
|
||||||
|
// User logged out
|
||||||
|
$loginManager = $this->createMock(LoginManager::class);
|
||||||
|
$loginManager->method('isLoggedIn')->willReturn(false);
|
||||||
|
$loginManager->method('canLogin')->willReturn(true);
|
||||||
|
$this->container->loginManager = $loginManager;
|
||||||
|
|
||||||
|
// Config
|
||||||
|
$conf = $this->createMock(ConfigManager::class);
|
||||||
|
$conf->method('get')->willReturnCallback(function (string $parameter, $default) {
|
||||||
|
return $default;
|
||||||
|
});
|
||||||
|
$this->container->conf = $conf;
|
||||||
|
|
||||||
|
// PageBuilder
|
||||||
|
$pageBuilder = $this->createMock(PageBuilder::class);
|
||||||
|
$pageBuilder
|
||||||
|
->method('render')
|
||||||
|
->willReturnCallback(function (string $template): string {
|
||||||
|
return $template;
|
||||||
|
})
|
||||||
|
;
|
||||||
|
$this->container->pageBuilder = $pageBuilder;
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
<body>
|
<body>
|
||||||
<div id="pageheader">
|
<div id="pageheader">
|
||||||
{include="page.header"}
|
{include="page.header"}
|
||||||
<div class="center" id="page404" class="page404-container">
|
<div id="pageError" class="pageError-container center">
|
||||||
<h2>{'Sorry, nothing to see here.'|t}</h2>
|
<h2>{'Sorry, nothing to see here.'|t}</h2>
|
||||||
<img src="img/sad_star.png" alt="">
|
<img src="img/sad_star.png" alt="">
|
||||||
<p>{$error_message}</p>
|
<p>{$error_message}</p>
|
||||||
|
|
22
tpl/default/error.html
Normal file
22
tpl/default/error.html
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html{if="$language !== 'auto'"} lang="{$language}"{/if}>
|
||||||
|
<head>
|
||||||
|
{include="includes"}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="pageheader">
|
||||||
|
{include="page.header"}
|
||||||
|
<div id="pageError" class="pageError-container center">
|
||||||
|
<h2>{$message}</h2>
|
||||||
|
|
||||||
|
{if="!empty($stacktrace)"}
|
||||||
|
<pre>
|
||||||
|
{$stacktrace}
|
||||||
|
</pre>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<img src="img/sad_star.png" alt="">
|
||||||
|
</div>
|
||||||
|
{include="page.footer"}
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -5,17 +5,6 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{include="page.header"}
|
{include="page.header"}
|
||||||
{if="!$user_can_login"}
|
|
||||||
<div class="pure-g pure-alert pure-alert-error pure-alert-closable center">
|
|
||||||
<div class="pure-u-2-24"></div>
|
|
||||||
<div class="pure-u-20-24">
|
|
||||||
<p>{'You have been banned after too many failed login attempts. Try again later.'|t}</p>
|
|
||||||
</div>
|
|
||||||
<div class="pure-u-2-24">
|
|
||||||
<i class="fa fa-times pure-alert-close"></i>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{else}
|
|
||||||
<div class="pure-g">
|
<div class="pure-g">
|
||||||
<div class="pure-u-lg-1-3 pure-u-1-24"></div>
|
<div class="pure-u-lg-1-3 pure-u-1-24"></div>
|
||||||
<div id="login-form" class="page-form page-form-light pure-u-lg-1-3 pure-u-22-24 login-form-container">
|
<div id="login-form" class="page-form page-form-light pure-u-lg-1-3 pure-u-22-24 login-form-container">
|
||||||
|
@ -42,7 +31,6 @@ <h2 class="window-title">{'Login'|t}</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="pure-u-lg-1-3 pure-u-1-8"></div>
|
<div class="pure-u-lg-1-3 pure-u-1-8"></div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
|
||||||
|
|
||||||
{include="page.footer"}
|
{include="page.footer"}
|
||||||
</body>
|
</body>
|
||||||
|
|
25
tpl/vintage/error.html
Normal file
25
tpl/vintage/error.html
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
{include="includes"}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="pageheader">
|
||||||
|
{include="page.header"}
|
||||||
|
</div>
|
||||||
|
<div class="error-container">
|
||||||
|
<h1>Error</h1>
|
||||||
|
<p>{$message}</p>
|
||||||
|
|
||||||
|
{if="!empty($stacktrace)"}
|
||||||
|
<br>
|
||||||
|
<pre>
|
||||||
|
{$stacktrace}
|
||||||
|
</pre>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<p>Would you mind <a href="?">clicking here</a>?</p>
|
||||||
|
</div>
|
||||||
|
{include="page.footer"}
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -2,20 +2,15 @@
|
||||||
<html>
|
<html>
|
||||||
<head>{include="includes"}</head>
|
<head>{include="includes"}</head>
|
||||||
<body
|
<body
|
||||||
{if="$user_can_login"}
|
|
||||||
{if="empty($username)"}
|
{if="empty($username)"}
|
||||||
onload="document.loginform.login.focus();"
|
onload="document.loginform.login.focus();"
|
||||||
{else}
|
{else}
|
||||||
onload="document.loginform.password.focus();"
|
onload="document.loginform.password.focus();"
|
||||||
{/if}
|
|
||||||
{/if}>
|
{/if}>
|
||||||
<div id="pageheader">
|
<div id="pageheader">
|
||||||
{include="page.header"}
|
{include="page.header"}
|
||||||
|
|
||||||
<div id="headerform">
|
<div id="headerform">
|
||||||
{if="!$user_can_login"}
|
|
||||||
You have been banned from login after too many failed attempts. Try later.
|
|
||||||
{else}
|
|
||||||
<form method="post" name="loginform">
|
<form method="post" name="loginform">
|
||||||
<label for="login">Login: <input type="text" id="login" name="login" tabindex="1"
|
<label for="login">Login: <input type="text" id="login" name="login" tabindex="1"
|
||||||
{if="!empty($username)"}value="{$username}"{/if}>
|
{if="!empty($username)"}value="{$username}"{/if}>
|
||||||
|
@ -31,7 +26,6 @@
|
||||||
<input type="hidden" name="token" value="{$token}">
|
<input type="hidden" name="token" value="{$token}">
|
||||||
{if="$returnurl"}<input type="hidden" name="returnurl" value="{$returnurl}">{/if}
|
{if="$returnurl"}<input type="hidden" name="returnurl" value="{$returnurl}">{/if}
|
||||||
</form>
|
</form>
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue