Compare commits
2 Commits
e58036b2b3
...
b890432e5b
| Author | SHA1 | Date |
|---|---|---|
|
|
b890432e5b | |
|
|
70093b1376 |
|
|
@ -31,6 +31,7 @@ nav {
|
|||
|
||||
nav ul {
|
||||
display: flex;
|
||||
margin: -1px 0 0 0;
|
||||
padding: 0 1.5em;
|
||||
border: 0;
|
||||
border-bottom: 1px solid #000000;
|
||||
|
|
|
|||
|
|
@ -21,8 +21,7 @@ class AuthMiddleware implements MiddlewareInterface
|
|||
// TODO: Lots of stuff. Session middleware, auth handler class, etc...
|
||||
if ($uri->getPath() !== '/login') {
|
||||
// Check authorization via session
|
||||
// TODO username or user ID?
|
||||
if (empty($_SESSION['username'])) {
|
||||
if (empty($_SESSION['user_id'])) {
|
||||
// Not logged in -> Redirect to /login
|
||||
$response = new Response();
|
||||
return $response
|
||||
|
|
|
|||
|
|
@ -19,15 +19,15 @@ class UserHelper
|
|||
|
||||
public function isLoggedIn(): bool
|
||||
{
|
||||
return !empty($_SESSION['username']);
|
||||
return !empty($_SESSION['user_id']);
|
||||
}
|
||||
|
||||
public function getCurrentUser(): AdminUser
|
||||
{
|
||||
$username = $_SESSION['username'] ?? null;
|
||||
if (empty($username)) {
|
||||
$userId = $_SESSION['user_id'] ?? null;
|
||||
if (empty($userId)) {
|
||||
throw new RuntimeException('Not logged in!');
|
||||
}
|
||||
return $this->adminUserRepository->getUserByName($username);
|
||||
return $this->adminUserRepository->getUserById($userId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ namespace MailAccountAdmin;
|
|||
|
||||
use DI\Container;
|
||||
use MailAccountAdmin\Common\UserHelper;
|
||||
use MailAccountAdmin\Frontend\Accounts\AccountController;
|
||||
use MailAccountAdmin\Frontend\Domains\DomainController;
|
||||
use MailAccountAdmin\Frontend\Login\LoginController;
|
||||
use MailAccountAdmin\Frontend\Dashboard\DashboardController;
|
||||
use MailAccountAdmin\Repositories\AdminUserRepository;
|
||||
|
|
@ -64,7 +66,7 @@ class Dependencies
|
|||
);
|
||||
});
|
||||
|
||||
// Login page
|
||||
// Frontend controllers
|
||||
$container->set(LoginController::class, function (ContainerInterface $c) {
|
||||
return new LoginController(
|
||||
$c->get(self::TWIG),
|
||||
|
|
@ -72,14 +74,24 @@ class Dependencies
|
|||
$c->get(AdminUserRepository::class),
|
||||
);
|
||||
});
|
||||
|
||||
// Dashboard
|
||||
$container->set(DashboardController::class, function (ContainerInterface $c) {
|
||||
return new DashboardController(
|
||||
$c->get(self::TWIG),
|
||||
$c->get(UserHelper::class),
|
||||
);
|
||||
});
|
||||
$container->set(DomainController::class, function (ContainerInterface $c) {
|
||||
return new DomainController(
|
||||
$c->get(self::TWIG),
|
||||
$c->get(UserHelper::class),
|
||||
);
|
||||
});
|
||||
$container->set(AccountController::class, function (ContainerInterface $c) {
|
||||
return new AccountController(
|
||||
$c->get(self::TWIG),
|
||||
$c->get(UserHelper::class),
|
||||
);
|
||||
});
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MailAccountAdmin\Frontend\Accounts;
|
||||
|
||||
use MailAccountAdmin\Frontend\BaseController;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
class AccountController extends BaseController
|
||||
{
|
||||
public function showAccounts(Request $request, Response $response): Response
|
||||
{
|
||||
$renderData = [
|
||||
];
|
||||
|
||||
return $this->view->render($response, 'accounts.html.twig', $renderData);
|
||||
}
|
||||
}
|
||||
|
|
@ -11,13 +11,6 @@ class DashboardController extends BaseController
|
|||
{
|
||||
public function showDashboard(Request $request, Response $response): Response
|
||||
{
|
||||
$currentUser = $this->userHelper->getCurrentUser();
|
||||
|
||||
$renderData = [
|
||||
'username' => $currentUser->getUsername(),
|
||||
'user' => $currentUser,
|
||||
];
|
||||
|
||||
return $this->view->render($response, 'dashboard.html.twig', $renderData);
|
||||
return $this->view->render($response, 'dashboard.html.twig');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace MailAccountAdmin\Frontend\Domains;
|
||||
|
||||
use MailAccountAdmin\Frontend\BaseController;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
class DomainController extends BaseController
|
||||
{
|
||||
public function showDomains(Request $request, Response $response): Response
|
||||
{
|
||||
$renderData = [
|
||||
];
|
||||
|
||||
return $this->view->render($response, 'domains.html.twig', $renderData);
|
||||
}
|
||||
}
|
||||
|
|
@ -66,7 +66,7 @@ class LoginController extends BaseController
|
|||
}
|
||||
|
||||
// Set login session
|
||||
$_SESSION['username'] = $user->getUsername();
|
||||
$_SESSION['user_id'] = $user->getId();
|
||||
return $response
|
||||
->withHeader('Location', '/')
|
||||
->withStatus(303);
|
||||
|
|
|
|||
|
|
@ -17,6 +17,22 @@ class AdminUserRepository
|
|||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AdminUserNotFoundException
|
||||
*/
|
||||
public function getUserById(int $userId): AdminUser
|
||||
{
|
||||
$statement = $this->pdo->prepare('SELECT * FROM admin_users WHERE admin_id = :admin_id LIMIT 1');
|
||||
$statement->execute(['admin_id' => $userId]);
|
||||
|
||||
if ($statement->rowCount() < 1) {
|
||||
throw new AdminUserNotFoundException("Admin with ID '$userId' was not found.");
|
||||
}
|
||||
|
||||
$row = $statement->fetch(PDO::FETCH_ASSOC);
|
||||
return AdminUser::createFromArray($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AdminUserNotFoundException
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace MailAccountAdmin;
|
||||
|
||||
use MailAccountAdmin\Frontend\Accounts\AccountController;
|
||||
use MailAccountAdmin\Frontend\Dashboard\DashboardController;
|
||||
use MailAccountAdmin\Frontend\Domains\DomainController;
|
||||
use MailAccountAdmin\Frontend\Login\LoginController;
|
||||
use Slim\App;
|
||||
|
||||
|
|
@ -18,5 +20,13 @@ class Routes
|
|||
|
||||
// Dashboard
|
||||
$app->get('/', DashboardController::class . ':showDashboard');
|
||||
|
||||
// Domains
|
||||
$app->get('/domains', DomainController::class . ':showDomains');
|
||||
$app->get('/domains/{foo}', DomainController::class . ':showDomains');
|
||||
|
||||
// Accounts
|
||||
$app->get('/accounts', AccountController::class . ':showAccounts');
|
||||
$app->get('/accounts/{foo}', AccountController::class . ':showAccounts');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
{% extends "base.html.twig" %}
|
||||
|
||||
{% block title %}Accounts{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Accounts</h2>
|
||||
|
||||
<p>List of accounts ... <b>TODO</b></p>
|
||||
<p><a href="/accounts/42">Test</a></p>
|
||||
{% endblock %}
|
||||
|
|
@ -18,9 +18,15 @@
|
|||
|
||||
<nav>
|
||||
<ul>
|
||||
<li class="nav_current_page"><a href="/">Dashboard</a></li>
|
||||
<li><a href="/domains">Domains</a></li>
|
||||
<li><a href="/accounts">Accounts</a></li>
|
||||
{% macro navbar_item(path, text) -%}
|
||||
<li{% if current_url() == path or current_url() starts with path ~ '/' %} class="nav_current_page"{% endif %}>
|
||||
<a href="{{ path }}">{{ text }}</a>
|
||||
</li>
|
||||
{%- endmacro -%}
|
||||
|
||||
{{ _self.navbar_item('/', 'Dashboard') }}
|
||||
{{ _self.navbar_item('/domains', 'Domains') }}
|
||||
{{ _self.navbar_item('/accounts', 'Accounts') }}
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
|
|
|
|||
|
|
@ -5,5 +5,5 @@
|
|||
{% block content %}
|
||||
<h2>Dashboard</h2>
|
||||
|
||||
<p>Hello, {{ username }}!</p>
|
||||
<p>Hello, {{ current_user_name }}!</p>
|
||||
{% endblock %}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
{% extends "base.html.twig" %}
|
||||
|
||||
{% block title %}Domains{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Domains</h2>
|
||||
|
||||
<p>List of domains ... <b>TODO</b></p>
|
||||
<p><a href="/domains/42">Test</a></p>
|
||||
{% endblock %}
|
||||
Loading…
Reference in New Issue