Skip to content
Snippets Groups Projects
Commit 5fe68332 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Added proper handling of error messages.

parent 2182551d
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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)) {
......
......@@ -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
......@@ -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 {
......
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
......@@ -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'); ?>">
......
......@@ -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",
......
......@@ -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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment