Skip to content

Commit a5fe247

Browse files
committed
First Authenticated Only Page
1 parent 82e8696 commit a5fe247

File tree

14 files changed

+247
-8
lines changed

14 files changed

+247
-8
lines changed

config/container.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
->addArgument(\App\Service\Authentication\AccountService::class)
2020
->addArgument(\App\Service\Authentication\PasswordService::class);
2121

22+
$container->add(\App\Controller\Login\OverviewController::class)
23+
->addArgument(League\Plates\Engine::class);
24+
2225
#
2326
# Services
2427
#
@@ -44,6 +47,12 @@
4447
#
4548
$container->add(\App\Validation\Authentication\RegisterValidation::class);
4649

50+
#
51+
# Middlewares
52+
#
53+
$container->add(\App\Middleware\AuthenticationMiddleware::class)
54+
->addArgument(\App\Service\Authentication\AccountService::class);
55+
4756
#
4857
# Dependencies
4958
#
@@ -63,7 +72,8 @@
6372

6473
$container->add(League\Plates\Engine::class)
6574
->addArgument(__DIR__.'/../template')
66-
->addMethodCall('loadExtension', [\App\PlatesExtension\Translator\TranslationExtension::class]);
75+
->addMethodCall('loadExtension', [\App\PlatesExtension\Translator\TranslationExtension::class])
76+
->addMethodCall('loadExtension', [\App\PlatesExtension\Authentication\AuthenticationExtension::class]);
6777

6878
$container->add(\App\PlatesExtension\Translator\TranslationExtension::class)
6979
->addArgument(\App\PlatesExtension\Translator\Translation::class);
@@ -74,6 +84,9 @@
7484

7585
$container->add(\App\PlatesExtension\Translator\JsonTranslation::class);
7686

87+
$container->add(\App\PlatesExtension\Authentication\AuthenticationExtension::class)
88+
->addArgument(\App\Service\Authentication\AccountService::class);
89+
7790
$responseFactory = (new \Laminas\Diactoros\ResponseFactory());
7891
$jsonStrategy = (new \League\Route\Strategy\JsonStrategy($responseFactory))->setContainer($container);
7992
$applicationStrategy = (new \League\Route\Strategy\ApplicationStrategy())->setContainer($container);

config/routes.php

+4
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,9 @@
1919
$router->get('/authentication/login', 'App\Controller\Authentication\LoginController::load');
2020
$router->post('/authentication/login', 'App\Controller\Authentication\LoginController::load');
2121

22+
$router->get('/authentication/logout', 'App\Controller\Authentication\LogoutController::load');
23+
24+
$router->get('/overview', 'App\Controller\Login\OverviewController::load')->lazyMiddlewares([\App\Middleware\AuthenticationMiddleware::class]);
25+
2226
$response = $router->dispatch($request);
2327
(new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);

