From 5fe683320dd16c345610e5bbb3a542720c66dc15 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Sun, 25 Sep 2016 23:56:13 +0200 Subject: [PATCH] Added proper handling of error messages. --- backend/helper/RendererHelper.php | 12 ++++++++---- backend/helper/ViewHelper.php | 1 + index.php | 4 ++-- login.php | 5 +++-- res/login.css | 30 ++++++++++++++++++++---------- templates/login.phtml | 3 +++ translations/de.json | 7 ++++++- translations/en.json | 7 ++++++- 8 files changed, 49 insertions(+), 20 deletions(-) diff --git a/backend/helper/RendererHelper.php b/backend/helper/RendererHelper.php index 5a06c0e..3529434 100644 --- a/backend/helper/RendererHelper.php +++ b/backend/helper/RendererHelper.php @@ -8,10 +8,12 @@ require_once 'TranslationHelper.php'; class RendererHelper { private $config; private $translator; + private $sessionHelper; - public function __construct(Config $config) { + public function __construct(Config $config, SessionHelper $sessionHelper = null) { $this->config = $config; $this->translator = new TranslationHelper($config); + $this->sessionHelper = $sessionHelper; } public function renderError($e) { @@ -33,13 +35,15 @@ class RendererHelper { echo json_encode($json) . "\n"; } - public function renderPage(string $template, array $vars = null) { + public function renderPage(string $template, array $vars = []) { $translation = $this->translator->loadTranslation($this->translator->findMatchingLanguage($_SERVER['HTTP_ACCEPT_LANGUAGE'])); - $viewHelper = new ViewHelper($translation, $vars); + $viewHelper = new ViewHelper($translation, array_merge($this->sessionHelper->vars, $vars)); $viewHelper->render($template); } - public function redirect(string $page, string $flash = null) { + public function redirect(string $page, array $vars = []) { header('Location: ' . $this->config->path_prefix . $page); + $this->sessionHelper->startSession(); + $this->sessionHelper->vars = $vars; } } \ No newline at end of file diff --git a/backend/helper/ViewHelper.php b/backend/helper/ViewHelper.php index ff7e78a..1d22ba9 100644 --- a/backend/helper/ViewHelper.php +++ b/backend/helper/ViewHelper.php @@ -28,6 +28,7 @@ class ViewHelper { $var = $var[$key]; echo $var; }; + $vars = $this->vars; $path = $this->template_dir . '/' . $template_file . '.phtml'; if (file_exists($path)) { diff --git a/index.php b/index.php index 63fa891..8e66747 100644 --- a/index.php +++ b/index.php @@ -9,12 +9,12 @@ require_once 'backend/helper/SessionHelper.php'; $session = SessionHelper::getInstance(); $config = Config::createFromGlobals(); -$renderer = new RendererHelper($config); +$renderer = new RendererHelper($config, $session); $backend = Backend::createFromConfig($config); if (!$backend->authenticate($session->username ?: '', $session->password ?: '')) { $session->destroy(); - $renderer->redirect('/login.php'); + $renderer->redirect('/login.php', ['message' => 'login.message.error_unauthed', 'type' => 'error']); } else { $renderer->renderPage('search', ['username' => $session->username]); } \ No newline at end of file diff --git a/login.php b/login.php index 1aa44fd..d1bd68f 100644 --- a/login.php +++ b/login.php @@ -9,7 +9,7 @@ require_once 'backend/helper/SessionHelper.php'; $session = SessionHelper::getInstance(); $config = Config::createFromGlobals(); -$renderer = new RendererHelper($config); +$renderer = new RendererHelper($config, $session); $backend = Backend::createFromConfig($config); if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['action'] === 'login') { @@ -21,10 +21,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_GET['action']) && $_GET['ac $renderer->redirect('/'); } else { syslog(LOG_ERR, "Could not authenticate user " . $username); + $renderer->redirect('/login.php', ['message' => 'login.message.error_invalid', 'type' => 'error']); } } elseif (isset($_GET['action']) && $_GET['action'] === 'logout') { $session->destroy(); - $renderer->redirect('/login.php'); + $renderer->redirect('/login.php', ['message' => 'login.message.success_logout', 'type' => 'info']); } else if ($backend->authenticate($session->username ?: '', $session->password ?: '')) { $renderer->redirect('/'); } else { diff --git a/res/login.css b/res/login.css index 29f00a5..7e0bde0 100644 --- a/res/login.css +++ b/res/login.css @@ -1,8 +1,13 @@ -body :first-child { +body { + font-family: 'Open Sans', Helvetica Neue, Helvetica, Arial, arial, sans-serif; + -webkit-font-smoothing: antialiased; +} + +body > :first-child { margin-top: 8rem; } -body:last-child { +body > :last-child { margin-bottom: 2rem; } @@ -11,13 +16,11 @@ form { margin: 0 auto 25px; border-radius: 2px; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3); - width: 274px; - padding: 40px 40px; + width: 16rem; + padding: 1.5rem 2rem; } h1 { - font-family: 'Open Sans', arial; - -webkit-font-smoothing: antialiased; color: #555; font-size: 42px; font-weight: 300; @@ -26,7 +29,6 @@ h1 { } h2 { - font-family: 'Open Sans', arial; color: #555; font-size: 18px; font-weight: 400; @@ -76,18 +78,26 @@ input[type=submit] { background-image: linear-gradient(top, #4d90fe, #4787ed); width: 100%; display: block; - margin-bottom: 10px; z-index: 1; position: relative; box-sizing: border-box; } +form .message { + margin: 0 0 1rem 0; + font-size: 14px; +} + +form .message.error { + color: #b71c1c; +} + @media (max-width: 800px) { - body :first-child { + body > :first-child { margin-top: 1rem; } - body:last-child { + body > :last-child { margin-bottom: 1rem; } } \ No newline at end of file diff --git a/templates/login.phtml b/templates/login.phtml index ab93067..cfca65c 100644 --- a/templates/login.phtml +++ b/templates/login.phtml @@ -21,6 +21,9 @@ <h2><?php $t('login.description'); ?></h2> <form method="post" action="login.php?action=login"> + <?php if ($vars['message']): ?> + <p class="message <?php echo $vars['type']; ?>"><?php echo $t($vars['message']); ?></p> + <?php endif; ?> <input name="username" type="text" placeholder="<?php $t('login.username'); ?>"> <input name="password" type="password" placeholder="<?php $t('login.password'); ?>"> <input type="submit" value="<?php $t('login.submit'); ?>"> diff --git a/translations/de.json b/translations/de.json index 2ba1b89..190ad83 100644 --- a/translations/de.json +++ b/translations/de.json @@ -14,7 +14,12 @@ "description": "You have to login to access this page", "username": "Benutzername", "password": "Passwort", - "submit": "Anmelden" + "submit": "Anmelden", + "message": { + "success_logout": "You have successfully logged out.", + "error_invalid": "Invalid username/password combination.", + "error_unauthed": "You need to be logged in to access this page." + } }, "search": "Suchen", "logout": "Abmelden", diff --git a/translations/en.json b/translations/en.json index be43c3c..1d63a05 100644 --- a/translations/en.json +++ b/translations/en.json @@ -14,7 +14,12 @@ "description": "You have to login to access this page", "username": "Username", "password": "Password", - "submit": "Login" + "submit": "Login", + "message": { + "success_logout": "You have successfully logged out.", + "error_invalid": "Invalid username/password combination.", + "error_unauthed": "You need to be logged in to access this page." + } }, "search": "Search", "logout": "Logout", -- GitLab