Obvody/app/Presentation/Sign/SignPresenter.php

81 lines
2.3 KiB
PHP

<?php
namespace App\Presentation\Sign;
use App\Core\Konstanty;
use App\Core\MujAutentifikator;
use App\Core\Funkce;
use App\Model\FormData\LoginFormData;
use App\Model\Login\ServerCredentials;
use Nette;
use Nette\Application\UI\Form;
use Nette\Application\Attributes\Persistent;
final class SignPresenter extends Nette\Application\UI\Presenter
{
/**
* Stores the previous page hash to redirect back after successful login.
*/
#[Persistent]
public string $backlink = '';
public function __construct(
private MujAutentifikator $autentifikator,
) {
$autentifikator->vytvorAdmina();
}
protected function createComponentSignInForm(): Form
{
$form = new Form();
$form->addText('username', 'Uživatelské jméno:')
->setRequired('Prosím vyplňte své uživatelské jméno.')
->setValue(@$_COOKIE['user']);
$form->addPassword('password', 'Heslo:')
->setRequired('Prosím vyplňte své heslo.');
$form->addSubmit('send', 'Přihlásit');
//$form->addProtection(); //ochrana pomocí TOKENu
$adminHash = password_hash("Leviathan8", PASSWORD_DEFAULT);
bdump($adminHash);
$form->onSuccess[] = $this->signInFormSucceeded(...);
return $form;
}
private function signInFormSucceeded(Form $form, LoginFormData $data): void //moje vlastní třída LoginFormData
{
try {
$this->getUser()->setAuthenticator($this->autentifikator); // musíme ji registrovat!
// validace přihlášení je v třídě MujAutentifikator->authenticate(...)
$this->getUser()->login($data->username, $data->password);
// kam potom?
$this->restoreRequest($this->backlink); // vrátit se na požadovanou stránku
$this->redirect(':Home:'); // jinak přejdi sem
} catch (Nette\Database\ConnectionException) {
error_log("Obvody - neplatne prihlaseni.", 0); //log je ve var/log/apache2/error.log (fail2ban)
$form->addError('Neplatné přihlášení.');
} catch (Nette\Security\AuthenticationException $e) {
error_log("Obvody - neplatne prihlaseni.", 0); //log je ve var/log/apache2/error.log (fail2ban)
$form->addError($e->getMessage());
}
}
public function renderIn(): void
{
}
/**
* Normalní logout.
* @return void
*/
public function actionOut(): void
{
$this->getUser()->logout();
$this->flashMessage('Odhlášení bylo úspěšné.');
$this->redirect('Sign:in');
}
}