Compare commits

..

No commits in common. "e1d1ef5eeb4cd5c596336f40e8881b0467c70664" and "10efd6d1a665c7db1920296946295de9db08a3ff" have entirely different histories.

3 changed files with 6 additions and 131 deletions

View File

@ -1,68 +1,13 @@
<?php <?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_errors', value: 1);
ini_set(option: 'display_startup_errors', value: 1); ini_set(option: 'display_startup_errors', value: 1);
// no one sane should ignore deprecations
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
session_start();
require dirname(path: __DIR__) . '/src/bootstrap.php'; require dirname(path: __DIR__) . '/src/bootstrap.php';
use App\Controller\AddressBookAdminController; use App\Controller\AddressBook;
use App\Controller\AddressBookController;
use App\Controller\SecurityController;
use App\Service\Container;
use App\Service\Router;
$container = new Container(); $container = new \App\Service\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);
// TODO maybe refactor route adding to the controllers? //$addressBook = new AddressBook();
// 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,29 +10,23 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\User; use App\Entity\User;
use App\Service\Router;
use App\Service\Template; use App\Service\Template;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use JetBrains\PhpStorm\NoReturn;
class AddressBookController class AddressBookController
{ {
public function __construct( public function __construct(
private readonly Template $template, private readonly Template $template,
private readonly User $user, private readonly User $user,
private readonly UserRepository $userRepository, private readonly UserRepository $userRepository
private readonly Router $router
) )
{ {
// empty body
} }
#[NoReturn]
public function main(): void public function main(): void
{ {
$this->template->render(templateName: 'index.html.php', vars: [ $this->template->render(templateName: 'index.html.php', vars: [
'user' => $this->user, 'user' => $this->user
'router' => $this->router
]); ]);
} }

View File

@ -1,64 +0,0 @@
<?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: /');
}
}