Compare commits

..

3 Commits

Author SHA1 Message Date
tracer e1d1ef5eeb added all needed routes (so far) 2022-10-24 20:20:52 +02:00
tracer cfc4e22497 initial commit 2022-10-24 20:19:28 +02:00
tracer 66f02c8c6e added router to main 2022-10-24 20:12:48 +02:00
3 changed files with 131 additions and 6 deletions

View File

@ -1,13 +1,68 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
ini_set(option: 'display_errors', value: 1);
ini_set(option: 'display_startup_errors', value: 1);
// no one sane should ignore deprecations
error_reporting(error_level: E_ALL);
session_start();
require dirname(path: __DIR__) . '/src/bootstrap.php';
use App\Controller\AddressBook;
use App\Controller\AddressBookAdminController;
use App\Controller\AddressBookController;
use App\Controller\SecurityController;
use App\Service\Container;
use App\Service\Router;
$container = new \App\Service\Container();
$container = new Container();
$router = $container->get(className: Router::class);
$security = $container->get(className: SecurityController::class);
$addressBook = $container->get(className: AddressBookController::class);
$addressBookAdmin = $container->get(className: AddressBookAdminController::class);
$addressBook = $container->get(AddressBook::class);
//$addressBook = new AddressBook();
// TODO maybe refactor route adding to the controllers?
// I currently think that makes sense.
$router->addRoute(name: 'app_login', route: '/login', callback: function () use ($security) {
$security->login();
});
$router->addRoute(name: 'app_logout', route: '/logout', callback: function () use ($security) {
$security->logout();
});
$router->addRoute(name: 'app_main', route: '/', callback: function () use ($addressBook) {
$addressBook->main();
});
$router->addRoute(name: 'app_admin', route: '/admin', callback: function () use ($addressBookAdmin) {
$addressBookAdmin->admin();
});
$router->addRoute(name: 'app_admin_users', route: '/admin/users', callback: function () use ($addressBookAdmin) {
$addressBookAdmin->adminUser();
});
$router->addRoute(name: 'app_admin_users_edit', route: '/admin/users/{nick}', callback: function (array $parameters) use ($addressBookAdmin) {
$addressBookAdmin->adminUserEdit(parameters: $parameters);
});
$router->addRoute(name: 'app_admin_users_add', route: '/admin/users/add', callback: function () use ($addressBookAdmin) {
$addressBookAdmin->adminUserAdd();
});
$router->addRoute(name: 'app_admin_users_delete', route: '/admin/users/delete/{nick}', callback: function (array $parameters) use ($addressBookAdmin) {
$addressBookAdmin->adminUserDelete(parameters: $parameters);
});
$router->handleRouting();

View File

@ -10,23 +10,29 @@
namespace App\Controller;
use App\Entity\User;
use App\Service\Router;
use App\Service\Template;
use App\Repository\UserRepository;
use JetBrains\PhpStorm\NoReturn;
class AddressBookController
{
public function __construct(
private readonly Template $template,
private readonly User $user,
private readonly UserRepository $userRepository
private readonly UserRepository $userRepository,
private readonly Router $router
)
{
// empty body
}
#[NoReturn]
public function main(): void
{
$this->template->render(templateName: 'index.html.php', vars: [
'user' => $this->user
'user' => $this->user,
'router' => $this->router
]);
}

View File

@ -0,0 +1,64 @@
<?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Controller;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Service\Router;
use App\Service\Template;
class SecurityController
{
public function __construct(
private readonly Template $template,
private readonly UserRepository $userRepository,
private readonly Router $router
)
{
}
public function login(): never
{
if (!empty($_POST)) {
$nick = $_POST['nick'] ?? '';
$password = $_POST['password'] ?? '';
if ($nick && $password) {
if ($user = $this->userRepository->findbyNick(nick: $nick)) {
if (password_verify(password: $password, hash: $user->getPassword())) {
$_SESSION['user_id'] = $user->getId();
header(header: 'Location: /');
exit(0);
} else {
$message = "Wrong credentials.";
}
} else {
$message = "User not found.";
}
} else {
$message = 'You need to enter your credentials.';
}
}
$this->template->render(templateName: 'security/login.html.php', vars: [
'user' => $user ?? new User(),
'message' => $message ?? '',
'router' => $this->router
]);
}
function logout(): void
{
session_unset();
header(header: 'Location: /');
}
}