src/App/Controller/Authentication/LoginController.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Model\Authentication\Account;
77
use App\Service\Authentication\AccountService;
88
use App\Service\Authentication\PasswordService;
9+
use App\Software;
910
use League\Plates\Engine;
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
@@ -62,6 +63,7 @@ public function login(ServerRequestInterface $request)
6263
{
6364
MESSAGES->add('success', 'login-account-successful');
6465
$this->accountService->updateLastUserLogin($account);
66+
$_SESSION[Software::SESSION_USERID_NAME] = $account->getId();
6567
if(!$account->isSetupComplete()) {
6668
header("Location: /authentication/setup");
6769
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Controller\Authentication;
4+
5+
use Laminas\Diactoros\Response\RedirectResponse;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
class LogoutController
10+
{
11+
12+
public function load(ServerRequestInterface $request): ResponseInterface
13+
{
14+
session_destroy();
15+
16+
return new RedirectResponse('/authentication/login');
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Controller\Login;
4+
5+
use App\Http\HtmlResponse;
6+
use League\Plates\Engine;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
10+
class OverviewController
11+
{
12+
13+
public function __construct(
14+
private readonly Engine $engine
15+
)
16+
{
17+
}
18+
19+
public function load(ServerRequestInterface $request): ResponseInterface
20+
{
21+
return new HtmlResponse(
22+
$this->engine->render('login/overview')
23+
);
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Middleware;
4+
5+
use App\Model\Authentication\Account;
6+
use App\Service\Authentication\AccountService;
7+
use App\Software;
8+
use Laminas\Diactoros\Response\RedirectResponse;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\ServerRequestInterface;
11+
use Psr\Http\Server\MiddlewareInterface;
12+
use Psr\Http\Server\RequestHandlerInterface;
13+
14+
class AuthenticationMiddleware implements MiddlewareInterface
15+
{
16+
17+
public function __construct(
18+
private readonly AccountService $accountService
19+
)
20+
{
21+
}
22+
23+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
24+
{
25+
26+
if(empty($_SESSION[Software::SESSION_USERID_NAME]))
27+
{
28+
return new RedirectResponse('/authentication/login');
29+
}
30+
31+
$account = new Account();
32+
$account->setId($_SESSION[Software::SESSION_USERID_NAME]);
33+
34+
$accountData = $this->accountService->findAccountById($account->getId());
35+
if($accountData === FALSE || $accountData['active'] === 0)
36+
{
37+
session_destroy();
38+
return new RedirectResponse('/authentication/login');
39+
}
40+
41+
$account->setEmail($accountData['email']);
42+
$account->setBusiness($accountData['business']);
43+
$account->setAdmin($accountData['isAdmin']);
44+
45+
return $handler->handle($request->withAttribute(Account::class, $account));
46+
47+
}
48+
}

src/App/Model/Authentication/Account.php

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Account
1616
private bool $business;
1717
private bool $active;
1818
private bool $setupComplete;
19+
private bool $admin;
1920

2021
public function getId(): int
2122
{
@@ -107,4 +108,14 @@ public function setSetupComplete(bool $setupComplete): void
107108
$this->setupComplete = $setupComplete;
108109
}
109110

111+
public function isAdmin(): bool
112+
{
113+
return $this->admin;
114+
}
115+
116+
public function setAdmin(bool $admin): void
117+
{
118+
$this->admin = $admin;
119+
}
120+
110121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\PlatesExtension\Authentication;
4+
5+
use App\Service\Authentication\AccountService;
6+
use App\Software;
7+
use League\Plates\Engine;
8+
use League\Plates\Extension\ExtensionInterface;
9+
10+
#[\AllowDynamicProperties]
11+
class AuthenticationExtension implements ExtensionInterface
12+
{
13+
14+
public function __construct(private readonly AccountService $accountService)
15+
{
16+
}
17+
18+
public function register(Engine $engine)
19+
{
20+
$engine->registerFunction('getAccountInformation', [$this, 'getAccountInformation']);
21+
}
22+
23+
public function getAccountInformation(): false|array
24+
{
25+
if(
26+
isset($_SESSION[Software::SESSION_USERID_NAME]) &&
27+
!empty($_SESSION[Software::SESSION_USERID_NAME])
28+
)
29+
{
30+
$account = $this->accountService->findAccountById($_SESSION[Software::SESSION_USERID_NAME]);
31+
if($account !== FALSE)
32+
{
33+
return $account;
34+
}
35+
}
36+
37+
return false;
38+
}
39+
40+
}

src/App/Software.php

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Software
2424
public const CONSOLE_LOG_FILENAME = 'console.log';
2525

2626
public const DATABASE_TIME_FORMAT = 'Y-m-d H:i:s';
27+
public const SESSION_USERID_NAME = 'webtoolkit_login_id';
2728

2829
/**
2930
* @throws EnvironmentException

template/authentication/login.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php $this->layout('basetemplate'); ?>
22

3+
<?php $this->insert('element/navigation') ?>
4+
35
<div class="container mt-3">
46

57
<?php foreach (MESSAGES->getAll() as $alert): ?>
@@ -13,11 +15,10 @@
1315
<div class="col-4">
1416
<div class="card">
1517
<div class="card-header text-center">
16-
<h3><?= $_ENV['SOFTWARE_TITLE'] ?></h3>
18+
<h4><?= $this->e($this->translate('login-account-title')) ?></h4>
1719
</div>
1820
<div class="card-body">
1921
<form action="" method="post">
20-
<h4><?= $this->e($this->translate('login-account-title')) ?></h4>
2122
<div class="mb-3">
2223
<label for="email" class="form-text"><?= $this->e($this->translate('email')) ?></label>
2324
<input type="email" class="form-control" name="email" id="email" required>

template/authentication/registration.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php $this->layout('basetemplate'); ?>
22

3+
<?php $this->insert('element/navigation') ?>
4+
35
<div class="container mt-3">
46

57
<?php foreach (MESSAGES->getAll() as $alert): ?>
@@ -13,11 +15,10 @@
1315
<div class="col-4">
1416
<div class="card">
1517
<div class="card-header text-center">
16-
<h3><?= $_ENV['SOFTWARE_TITLE'] ?></h3>
18+
<h4><?= $this->e($this->translate('create-account-title')) ?></h4>
1719
</div>
1820
<div class="card-body">
1921
<form action="" method="post">
20-
<h4><?= $this->e($this->translate('create-account-title')) ?></h4>
2122
<div class="mb-3 mt-4">
2223
<span><?= $this->e($this->translate('create-account-for')) ?></span><br>
2324
<div class="form-check form-check-inline">

template/element/navigation.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<nav class="navbar navbar-expand-lg bg-body-tertiary">
2+
<div class="container">
3+
<a class="navbar-brand" href="/">
4+
<b><?= $_ENV['SOFTWARE_TITLE'] ?></b>
5+
</a>
6+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
7+
<span class="navbar-toggler-icon"></span>
8+
</button>
9+
<div class="collapse navbar-collapse" id="navbarNav">
10+
<ul class="navbar-nav me-auto">
11+
<?php if($this->getAccountInformation() === FALSE): ?>
12+
<li class="nav-item">
13+
<a class="nav-link" href="/">Startseite</a>
14+
</li>
15+
<?php else: ?>
16+
<li class="nav-item">
17+
<a class="nav-link" href="/overview">Dashboard</a>
18+
</li>
19+
<?php endif; ?>
20+
<li class="nav-item">
21+
<a class="nav-link" href="#">Produkte</a>
22+
</li>
23+
</ul>
24+
25+
<ul class="navbar-nav ms-start">
26+
<?php if($this->getAccountInformation() === FALSE): ?>
27+
<li class="nav-item">
28+
<a class="nav-link" href="/authentication/login">Anmelden</a>
29+
</li>
30+
<li class="nav-item">
31+
<a class="nav-link btn btn-primary" href="/authentication/registration">Registrieren</a>
32+
</li>
33+
<?php else: ?>
34+
<li class="nav-item dropdown">
35+
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
36+
Angemeldet als <b><?= $this->getAccountInformation()['name'] ?></b>
37+
</a>
38+
<ul class="dropdown-menu">
39+
<li><a class="dropdown-item" href="#">Kontoeinstellungen</a></li>
40+
<li><a class="dropdown-item" href="#">Lizenzierungen</a></li>
41+
<hr>
42+
<li><a class="dropdown-item" href="/authentication/logout">Abmelden</a></li>
43+
</ul>
44+
</li>
45+
<a class="nav-link">
46+
<span class="badge rounded-pill text-bg-secondary">Basic</span>
47+
</a>
48+
<?php if($this->getAccountInformation()['isAdmin'] === 1): ?>
49+
<a class="nav-link text-danger" href="/admin/dashboard">
50+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-house-gear" viewBox="0 0 16 16">
51+
<path d="M7.293 1.5a1 1 0 0 1 1.414 0L11 3.793V2.5a.5.5 0 0 1 .5-.5h1a.5.5 0 0 1 .5.5v3.293l2.354 2.353a.5.5 0 0 1-.708.708L8 2.207l-5 5V13.5a.5.5 0 0 0 .5.5h4a.5.5 0 0 1 0 1h-4A1.5 1.5 0 0 1 2 13.5V8.207l-.646.647a.5.5 0 1 1-.708-.708L7.293 1.5Z"/>
52+
<path d="M11.886 9.46c.18-.613 1.048-.613 1.229 0l.043.148a.64.64 0 0 0 .921.382l.136-.074c.561-.306 1.175.308.87.869l-.075.136a.64.64 0 0 0 .382.92l.149.045c.612.18.612 1.048 0 1.229l-.15.043a.64.64 0 0 0-.38.921l.074.136c.305.561-.309 1.175-.87.87l-.136-.075a.64.64 0 0 0-.92.382l-.045.149c-.18.612-1.048.612-1.229 0l-.043-.15a.64.64 0 0 0-.921-.38l-.136.074c-.561.305-1.175-.309-.87-.87l.075-.136a.64.64 0 0 0-.382-.92l-.148-.044c-.613-.181-.613-1.049 0-1.23l.148-.043a.64.64 0 0 0 .382-.921l-.074-.136c-.306-.561.308-1.175.869-.87l.136.075a.64.64 0 0 0 .92-.382l.045-.148ZM14 12.5a1.5 1.5 0 1 0-3 0 1.5 1.5 0 0 0 3 0Z"/>
53+
</svg>
54+
</a>
55+
<?php endif; ?>
56+
<?php endif; ?>
57+
</ul>
58+
</div>
59+
</div>
60+
</nav>

template/login/overview.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php $this->layout('basetemplate') ?>
2+
3+
<?php $this->insert('element/navigation') ?>
4+
5+
<div class="container mt-3">
6+
7+
8+
9+
</div>

template/publicPage/index.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php $this->layout('basetemplate') ?>
22

3-
<h1>Webtookit</h1>
3+
<?php $this->insert('element/navigation') ?>
44

5-
Version: <?= \App\Software::VERSION ?> <br>
6-
Build: <?= \App\Software::BUILD ?>
5+
<div class="container mt-3">
6+
<h1>Webtookit</h1>
7+
8+
Version: <?= \App\Software::VERSION ?> <br>
9+
Build: <?= \App\Software::BUILD ?>
10+
</div>

0 commit comments

Comments
 (0)